解决pageHelper分页失效以及如何配置问题
作者:__WanG 时间:2022-03-15 06:45:44
pageHelper分页失效及配置问题
我在使用pageHelper的系统中加入mybatis-plus, 结果所有分页都失效了
原因
我这边的失效原因初步定为是因为mybatis-plus的自动配置和pageHelper的自动配置冲突了, 导致pageHelper的自动配置失效(最终是加上个配置类解决的)
解决方案
新建一个配置类
/**
* @Author: WanG
* @Date: 2019-05-13 18:42
* @version: v1.0
* @description: TODO
*/
@Configuration
public class MybatisConfig {
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
PageHelper分页无效及报错
第一种情况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>
第二种情况分页无效
原因:可能是代码前后顺序有问题,应该先写分页,再执行sql。
错误写法:
List<Student> students = studentMapper.selectAll();
PageHelper.startPage(1, 5, true);
正确写法:
PageHelper.startPage(1, 5, true);
List<Student> students = studentMapper.selectAll();
来源:https://wangq.blog.csdn.net/article/details/90178979
标签:pageHelper,分页,配置
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)
2022-07-31 14:39:23
六款值得推荐的android(安卓)开源框架简介
2023-06-24 01:46:54
MyBatis中映射文件的使用案例代码
2021-09-02 23:55:41
![](https://img.aspxhome.com/file/2023/0/60910_0s.png)
Java Web使用简单的批处理操作(记事本+Tomcat)
2021-08-13 10:08:33
![](https://img.aspxhome.com/file/2023/4/66074_0s.png)
java结合HADOOP集群文件上传下载
2022-12-14 23:08:52
C#设计模式之职责链模式示例详解
2023-11-08 05:26:38
基于Java ActiveMQ的实例讲解
2023-08-24 08:39:38
Java中的SuppressWarnings注解使用
2023-08-18 17:31:19
![](https://img.aspxhome.com/file/2023/9/78269_0s.png)
Java RandomAccessFile 指定位置实现文件读取与写入
2023-06-05 17:06:25
java获取当前时间并格式化代码实例
2021-10-06 17:06:16
![](https://img.aspxhome.com/file/2023/0/79240_0s.png)
C#如何优雅的结束线程
2022-07-27 19:45:27
前端如何调用后端接口进行数据交互详解(axios和SpringBoot)
2023-10-17 02:48:43
![](https://img.aspxhome.com/file/2023/5/72615_0s.png)
SpringBoot JPA使用配置过程详解
2022-12-03 15:31:33
Java注解方式之防止重复请求
2023-05-29 16:30:51
![](https://img.aspxhome.com/file/2023/1/82981_0s.jpg)
C语言高效编程的几招小技巧
2023-11-02 14:12:50
Android根据电话号码获得联系人头像实例代码
2022-01-07 04:08:10
Java调用linux shell脚本的方法
2022-03-25 23:09:46
基于rocketmq的有序消费模式和并发消费模式的区别说明
2021-10-29 08:41:02
![](https://img.aspxhome.com/file/2023/0/80310_0s.png)
Spring纯注解配置实现代码示例解析
2021-06-05 18:54:28
MyBatisPlus 自定义sql语句的实现
2021-11-11 07:51:20