Mybatis#PageHepler分页插件的使用不当导致出现问题记录
事情经过
之前的项目是没有引入PageHepler
组件的,这次引入了,出现了一些查询接口莫名奇妙的加上了limit参数,代码中也并没有PageHelper.startPage()
方法去声明,线上出现了部分查询接口出现SQL报错
解决过程
样例代码
mapper代码:
<select id="queryMessageList"
parameterType="runstatic.cloudexamples.cloudwebexample.entity.QueryMessageListParam"
resultType="runstatic.cloudexamples.cloudwebexample.entity.Message">
select * from message
limit #{pageNum}, #{pageSize}
</select>
请求参数代码:
@Data
public class QueryMessageListParam {
private Integer pageNum;
private Integer pageSize;
}
初步怀疑
怀疑有的接口或者方法没有及时清理掉PageHepler的Page共享变量,在出现报错的接口上尝试手动调用 PageHelper.clearPage();
, 结果并没有什么效果,还是有莫名奇妙的limit拼接上了SQL上。
### Error querying database. Cause: java.sql.SQLSyntaxErrorException: 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 10' at line 3
### The error may exist in file [C:\Users\chenmoand\IdeaProjects\cloud-examples\cloud-web-example\target\classes\mapper\MessageMapper.xml]
### The error may involve runstatic.cloudexamples.cloudwebexample.mapper.MessageMapper.queryMessageList-Inline
### The error occurred while setting parameters
### SQL: select * from message limit ?, ? LIMIT ?
### Cause: java.sql.SQLSyntaxErrorException: 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 10' at line 3
; bad SQL grammar []] with root cause
java.sql.SQLSyntaxErrorException: 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 10' at line 3
继续排查
发现报错的接口都有一个共同点,那就是接口参数上都定义了这俩参数名
private Integer pageNum;
private Integer pageSize;
然后检查了一下PageHepler
的官方文档
发现原因是supportMethodsArguments
参数, 起初以为这个参数是在传入Page
对象的时候才会自动解析,没想到实际上是不管是自定义的实体类,只要跟他定义的参数名一样,就会触发一次隐性分页,最终修改配置文件;
pagehelper:
# 原因所在
support-methods-arguments: false
helperDialect: mysql
reasonable: true
params: count=countSql
反思了一下,也是自己看文档不认真。