<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"/><resultproperty="name"column="tname"/><collectionproperty="students"ofType="Student"><resultproperty="id"column="sid"/><resultproperty="name"column="sname"/><resultproperty="tid"column="tid"/></collection></resultMap>
<selectid="getTeacher2"resultMap="TeacherStudent2">
select * from mybatis.teacher where id = #{tid}
</select><resultMapid="TeacherStudent2"type="Teacher"><collectionproperty="students"javaType="ArrayList"ofType="Student"select="getStudentByTeacherId"column="id"/></resultMap><selectid="getStudentByTeacherId"resultType="Student">
select * from mybatis.student where tid = #{tid}
</select>
多对一
<!-- 按照结果嵌套处理--><selectid="getStudent2"resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id;
</select><resultMapid="StudentTeacher2"type="Student"><resultproperty="id"column="sid"></result><resultproperty="name"column="sname"></result><associationproperty="teacher"javaType="Teacher"><resultproperty="name"column="tname"/></association></resultMap>
<selectid="getStudent"resultMap="StudentTeacher">
select * from student
</select><resultMapid="StudentTeacher"type="Student"><resultproperty="id"column="id"/><resultproperty="name"column="name"/><!-- association--><associationproperty="teacher"column="tid"javaType="Teacher"select="getTeacher"/></resultMap><selectid="getTeacher"resultType="Teacher">
select * from teacher where id=#{id}
</select>
一对多<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"> .