多对多关联! 其实就是两个一对多的关联! 比如说 一个学生可以有多个老师!一个老师可以有多个学生!
那么 学生和老师之间的关系 可以理解为 多对多的关联关系!
关键是怎么建立数据库中两个表之间的关系???
这时候需要一个中间表来组织两张表的关系!
创建对应的数据库表!
student表
teacher表
middle表
创建对应的实体类
/** *学生对应的实体类 */ public class Student { private Integer sId; private String sName; //一个学生可以有多个老师 private Set<Teacher> teachers=new HashSet<Teacher>(); public Integer getsId() { return sId; } public void setsId(Integer sId) { this.sId = sId; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } public Student(Integer sId, String sName, Set<Teacher> teachers) { super(); this.sId = sId; this.sName = sName; this.teachers = teachers; } public Student() { super(); } //在双向关联的时候 只能一方显示关联信息 否则会出现stackOverflow 异常 @Override public String toString() { return "Student [sId=" + sId + ", sName=" + sName + ", teachers=" + teachers + "]"; } }
/** *老师的实体类 */ public class Teacher { private Integer tId; private String tName; //一个老师可以有多个学生 private Set<Student> students=new HashSet<Student>(); 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 Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } public Teacher(Integer tId, String tName, Set<Student> students) { super(); this.tId = tId; this.tName = tName; this.students = students; } public Teacher() { super(); } @Override public String toString() { return "Teacher [tId=" + tId + ", tName=" + tName + ", students=" + students.size() + "]"; } }
创建对应的dao
public interface StudentDao { /** * 根据学生的编号查询所有对应老师信息 */ Student selectTeahcerById(Integer tId); }
创建对应的mapper文件
<?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="cn.bdqn.dao.StudentDao"> <resultMap type="Student" id="studentMap"> <id property="sId" column="sid"/> <result property="sName" column="sname"/> <!-- 设置关联集合的属性 --> <collection property="teachers" ofType="Teacher"> <id property="tId" column="tid"/> <result property="tName" column="tname"/> </collection> </resultMap> <!-- 根据学生的编号查询所有对应老师信息 --> <select id="selectTeahcerById" resultMap="studentMap"> select sid,sname,tid,tname from student,middle,teacher where sid=studentId and tid=teacherId and sid=#{xxx} </select> </mapper>
在mybatis.xml文件中管理mapper文件
<!-- 加载映射文件信息 --> <mappers> <mapper resource="cn/bdqn/dao/StudentMapper.xml" /> </mappers>
创建对应的测试类
public class TeacherTest { StudentDao dao; SqlSession session; @Before public void before() { // 因为需要关闭session 需要把session提取出去 session = SessionUtil.getSession(); dao = session.getMapper(StudentDao.class); } @After public void after() { if (session != null) { session.close(); } } /** * 根据老师的编号查询所有的导师信息 */ @Test public void test1() { Student student = dao.selectTeahcerById(1); System.out.println(student); } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/12142.html