经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MyBatis » 查看文章
MyBatis limit分页设置的实现
来源:jb51  时间:2021/4/6 9:27:25  对本文有异议

错误的写法:

  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>
  24.  

 在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. private List<String> ids;
  4. private List<Integer> statusList;
  5. // 前端传入的页码
  6. private int pageNo; // 从1开始
  7. // 每页的条数
  8. private int pageSize;
  9. // 数据库的偏移
  10. private int offSet;
  11. // 数据库的大小限制
  12. private int limit;
  13. // 这里重写offSet和limit的get方法
  14. public int getOffSet() {
  15. return (pageNo-1)*pageSize;
  16. }
  17. public int getLimit() {
  18. return pageSize;
  19. }
  20. }

到此这篇关于MyBatis limit分页设置的实现的文章就介绍到这了,更多相关MyBatis limit分页内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号