关于使用PageHelper报错java.sql.SQLSyntaxErrorException
主要报错信息如下
2023-10-15 17:02:43.178 ERROR 21788 --- [nio-8080-exec-3] 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: 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 5, 5' at line 2
### The error may exist in com/fl/casetlias/mapper/EmpMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from emp; 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 5, 5' at line 2
; bad SQL grammar []; nested exception is 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 5, 5' at line 2] with root cause
postman信息如下:
{
"timestamp": "2023-10-15T22:02:43.188+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/emps"
}
解决方法
仔细看看控制台报错信息
发现sql语句为
Preparing: select * from emp; LIMIT ?, ?
回到mapper文件中,发现
@Select("select * from emp;")
List<Emp> list();
删除’;'后,运行正常
总结
使用PageHelper进行分页查询时,PageHelper会自动在语句末尾追加LIMIT,所以语句不要加’;’