MyBatis-plus分页出现两个limit?

12 篇文章 0 订阅

1、问题出路

测试环境、线上环境日志陆续出现此类问题,本地环境并没有出现此类异常,而且服务器中也不是持续报错,而是时而报错,时而正常,报错如下:

 
2021-12-01 12:03:02.909 ERROR 28056 --- [nio-8000-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 20' at line 6
### The error may exist in file [G:\Git\xf\service-xf\target\classes\com\lm\anga\platform\anslip\dao\xml\AnSlip.map.xml]
### The error may involve com.lm.anga.platform.anslip.dao.mapper.AnSlipMapper.getLastData-Inline
### The error occurred while setting parameters
### SQL: SELECT     id_,station_id_,collect_time_,an_slip_      FROM dt_an_slip_data   WHERE   station_id_=? order by collect_time_ desc limit 1 LIMIT ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 20' at line 6
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 20' at line 6] with root cause
 
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 20' at line 6
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_40]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_40]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_40]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_40]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.21.jar:na]
SQL语句
 SELECT     id_,station_id_,collect_time_,an_slip_      FROM dt_an_slip_data   WHERE   station_id_=? order by collect_time_ desc limit 1 LIMIT ?

原因

PageHelper 方法使用了静态的 ThreadLocal 参数,分页参数和线程是绑定的。

只要你可以保证在 PageHelper 方法调用后紧跟 MyBatis 查询方法,这就是安全的。因为 PageHelper 在 finally 代码段中自动清除了 ThreadLocal 存储的对象。

线程中start的page 不能保证线程在当前执行退出时清理完page变量

PageBean pageBean=queryFilter.getPageBean();
PageHelper.startPage(pageBean.getCurrentPage(),pageBean.getPageSize());

解决

使用PageHelper插件后,清楚缓存

	PageHelper.clearPage();

建议

MyBatis-plus分页插件和PageHelper插件二选一

@Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 开启 PageHelper 的支持
        paginationInterceptor.setLocalPage(true);//如果使用MyBatis-plus分页插件时,可设置为false
        List<ISqlParser> sqlParserList = new ArrayList<>();
        paginationInterceptor.setSqlParserList(sqlParserList);
        return paginationInterceptor;
    }

本文作者:好名字
原文链接:http://www.cuizb.top/myblog/article/1638714725
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 3.0 CN协议进行许可。转载请署名作者且注明文章出处。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mybatis-Plus 是一个 Mybatis 的插件,提供了很多增强功能,其中包括分页查询。Mybatis-Plus分页查询使用起来非常简单,只需要引入相关依赖,然后在查询方法中使用 Page 对象即可。 具体步骤如下: 1. 引入 Mybatis-Plus分页插件依赖,例如: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> ``` 2. 在查询方法中创建一个 Page 对象,并且将当前页和每页显示条数设置好,例如: ``` Page<User> page = new Page<>(1, 10); ``` 3. 调用 Mybatis-Plus 提供的分页查询方法,例如: ``` IPage<User> userPage = userMapper.selectPage(page, new QueryWrapper<User>().lambda().eq(User::getAge, 18)); ``` 其中,userMapper 是 Mybatis-Plus 自动生成的 Mapper 接口,selectPageMybatis-Plus 提供的分页查询方法,第一个参数是 Page 对象,第二个参数是查询条件。 4. 最后,可以通过 userPage 对象获取分页查询的结果,例如: ``` List<User> userList = userPage.getRecords(); long total = userPage.getTotal(); ``` 其中,getRecords 方法返回当前页的数据列表,getTotal 方法返回总记录数。 这就是 Mybatis-Plus分页查询的基本用法。需要注意的是,Mybatis-Plus 默认使用的是物理分页,也就是先查询出所有符合条件的记录,然后再根据分页参数返回对应的数据,这种方式虽然能够实现分页,但是对于数据量非常大的情况下,会影响查询性能。因此,建议在需要分页的情况下,使用逻辑分页,也就是在查询条件中加上分页相关的参数,例如 limit offset。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java技术债务

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值