比如:一个老师拥有多个学生!
对于老师而言,就是一对多的关系
- 环境搭建,和刚才一样
实体类:
public class Student {
private int id;
private String name;
private int tid;
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List<Student> students;
按嵌套查询
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--configuration核心配置文件-->
<mapper namespace="com.xiao.dao.TeacherMapper">
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname, t.name tname,t.id tid
from student s,teacher t
where s.tid = t.id and t.id = #{tid}
</select>
<resultMap id="TeacherStudent" type="Teacher" >
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
</mapper>
public interface TeacherMapper {
//获取老师下的所有学生及老师的信息
Teacher getTeacher(@Param("tid") int id);
}
1