1.第一种情况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 5'
> at line 3
原因:在xml写的sql带了分号,由于PageHelper会在sql尾部追加limit,所以导致生成sql时有误,导致错误。
错误写法:
<select id="selectAll" resultMap="BaseResultMap">
SELECT * FROM student;
</select>
正确写法:
<select id="selectAll" resultMap="BaseResultMap">
SELECT * FROM student
</select>
2.第二种情况分页无效
原因:可能是代码前后顺序有问题,应该先写分页,再执行sql。
错误写法:
List<Student> students = studentMapper.selectAll();
PageHelper.startPage(1, 5, true);
正确写法:
PageHelper.startPage(1, 5, true);
List<Student> students = studentMapper.selectAll();
本文介绍了解决MyBatis中使用PageHelper插件时出现的两种常见问题:SQL语法错误和分页功能失效。对于SQL语法错误,主要是由于在XML文件中编写的SQL语句末尾错误地包含了分号;对于分页功能失效,则是因为PageHelper插件的调用位置不正确。
2万+

被折叠的 条评论
为什么被折叠?



