解决MyBatis一对多分页查询不匹配条数的问题

在开发过程中,写mybatis后台分页时(不会后台分页的可以参考我的这篇博客https://blog.csdn.net/geng2568/article/details/88042834,),要求展示主表信息加从表信息实现分页,并且主表和从表的关系是一对多,这样在写后台分页过程中,每页显示的信息总会少于要求显示的信息,这是因为在一对多左关联查询时,从表中有多少个和主表对应的数据,查询结果中就有几条主表信息和从表对应。总之:

 原先的sql是:

<select id="getServerInfoPage" resultMap="BaseResultMap" parameterType="java.util.Map" >
    SELECT
    sm.*,vm.id vmid,vm.v_user_name vusername,vm.password vmpassword,vm.v_big_internet,
    vm.big_mask_code,vm.big_gateway,vm.small_mask_code,
    vm.small_internet,vm.use_person, vm.operate_system,
    vm.application vmapplication,vm.remark vmremark
    FROM server_manager sm
    LEFT JOIN
    virtual_machine_info vm
    ON
    sm.id = vm.server_id
    <where>
      <if test="serverBrand != null and serverBrand != ''" >
        and sm.server_brand like concat('%',#{serverBrand,jdbcType=VARCHAR},'%')
      </if>
      <if test="serverType != null and serverType != ''" >
        and sm.server_type like concat('%',#{serverType,jdbcType=VARCHAR},'%')
      </if>
      <if test="serverPosition != null and serverPosition != ''" >
        and sm.server_position like concat('%',#{serverPosition,jdbcType=VARCHAR},'%')
      </if>
      <if test="userName != null and userName != ''" >
        and sm.user_name = #{userName,jdbcType=VARCHAR}
      </if>
      <if test="password != null and password != ''" >
        and sm.password = #{password,jdbcType=VARCHAR}
      </if>
    </where>
        ORDER BY id asc
        LIMIT #{pageStart},#{pageSize}
    </select>

改正之后的sql: 

<select id="getServerInfoPage" resultMap="BaseResultMap" parameterType="java.util.Map" >
    SELECT
    sm.*,vm.id vmid,vm.v_user_name vusername,vm.password vmpassword,vm.v_big_internet,
    vm.big_mask_code,vm.big_gateway,vm.small_mask_code,
    vm.small_internet,vm.use_person, vm.operate_system,
    vm.application vmapplication,vm.remark vmremark
    FROM server_manager sm
    LEFT JOIN
    virtual_machine_info vm
    ON
    sm.id = vm.server_id
    <where>
      sm.id IN (SELECT test.id from (SELECT id FROM server_manager  LIMIT #{pageStart},#{pageSize}) AS test)
      <if test="serverBrand != null and serverBrand != ''" >
        and sm.server_brand like concat('%',#{serverBrand,jdbcType=VARCHAR},'%')
      </if>
      <if test="serverType != null and serverType != ''" >
        and sm.server_type like concat('%',#{serverType,jdbcType=VARCHAR},'%')
      </if>
      <if test="serverPosition != null and serverPosition != ''" >
        and sm.server_position like concat('%',#{serverPosition,jdbcType=VARCHAR},'%')
      </if>
      <if test="userName != null and userName != ''" >
        and sm.user_name = #{userName,jdbcType=VARCHAR}
      </if>
      <if test="password != null and password != ''" >
        and sm.password = #{password,jdbcType=VARCHAR}
      </if>
      <if test="hardpan != null and hardpan !=''" >
        and sm.hardpan = #{hardpan,jdbcType=VARCHAR}
      </if>
      <if test="memory != null and memory != ''" >
        and sm.memory = #{memory,jdbcType=VARCHAR}
      </if>
      <if test="cpu != null and cpu!=''" >
        and sm.cpu = #{cpu,jdbcType=VARCHAR}
      </if>
      <if test="bigInternet != null and bigInternet !=''" >
        and sm.big_internet like concat('%',#{bigInternet,jdbcType=VARCHAR},'%')
      </if>
      <if test="maskCode != null and maskCode !=''" >
        and sm.mask_code like concat('%',#{maskCode,jdbcType=VARCHAR},'%')
      </if>
     </where>
        ORDER BY id asc
     </select>

总之:基本sql需要加强锻炼,以后再遇到这种情况就能够得心应手了,希望可以帮助遇到类似错误的小伙伴,加油!!!! 

 

 

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值