mybatis复杂查询
关联的嵌套select查询
<select id="getStudentList" resultMap="studentTeacher">
select * from school.student
</select>
<resultMap id="studentTeacher" type="Student">
<result property="id" column="id"></result>
<result property="studentName" column="studentName"></result>
<result property="password" column="password"></result>
<result property="teacherId" column="teacherId"></result>
<!-- 复杂的属性
对象:association
集合:collection
-->
<association property="teachers" column="teacherId" javaType="Teacher" select="getTeacherById"/>
</resultMap>
关联的嵌套结果映射
<select id="getStudentList2" resultMap="studentResult">
select student.id sid,student.studentName sname,student.teacherId tid,teacher.teacherName tname from student,teacher where student.teacherId=teacher.id
</select>
<resultMap id="studentResult" type="Student">
<result property="id" column="sid"></result>
<result property="studentName" column="sname"></result>
<result property="teacherId" column="tid"></result>
<association property="teachers" column="tid" javaType="Teacher" resultMap="teacherResult"/>
</resultMap>
<resultMap id="teacherResult" type="Teacher">
<result property="teacherName" column="tname"/>
</resultMap>
以上需要注意的是property
属性的值应是实体类的属性名一致
如果属性是对象,则使用assocation
,如果属性的对象集合,则使用collection