多对一映射
多个学生对映一个老师
<!--多表查询-->
<select id="getAllStudent" resultMap="getAllStudent">
select sid,sname,t.tid,name from student s,teacher t
where s.tid = t.tid
</select>
<!--处理结果集-->
<resultMap id="getAllStudent" type="student">
<id property="sid" column="sid"></id>
<result property="sname" column="sname"></result>
<!--处理老师类型-->
<association property="teacher" javaType="teacher">
<id property="tid" column="tid"></id>
<result property="name" column="name"></result>
</association>
</resultMap>
测试结果:
一对多映射
select t.tid,name,sid,sname from student s,teacher t
where s.tid = t.tid and t.tid = #{tid}
<resultMap id="getTeacher" type="teacher">
<id property="tid" column="tid"></id>
<result property="name" column="name"></result>
<!-- 集合对应的泛型用oftype-->
<collection property="students" ofType="student">
<id column="sid" property="sid"></id>
<result column="sname" property="sname"></result>
</collection>
</resultMap>
测试结果: