问题描述
初学MyBatis,使用分页插件的时候,输入PageHelper.startPage(m,n)会报如下错误,导致报红不分页
org.apache.ibatis.exceptions.PersistenceException:
### 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 0,2' at line 1
### The error may exist in com/aaa/mapper/UserMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select * from user; 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 0,2' at line 1
原因分析:
提示SQL语句异常,第一方反应打开看看自己的.xml文件中的SQL语句是否异常(如下),大致一看没有问题,相继检查了 PageHelper依赖和插件,也没有问题
<select id="findAll2" resultType="user">
select * from user;
</select>
查阅相关资料发现:
当我们使用PageHelper这个分页插件的时候,会自动给我们的SQL语句加上limit,所以加上limit之后我们的SQL因为有个;号所以出现了语法错误,
解决方案:
去掉SQL语句中末尾的分号(”;“)