这篇文章主要为大家介绍了MyBatis多对多关联映射的创建示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
示例
【通过班级查询老师信息】
创建t_classes
创建t_classessTeacher
创建t_teacher
创建Classes
package com.po;
import java.util.List;
public class Classes {
private Integer cid;
private String cname;
private List<Teacher> teachers;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Classes [cid=" + cid + ", cname=" + cname + ", teachers=" + teachers + "]";
}
}
创建Teacher
package com.po;
import java.util.List;
public class Teacher {
private Integer tid;
private String tname;
private List<Classes> classes;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public List<Classes> getClasses() {
return classes;
}
public void setClasses(List<Classes> classes) {
this.classes = classes;
}
@Override
public String toString() {
return "Teacher [tid=" + tid + ", tname=" + tname + "]";
}
}
创建ClassesMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.ClassesMapper">
<!-- 自定义结果映射 -->
<resultMap type="com.po.Classes" id="ClassesWithTeacherResult">
<id property="cid" column="cid"/>
<result property="cname" column="cname"/>
<!-- 多表关联映射 -->
<collection property="teachers" ofType="com.po.Teacher">
<id property="tid" column="tid"/>
<result property="tname" column="tname"/>
</collection>
</resultMap>
<select id="findClassesWithTeacher" parameterType="Integer" resultMap="ClassesWithTeacherResult">
select * from t_classes c,t_teacher t,t_classessTeacher ct
where ct.cid=c.cid
and ct.tid=t.tid
and c.cid=#{cid}
</select>
</mapper>
配置mybatis-config.xml
<!-- 配置Mapper的位置 -->
<mappers>
<mapper resource="com/mapper/ClassesMapper.xml"/>
</mappers>
创建ClassesDao
package com.dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.factory.SessionFactory;
import com.po.Classes;
public class ClassesDao {
private SqlSession session=SessionFactory.getSqlSession();
@Test
public void findClassesWithTeacher() {
Classes classess=session.selectOne("com.mapper.ClassesMapper.findClassesWithTeacher", 1);
System.out.println(classess);
}
}
执行 findClassesWithTeacher()方法
以上就是MyBatis多对多关联映射创建示例的详细内容,更多关于MyBatis多对多关联映射的资料请关注编程学习网其它相关文章!
沃梦达教程
本文标题为:MyBatis多对多关联映射创建示例
猜你喜欢
- Java中的日期时间处理及格式化处理 2023-04-18
- 基于Java Agent的premain方式实现方法耗时监控问题 2023-06-17
- 深入了解Spring的事务传播机制 2023-06-02
- JSP 制作验证码的实例详解 2023-07-30
- ExecutorService Callable Future多线程返回结果原理解析 2023-06-01
- JSP页面间传值问题实例简析 2023-08-03
- Springboot整合minio实现文件服务的教程详解 2022-12-03
- SpringBoot使用thymeleaf实现一个前端表格方法详解 2023-06-06
- Spring Security权限想要细化到按钮实现示例 2023-03-07
- Java实现顺序表的操作详解 2023-05-19