1.pageHelper遇到resultMap的collection后,分页总数不对
<resultMap id="orders" type="OrderDto">
<id property="id" column="id"/>
<result property="number" column="onumber"/>
<association property="address" javaType="AddressDto">
<id property="id" column="aid"/>
<result property="name" column="aname"/>
<result property="address" column="address"/>
<result property="remark" column="aremark"/>
</association>
<association property="activity" javaType="ActivityDto">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
</association>
<collection property="productRels" ofType="OrderProductRelDto">
<id property="id" column="rid"/>
<result property="qty" column="qty"/>
</collection>
</resultMap>
由于PageHelper分页总数是根据count(0)来计算出来的(即自动生成一条SQL select count(0) from xx where 筛选条件),而collection会把结果集合并(一对多的情况),计算总数在合并结果集之前,所以计算的总数不对
2.pageHelper遇到resultMap的collection后,分页结果集不对
同计数的原理一样,分页实际是在合并结果集之前进行的(即直接在查询SQL语句后面加了limit x,x),这就导致了存在一对多情况时,实际返回的结果集个数与分页要求的每页结果集个数不一致,且如果最后一条记录也涉及一对多时,结果集内容也不一定准确
综上:需要使用pageHelper分页时,resultMap不要用嵌套结果的方式,可以使用嵌套查询的方式;
(嵌套查询示例如下)
<resultMap id="BaseRoleResultMap" type="cn.com.hellowood.springsecurity.model.RoleModel">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="is_active" property="isActive" jdbcType="BOOLEAN"/>
<result column="description" property="description" jdbcType="VARCHAR"/>
<result column="last_update_time" property="lastUpdateTime" jdbcType="TIMESTAMP"/>
<collection property="menus" ofType="cn.com.hellowood.springsecurity.model.menus"
javaType="java.util.ArrayList" select="getMenus" column="id">
</collection>
</resultMap>
参考自:
https://blog.youkuaiyun.com/qq_33315102/article/details/80572566