Mybatis 一对多分页数据条数不正确处理方式

今天在进行一对多的left join处理的时候发现分页的数据量不正确,后来把打印的sql去数据库执行,查询出来的确实是10条
1在xml里进行更改collection的映射

<resultMap id="userMap" type="com.abc.entity.SysUser">
        <id property="uid" column="uid"/>
        <result property="uname" column="uname"/>
        <result property="nick" column="nick"/>
        <result property="pwd" column="pwd"/>
        <result property="salt" column="salt"/>
        <result property="lock" column="lock"/>
        <result property="created" column="created" jdbcType="TIMESTAMP"/>
        <result property="updated" column="updated" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 一对多:user、role -->
    <resultMap id="userIncludeRolesMap" extends="userMap" type="com.abc.entity.SysUser">
        <collection property="roleList" column="uid"  select="QueryUserRoles">
        </collection>
    </resultMap>

2.关联查询

 <select id="QueryUserRoles" resultType="com.abc.entity.SysRole">
        select
        r.rid as rid,
        r.rname as rname,
        r.rdesc as rdesc,
        r.rval as rval,
        r.created as created,
        r.updated as updated
        from  sys_user_role ur
        left join sys_role r on ur.role_id = r.rid
         where ur.user_id= #{uid}
    </select>

    <select id="selectUserIncludeRoles" resultMap="userIncludeRolesMap">
        select
        u.*
        from sys_user u
        <where>
            <if test="uname != null and uname != ''">
                u.`uname` like concat('%',#{uname},'%')
            </if>
        </where>
        order by u.uid
    </select>

主要就是把collection的映射数据拿一个子查询去映射,column就是从表要关联主表的那个字段

另:分页插件使用

Page page = PageHelper.startPage(xx,xx);
// todo:your query
PageInfo pageInfo = new PageInfo<>(page);

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值