<!--按照结果嵌套处理--><selectid="getStudent2"resultMap="Student_Teacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid = t.id;
</select><!--在sql中起了别名,列应该就变为别名--><resultMapid="Student_Teacher2"type="Student"><resultproperty="id"column="sid"/><resultproperty="name"column="sname"/><!--复杂类型--><associationproperty="teacher"javaType="Teacher"><resultproperty="name"column="tname"/></association></resultMap>
<!--按照嵌套查询方式--><selectid="getTeacher2"resultMap="Teacher_student_2">
select * from teacher where id = #{tid}
</select><resultMapid="Teacher_student_2"type="teacher"><resultproperty="id"column="id"/><resultproperty="name"column="name"/><!--因为student类型为List,因此javaType="ArrayList",其中的具体类型为"Student"--><collectionproperty="students"column="id"javaType="ArrayList"ofType="Student"select="getStudent"/></resultMap><selectid="getStudent"resultType="Student">
select * from student where tid = #{id}
</select>
根据结果嵌套查询
TeacherMapper.xml
<!--按照结果嵌套查询--><selectid="getTeacher"resultMap="Teacher_Student">
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="Teacher_Student"type="teacher"><resultproperty="id"column="tid"/><resultproperty="name"column="tname"/><!--javaType ="" 执行属性的类型 集合中的泛型信息,使用ofType获取--><collectionproperty="students"ofType="Student"><resultproperty="id"column="sid"/><resultproperty="name"column="sname"/><resultproperty="tid"column="tid"/></collection></resultMap>