使用mapper select中的collection 但是需要分页的做法

来源:互联网 发布:js 后退返回网址 编辑:程序博客网 时间:2024/06/06 02:39

当你的一个实体类中有一个集合属性的时候,你需要用到mapper中的collection标签,但是假如你恰巧又需要分页的时候时候,你使用了pagehelper这个的插件的时候,这个就没办法实现你的需求,你将集合属性满足的时候,你去看查询的SQL语句你就会发现,你查询的SQL条数和你得到实体数应该是对应不上的,pagehelper实际上是对SQL查询语句的限制,而不是对实体类进行限制,这个时候,一次SQL是现实分页已经满足不来了, collection中还有一个标签的select 这个 select实际上满足的是集合中的实体的自动关联查询 ,这个时候,就是N+1次的查询 ,n就是你的size的大小,限制好size大小,基本就不会出现性能上的问题。

<resultMap id="detectRecord" type="DetectRecord">    <id column="id" property="id"/>    ....    <collection property="values" column="specimen_id" select="getDetectValues"/></resultMap><resultMap id="detectValue" type="DetectValue">    <id column="id" property="id"/>
    .....</resultMap>
<select id="getDetectRecords" resultMap="detectRecord">    SELECT           FROM       </select><select id="getDetectValues" parameterType="String" resultMap="detectValue">    select            from           where        </select>