MyBatis-plus分頁出現兩個limit?


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協議進行許可。轉載請署名作者且注明文章出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM