mybatis中的多对多的查询

数据库设计如下:

老师数据表:

学生数据表:

第三方表:

dao层接口:

/**
* 多对多
* @param tid
* @return
*/
public Teacher getStudentByTeacherId(int tid);


xml文件:





测试类:
/**
* 多对多
*/
@Test
public void getStudentByTeacherId(){
SqlSession session = MyBatisUtil.getSession();
ITeacherDAO mapper = session.getMapper(ITeacherDAO.class);
Teacher teacher = mapper.getStudentByTeacherId(1);
System.out.println("老师名字:"+teacher.getTname());
for (Student item :teacher.getStudents()){
System.out.println("学生名字:"+item.getName());
}
}


效果如下:


 
 
原文地址:https://www.cnblogs.com/sujulin/p/7677592.html