多次查询,非联合查询版本
<resultMap type="teacher" id="techMap"> <id column="id" property="id"/> <result column="name" property="name"/> <collection property="list" column="id" ofType="student" select="com.xxx.mapper.StudentMapper.selByTid"> </collection> </resultMap> <select id="selAll" resultMap="techMap"> select * from teacher </select>
联合查询版本
<resultMap type="teacher" id="mymap1"> <id column="tid" property="id"/> <result column="tname" property="name"/> <collection property="list" ofType="student" > <id column="sid" property="id"/> <result column="sname" property="name"/> <result column="age" property="age"/> <result column="tid" property="tid"/> </collection> </resultMap> <select id="selAll1" resultMap="mymap1"> select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid; </select>