18 <!-- 19 方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集 20 封装联表查询的数据(去除重复的数据) 21 select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1 22 --> 23 <select id="getClass" parameterType="int" resultMap="ClassResultMap"> 24 select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id} 25 </select> 26 <!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> 27 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap"> 28 <id property="id" column="c_id"/> 29 <result property="name" column="c_name"/> 30 <association property="teacher" javaType="me.gacl.domain.Teacher"> 31 <id property="id" column="t_id"/> 32 <result property="name" column="t_name"/> 33 </association> 34 </resultMap> 35 36 <!-- 37 方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型 38 SELECT * FROM class WHERE c_id=1; 39 SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的teacher_id的值 40 --> 41 <select id="getClass2" parameterType="int" resultMap="ClassResultMap2"> 42 select * from class where c_id=#{id} 43 </select> 44 <!-- 使用resultMap映射实体类和字段之间的一一对应关系 --> 45 <resultMap type="me.gacl.domain.Classes" id="ClassResultMap2"> 46 <id property="id" column="c_id"/> 47 <result property="name" column="c_name"/> 48 <association property="teacher" column="teacher_id" select="getTeacher"/> 49 </resultMap> 50 51 <select id="getTeacher" parameterType="int" resultType="me.gacl.domain.Teacher"> 52 SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id} 53 </select>