1.Teacher.java
private List<Student> list;//添加list,查找老师的所有学生
2.TeacherMapper.xml
<mapper namespace="com.bjsxt.mapper.TeacherMapper">
<resultMap type="teacher" id="mymap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<collection property="list" column="id" select="com.bjsxt.mapper.StudentMapper.selByTid" ofType="student"></collection>
</resultMap>
</mapper>
<select id="" resultMap="mymap">
select * from teacher
</select>
</mapper>
3.StudentMapper.xml
<mapper namespace="com.bjsxt.StudentMapper">
<select id="selByTid" parameterType="int" resultType="student">
select * from student where tid=#{0}
</select>
</mapper>
使用<resultMap>实现加载集合数据(联合查询方式)
1.在TeacherMapper.xml中配置
<resultMap type="teacher" id="mymap">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="list" ofType="student"/>//List<student> list
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<result column="age" property="age"/>
<result column="tid" property="tid"/>
</collection>
<select id="selAll" resultMap="mymap">
select t.id tid,t.name tname,s.id sid.s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.id
</select>