【问题标题】:Collection in resultMap performance issue with mybatis使用 mybatis 在 resultMap 中收集性能问题
【发布时间】:2016-04-18 05:44:54
【问题描述】:

我的班级有列表,因此我在映射结果时使用了集合标签。这是一个示例代码:

<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 秒。有什么办法可以改善这一点吗?缓存不是选项,我被要求在一个响应中返回所有学生。

【问题讨论】:

    标签: sql performance collections mybatis


    【解决方案1】:

    这就是著名的 1+N 选择问题。你可以使用嵌套的结果图来解决它:

    <resultMap id="classroomMapper" type="ClassroomEntity" >
        <result property="classroomName" column="CLASSROOMNAME" />
        <result property="classroomCode" column="CLASSROOMCODE" />
        <collection property="studentList"  resultMap="studentMapper" />
    </resultMap>
    

    【讨论】:

    • 不错,但是为了正常运行,必须将classicCode属性添加为id标签而不是结果标签。这让 Mybatis 可以按预期对结果进行分组。
    猜你喜欢
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2018-12-07
    • 2012-11-10
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多