我的班级中有列表,因此我在映射结果时使用了集合标记。这是一个示例代码:
<select id="retrieveClassRoomsWithStudents" resultMap="classroomMapper">
SELECT CLS.CLASSROOMCODE, CLS.CLASSROOMNAME
FROM TMP.CLASSROOM CLS
ORDER BY CLS.CLASSROOMNAME
</select>
<select id="retrieveStudents" resultMap="studentMapper" parameterType="Integer" >
SELECT STD.CLASSROOMCODE, STD.STUDENTNUMBER, STD.STUDENTNAME
FROM TMP.STUDENTS STD
WHERE STD.CLASSROOMCODE = #{CLASSROOMCODE}
ORDER BY STD.STUDENTNUMBER
</select>
<resultMap id="classroomMapper" type="ClassroomEntity" >
<result property="classroomName" column="CLASSROOMNAME" />
<result property="classroomCode" column="CLASSROOMCODE" />
<collection property="studentList" column="CLASSROOMCODE" javaType="ArrayList" ofType="StudentEntity" select="retrieveStudents" />
</resultMap>
<resultMap id="studentMapper" type="StudentEntity" >
<result property="classroomCode" column="CLASSROOMCODE"/>
<result property="studentNumber" column="STUDENTNUMBER"/>
<result property="studentName" column="STUDENTNAME"/>
</resultMap>
我有大约200个班级和10,000名学生。 “retrieveClassRoomsWithStudents”方法运行20秒。我尝试过左外连接以减少查询次数。单个查询在70毫秒内运行,但结果映射再次需要大约20秒。有没有办法改善这个?缓存是不可取的,我被要求在一个回复中返回所有学生。
答案 0 :(得分:1)
这是着名的1 + N选择问题。您可以使用嵌套的resultmap来解决它:
<resultMap id="classroomMapper" type="ClassroomEntity" >
<result property="classroomName" column="CLASSROOMNAME" />
<result property="classroomCode" column="CLASSROOMCODE" />
<collection property="studentList" resultMap="studentMapper" />
</resultMap>