springboot整合pagehelper分页无效的常见原因
1.maven依赖的问题
此类原因是与pom.xml文件中引入的分页依赖有关
由于springboot本身集成pagerhelper的分页插件
只需要引入如下依赖即可
<!-- spring-boot mybatis pagehelper -->
<dependency>
<groupid>com.github.pagehelper</groupid>
<artifactid>pagehelper-spring-boot-starter</artifactid>
<version>1.2.10</version>
</dependency>如引入的为如下依赖
需要添加bean注入(如何添加请自行百度)
<dependency>
<groupid>com.github.pagehelper</groupid>
<artifactid>pagehelper</artifactid>
<version>5.1.10</version>
</dependency>2.执行pagehelper.startpage(int pagenum, int pagesize)
后没有紧跟分页查询,而是先执行了其他查询
如下初始化分页器后,应该紧跟mybatis的分页查询语句,方法中如有其他查询需求,需要在其他查询完成后,再执行pagehelper.startpage(int pagenum, int pagesize)方法
public pageinfo<r> page(map<string, ? extends object> map) {
//获取第1页,10条内容,默认查询总数count
pagehelper.startpage(integer.parseint(map.get("pagenum").tostring()), integer.parseint(map.get("pagesize").tostring()));
string sql = string.format("%s%s",sqlmapping , map.get("mapping")==null?"getpageobjlist" : map.get("mapping")) ;
list<r> l = sqlsessiontemplate.selectlist(sql , map);
return new pageinfo<r>(l);
}3.没有配置mybatis的分页拦截器(也是我遇到的问题)
当拦截器没有配置的时候,每次进行list查询都会返回全部结果数据,此时需要在启动类中注入拦截器类
@bean
public interceptor[] plugins() {
return new interceptor[]{new pageinterceptor()};
}或者在mybatis的配置文件mybatis-config.xml中添加如下代码
<configuration> <plugins> <plugin interceptor="com.github.pagehelper.pageinterceptor"/> </plugins> </configuration>
总结
以上就是综合网上大家遇到的springboot使用pagehelper进行分页时,遇到查询出全部数据而没有进行分页的常见问题及解决方案。
这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论