1.多对多
在关系映射的"多对多"案例基础上,使用基于注解实现一个学生对应多个教师,一个教师也可以对应多个学生的多对多关联关系。
项目结构图如下:

1.在com.dao包下修改StudentsMapper和新建TeachersMapper
//StudentsMapper.java
package com.dao;
import com.entity.IdCard;
import com.entity.Students;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface StudentsMapper {
@Select("select * from students where sid=#{sid}")
@Results(@Result(column = "cid",property = "card",javaType = IdCard.class, one = @One(select = "com.dao.IdCardMapper.getIdCardById")))
public Students getStudentBySid(Integer sid);
@Select("select s.*,c.code from students s,idcard c where s.cid = c.id and s.sid=#{sid}")
@Results({
@Result(property = "card.id",column = "cid" ),
@Result(property = "card.code",column = "code")
})
public Students getStudentBySid2(Integer sid);
//根据班级编号查询学生集合
@Select("select * from students where class_room_id=#{classRoomId}")
public List<Students> getStudentsByClassRoomId(Integer classRoomId);
//根据学生编号查询带班级信息的学生资料
@Select("select s.*,c.code,cr.cname from students s,idcard c,classroom cr where s.cid = c.id and s.class_room_id=cr.cid and s.sid=#{sid}")
@Results({
@Result(property = "card.id",column = "cid" ),
@Result(property = "card.code",column = "code"),
@Result(property = "classRoom.cid",column = "class_room_id"),
@Result(property = "classRoom.cname",column = "cname")
})
public Students getStudentWithClassRoomBySid(Integer sid);
//根据学生编号查询带教师集合信息的学生信息
@Select("select * from students where sid = #{sid}")
@Results({
@Result(id=true,property = "sid",column = "sid"),
@Result(property = "teachers",column = "sid",many=@Many(select = "com.dao.TeachersMapper.queryTeachersFromStudentsAndTeachersBySid"))
})
public Students getStudentWithTeachersBySid(Integer sid);
//从中间表中根据教师编号查询学生集合
@Select("select * from students where sid in (select sid from students_teachers where tid=#{tid})")
public List<Students> getStudentsFromStudentsAndTeachersByTid(Integer tid);
}
//TeachersMapper.java
package com.dao;
import com.entity.Teachers;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface TeachersMapper {
//从中间表中根据学生编号查询教师集合
@Select("select * from teachers where tid in (select tid from students_teachers where sid=#{sid})")
public List<Teachers> queryTeachersFromStudentsAndTeachersBySid(Integer sid);
//根据教师编号查询带学生集合信息的教师信息
@Select("select * from teachers where tid = #{tid}")
@Results({
@Result(id=true,property = "tid",column = "tid"),
@Result(property = "stus",column = "tid",many=@Many(select = "com.dao.StudentsMapper.getStudentsFromStudentsAndTeachersByTid"))
})
public Teachers queryTeacherWithStudentsByTid(Integer tid);
}
2.在mybatis-config.xml添加映射接口。
<!--映射文件-->
<mappers>
<mapper class="com.dao.StudentsMapper"/>
<mapper class="com.dao.IdCardMapper"/>
<mapper class="com.dao.ClassRoomMapper"/>
<mapper class="com.dao.TeachersMapper"/>
</mappers>
3.编写测试类
//StudentsMapperTest.java
@Test
public void testQueryStudentWithTeachersBySid(){
SqlSession session = null;
Students s = null;
try {
session = SqlSessionFactoryUtil.openSqlSession();
StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class);
s = studentsMapper.getStudentWithTeachersBySid(2);
System.out.println(s);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
//TeachersMapperTest.java
@Test
public void testQueryTeachersWithStudentsByTid(){
SqlSession session = null;
try {
session = SqlSessionFactoryUtil.openSqlSession();
TeachersMapper teachersMapper = session.getMapper(TeachersMapper.class);
Teachers teacher= teachersMapper.queryTeacherWithStudentsByTid(1);
System.out.println(teacher);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}