<selectid="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><resultMapid="TeacherStudent"type="Teacher"><resultproperty="id"column="tid"></result><resultproperty="name"column="tname"></result><collectionproperty="students"ofType="Student"><resultproperty="id"column="sid"></result><resultproperty="name"column="sname"></result><resultproperty="tid"column="tid"></result></collection></resultMap>
按照查询嵌套处理
<selectid="getTeacher2"resultMap="TeacherStudent2">
select * from teacher where id = #{tid}
</select><resultMapid="TeacherStudent2"type="Teacher"><collectionproperty="students"javaType="ArrayList"ofType="Student"select="getStudentByTeacherId"column="id"></collection></resultMap><selectid="getStudentByTeacherId"resultType="Student">
select * from student where tid = #{tid}
</select>
环境搭建@Datapublic class Student { private int id; private String name; //学生需要关联一个老师 private int tid;}@Datapublic class Teacher { private int id; private String name; //...