错误的写法:

<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">
SELECT
a.*,
FROM
tb_user a
WHERE =
<if test="ids != null and ids.size()!=0">
AND a.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="statusList != null and statusList.size()!=0">
AND a.status IN
<foreach collection="statusList" item="status" index="index"
open="(" close=")" separator=",">
#{status}
</foreach>
</if>
ORDER BY a.create_time desc
LIMIT (#{pageNo}-)*#{pageSize},#{pageSize}; // 错误
</select>
在MyBatis中LIMIT之后的语句不允许的变量不允许进行算数运算,会报错。
正确的写法一:
<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">
SELECT
a.*,
FROM
tb_user a
WHERE =
<if test="ids != null and ids.size()!=0">
AND a.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="statusList != null and statusList.size()!=0">
AND a.status IN
<foreach collection="statusList" item="status" index="index"
open="(" close=")" separator=",">
#{status}
</foreach>
</if>
ORDER BY a.create_time desc
LIMIT ${(pageNo-)*pageSize},${pageSize}; (正确)
</select>
正确的写法二:(推荐)
<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">
SELECT
a.*,
FROM
tb_user a
WHERE =
<if test="ids != null and ids.size()!=0">
AND a.id IN
<foreach collection="ids" item="id" index="index"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
<if test="statusList != null and statusList.size()!=0">
AND a.status IN
<foreach collection="statusList" item="status" index="index"
open="(" close=")" separator=",">
#{status}
</foreach>
</if>
ORDER BY a.create_time desc
LIMIT #{offSet},#{limit}; (推荐,代码层可控)
</select>
分析:方法二的写法,需要再请求参数中额外设置两个get函数,如下:
@Data
public class QueryParameterVO { private List<String> ids; private List<Integer> statusList; // 前端传入的页码
private int pageNo; // 从1开始 // 每页的条数
private int pageSize; // 数据库的偏移
private int offSet; // 数据库的大小限制
private int limit; // 这里重写offSet和limit的get方法
public int getOffSet() {
return (pageNo-)*pageSize;
} public int getLimit() {
return pageSize;
}
}

错误的写法:
<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">    SELECT    a.*,    FROM    tb_user a    WHERE 1=1    <if test="ids != null and ids.size()!=0">        AND a.id IN        <foreach collection="ids" item="id" index="index"                 open="(" close=")" separator=",">            #{id}        </foreach>    </if>    <if test="statusList != null and statusList.size()!=0">        AND a.status IN        <foreach collection="statusList" item="status" index="index"                 open="(" close=")" separator=",">            #{status}        </foreach>    </if>    ORDER BY a.create_time desc    LIMIT (#{pageNo}-1)*#{pageSize},#{pageSize};  // 错误</select>在MyBatis中LIMIT之后的语句不允许的变量不允许进行算数运算,会报错。正确的写法一:<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">    SELECT    a.*,    FROM    tb_user a    WHERE 1=1    <if test="ids != null and ids.size()!=0">        AND a.id IN        <foreach collection="ids" item="id" index="index"                 open="(" close=")" separator=",">            #{id}        </foreach>    </if>    <if test="statusList != null and statusList.size()!=0">        AND a.status IN        <foreach collection="statusList" item="status" index="index"                 open="(" close=")" separator=",">            #{status}        </foreach>    </if>    ORDER BY a.create_time desc    LIMIT ${(pageNo-1)*pageSize},${pageSize}; (正确)</select>正确的写法二:(推荐)<select id="queryMyApplicationRecord" parameterType="MyApplicationRequest" resultMap="myApplicationMap">    SELECT    a.*,    FROM    tb_user a    WHERE 1=1    <if test="ids != null and ids.size()!=0">        AND a.id IN        <foreach collection="ids" item="id" index="index"                 open="(" close=")" separator=",">            #{id}        </foreach>    </if>    <if test="statusList != null and statusList.size()!=0">        AND a.status IN        <foreach collection="statusList" item="status" index="index"                 open="(" close=")" separator=",">            #{status}        </foreach>    </if>    ORDER BY a.create_time desc    LIMIT #{offSet},#{limit}; (推荐,代码层可控)</select>分析:方法二的写法,需要再请求参数中额外设置两个get函数,如下:@Datapublic class QueryParameterVO {     private List<String> ids;     private List<Integer> statusList;     // 前端传入的页码    private int pageNo;  // 从1开始     // 每页的条数    private int pageSize;     // 数据库的偏移    private int offSet;     // 数据库的大小限制    private int limit;     // 这里重写offSet和limit的get方法    public int getOffSet() {        return (pageNo-1)*pageSize;    }     public int getLimit() {        return pageSize;    }}
--------------------- 作者:timchen525 来源:CSDN 原文:https://blog.csdn.net/timchen525/article/details/79647666 版权声明:本文为博主原创文章,转载请附上博文链接!

MyBatis limit分页设置的更多相关文章

  1. Mybatis的ResultMap与limit分页查询

    ResultMap主要解决的是:属性名和字段不一致 如果在pojo中设置的是一个名字,在数据库上又是另一个名字,那么查询出来的结果或者其他操作的结果就为null. //在pojo中 private S ...

  2. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  3. Mybatis 的分页插件PageHelper-4.1.1的使用

    Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschin ...

  4. 权限管理系统之项目框架搭建并集成日志、mybatis和分页

    前一篇博客中使用LayUI实现了列表页面和编辑页面的显示交互,但列表页面table渲染的数据是固定数据,本篇博客主要是将固定数据变成数据库数据. 一.项目框架 首先要解决的是项目框架问题,搭建什么样的 ...

  5. Mybatis3详解(十四)----Mybatis的分页

    1.前言 在前面学习mybatis的时候,会经常对数据进行增删改查操作,使用最多的是对数据库进行查询操作,但是前面都是简单的案例,所以查询的数据量不是很大,自然查询时没有任何压力,但是如果在实际的项目 ...

  6. spring boot(二)整合mybatis plus+ 分页插件 + 代码生成

    先创建spring boot项目,不知道怎么创建项目的 可以看我上一篇文章 用到的环境 JDK8 .maven.lombok.mysql 5.7 swagger 是为了方便接口测试 一.Spring ...

  7. mybatis自定义分页拦截器

    最近看了一下项目中代码,发现系统中使用的mybatis分页使用的是mybatis自带的分页,即使用RowBounds来进行分页,而这种分页是基于内存分页,即一次查出所有的数据,然后再返回分页需要的数据 ...

  8. mybatis 实现分页和过滤模糊查询

    基于 mybatis 的分页和过滤查询 学习内容: 分页设计 1.分页需传递的参数 2.分页需展示的数据 3.分页需展示的数据的来源 3.1.结果总数与结果集(分页原理) 3.2.总页数.上一页和下一 ...

  9. 理解 Mybatis的分页插件 PageHelper

    Mybatis + SpringMVC + Maven实现分页查询 (推荐采用的插件是PageHelper) 先看一下之前的这篇博客,这里推荐了 Mybatis 的分页方法. 按照上面的方法设置后,确 ...

随机推荐

  1. MDX Cookbook 06 - GENERATE 循环遍历

    有时候需要从集合中取出特定的成员但是又不能执行遍历操作,这个时候就可以使用 GENERATE 函数来解决这个问题. 根据地区查询每年的销售额 - SELECT NON EMPTY { , NON EM ...

  2. PL/SQL学习笔记之触发器

    一:触发器响应的事件 数据库操作(DML)语句(DELETE,INSERT,UPDATE) 数据库定义(DDL)语句(CREATE,ALTER或DROP) 数据库操作(SERVERERROR,登录,注 ...

  3. (原)MobileNetV1

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9410540.html 论文: MobileNets: Efficient Convolutional ...

  4. 编写SHELL脚本--编写简单脚本

    1.简单脚本文件hello.sh,内容如下 #!/bin/bash pwd ls -al 执行脚本:bash hello.sh  或者使用root命令:  ./hello.sh 2.接受用户参数 $0 ...

  5. spring boot+ Intellj idea devtools 设置热部署

    POM文件 <!--添加依赖--> <dependency> <groupId>org.springframework.boot</groupId> & ...

  6. MAC EI Capitan上更新系统自带SVN版本号(关闭SIP方能sudo rm)

    继昨晚之后.决定更新系统自带的svn.自带的svn版本号是1.7.看官网svn:http://www.wandisco.com/subversion/download#osx 最新版本号是1.9.13 ...

  7. 蓝牙发现服务UUID(service UUID)

    出至<蓝牙标准Core_V4.0>2.5.1 uuid(1576页) 其中 Bluetooth_Base_UUID定义为 00000000-0000-1000-8000-00805F9B3 ...

  8. [svc]find+xargs/sed&sed后向引用+awk多匹配符+过滤行绝招总结&&产生随机数

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz find,文件+超过7天+超过1M的+按日期为文 ...

  9. sublime Text 几款插件

    留着自已以后用:http://blog.csdn.net/nivana999/article/details/7823805 1.sublime text实现vim命令格式(Vintage插件是自带的 ...

  10. 菜鸟学Java(二十三)——Java内存分析

    我们常说的Java内存主要分为四大块(寄存器不在考虑之内,我们无法用代码来操控它):stack(栈).heap(堆).data segment(数据区).code segment(代码区).它们的主要 ...