MyBatis limit分页设置

 

错误的写法:

  1. <select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">

  2. SELECT

  3. a.*,

  4. FROM

  5. tb_user a

  6. WHERE 1=1

  7. <if test="ids != null and ids.size()!=0">

  8. AND a.id IN

  9. <foreach collection="ids" item="id" index="index"

  10. open="(" close=")" separator=",">

  11. #{id}

  12. </foreach>

  13. </if>

  14. <if test="statusList != null and statusList.size()!=0">

  15. AND a.status IN

  16. <foreach collection="statusList" item="status" index="index"

  17. open="(" close=")" separator=",">

  18. #{status}

  19. </foreach>

  20. </if>

  21. ORDER BY a.create_time desc

  22. LIMIT (#{pageNo}-1)*#{pageSize},#{pageSize}; // 错误

  23. </select>

在MyBatis中LIMIT之后的语句不允许的变量不允许进行算数运算,会报错。

正确的写法一:

  1. <select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">

  2. SELECT

  3. a.*,

  4. FROM

  5. tb_user a

  6. WHERE 1=1

  7. <if test="ids != null and ids.size()!=0">

  8. AND a.id IN

  9. <foreach collection="ids" item="id" index="index"

  10. open="(" close=")" separator=",">

  11. #{id}

  12. </foreach>

  13. </if>

  14. <if test="statusList != null and statusList.size()!=0">

  15. AND a.status IN

  16. <foreach collection="statusList" item="status" index="index"

  17. open="(" close=")" separator=",">

  18. #{status}

  19. </foreach>

  20. </if>

  21. ORDER BY a.create_time desc

  22. LIMIT ${(pageNo-1)*pageSize},${pageSize}; (正确)

  23. </select>

正确的写法二:(推荐)

  1. <select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">

  2. SELECT

  3. a.*,

  4. FROM

  5. tb_user a

  6. WHERE 1=1

  7. <if test="ids != null and ids.size()!=0">

  8. AND a.id IN

  9. <foreach collection="ids" item="id" index="index"

  10. open="(" close=")" separator=",">

  11. #{id}

  12. </foreach>

  13. </if>

  14. <if test="statusList != null and statusList.size()!=0">

  15. AND a.status IN

  16. <foreach collection="statusList" item="status" index="index"

  17. open="(" close=")" separator=",">

  18. #{status}

  19. </foreach>

  20. </if>

  21. ORDER BY a.create_time desc

  22. LIMIT #{offSet},#{limit}; (推荐,代码层可控)

  23. </select>

分析:方法二的写法,需要再请求参数中额外设置两个get函数,如下:

  1. @Data

  2. public class QueryParameterVO {

  3.  
  4. private List<String> ids;

  5.  
  6. private List<Integer> statusList;

  7.  
  8. // 前端传入的页码

  9. private int pageNo; // 从1开始

  10.  
  11. // 每页的条数

  12. private int pageSize;

  13.  
  14. // 数据库的偏移

  15. private int offSet;

  16.  
  17. // 数据库的大小限制

  18. private int limit;

  19.  
  20. // 这里重写offSet和limit的get方法

  21. public int getOffSet() {

  22. return (pageNo-1)*pageSize;

  23. }

  24.  
  25. public int getLimit() {

  26. return pageSize;

  27. }

  28. }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值