SpringBoot + PageHelper, MyBatis分页不生效解决方案

PageHelper是Mybatis的一个很好的分页插件,但要使用它的分页功能需要注意一下几点。

1.导入相关包,例如maven导入依赖

1 <dependency>
2     <groupId>com.github.pagehelper</groupId>
3     <artifactId>pagehelper</artifactId>
4     <version>5.1.4</version>
5 </dependency>

需要注意的是SpringBoot用的是pagehelper-spring-boot-starter.

1 <dependency>
2     <groupId>com.github.pagehelper</groupId>
3     <artifactId>pagehelper-spring-boot-starter</artifactId>
4     <version>1.2.3</version>
5 </dependency>

2.在mybatis-config.xml中添加插件

1 <plugins>
2    <plugin interceptor="com.github.pagehelper.PageInterceptor">
3         <!--分页参数合理化  -->
4         <property name="reasonable" value="true"/>
5    </plugin>
6 </plugins>

SpringBoot yml配置文件配置方式

1 pagehelper:
2   helperDialect: mysql
3   reasonable: true  #开启优化,如果开启优化,在分页页码结果没有数据的时候,会显示有数据的页码数据
4   supportMethodsArguments: true #是否支持接口参数来传递分页参数,默认false
5   pageSizeZero: false #pageSize=0 返回所有
6   params: count=countSql

PageHelper官网:https://pagehelper.github.io/docs

猜你喜欢

转载自www.cnblogs.com/yapin/p/11925579.html