一、多对一处理(association)
1、
<mapper namespace="dao.StudentMapper">
<select id="getStudent" resultMap="studentAndclass">
select c.no,s.classid,s.NAME from tbstudent as s,tbclass as c where c.NO=s.CLASSID
</select>
<resultMap id="studentAndclass" type="pojo.Student">
<result property="no" column="no"/>
<result property="classid" column="classid"/>
<result property="name" column="name"/>
<association property="tbClass" javaType="pojo.TbClass">
<result property="no" column="no"/>
</association>
</resultMap>
2、
<mapper namespace="dao.StudentMapper">
<select id="getStudent" resultMap="studentAndclass">
select CLASSID from tbstudent
</select>
<resultMap id="studentAndclass" type="pojo.Student">
<result property="no" column="no"/>
<result property="classid" column="classid"/>
<result property="name" column="name"/>
<association property="tbClass" column="classid" javaType="pojo.TbClass" select="getTbClass"/>
</resultMap>
<select id="getTbClass" resultType="pojo.TbClass">
select no from tbclass where NO=#{no}
</select>
</mapper>
二、一对多处理(collection)
1、
<mapper namespace="dao.TbClassMapper">
<select id="getTbClass2" resultMap="getTbClassAndStudent" resultType="pojo.TbClass" >
select s.classid,c.no,s.name,c.TID from tbclass as c,tbstudent as s where s.CLASSID=c.NO and c.no=#{no}
</select>
<resultMap id="getTbClassAndStudent" type="pojo.TbClass">
<result property="no" column="no"/>
<result property="name" column="name"/>
<result property="tid" column="tid"/>
<collection property="student" column="classid" ofType="pojo.Student">
<result property="classid" column="classid"/>
<result property="name" column="name"/>
<result property="hometown" column="hometown"/>
</collection>
</resultMap>
</mapper>