mybatis-plus以及pageHelper依赖
<!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.4</version> </dependency> <!--分页插件依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency>
配置yml
# pageHelper分页插件配置 #分页设置 pagehelper: helper-dialect: mysql reasonable: true supportMethods-arguments: true pageSizeZero: false #pageSize=0 返回所有 params: count=countSql
使用
PageHelper.startPage(1,5);//第一页 每页两条 List<User> list = userMapper.fingAll(); PageInfo<User> pageInfo = new PageInfo<>(list); List<User> list1 = pageInfo.getList(); long total = pageInfo.getTotal(); System.out.println(list1); return list1;
然后分页没生效。。
原因:PageInterceptor在springboot项目中没有自动配置
解决: 在项目中自己初始化PageInterceptor拦截器(pagehelper依赖中的)
@Configuration public class MyBatisPlusConfig { @Bean PageInterceptor pageInterceptor(){ PageInterceptor pageInterceptor = new PageInterceptor(); Properties properties = new Properties(); properties.setProperty("helperDialect", "mysql"); pageInterceptor.setProperties(properties); // 由此可进入源码, return pageInterceptor; } }