1.pom中添加依赖包
1 <!--pageHelper基本依赖 --> 2 <dependency> 3 <groupId>com.github.pagehelper</groupId> 4 <artifactId>pagehelper</artifactId> 5 <version>5.1.2</version> 6 </dependency> 7 <!-- 在我的实验中不加这两个依赖分页不会成功 --> 8 <dependency> 9 <groupId>com.github.pagehelper</groupId> 10 <artifactId>pagehelper-spring-boot-autoconfigure</artifactId> 11 <version>1.2.5</version> 12 </dependency> 13 <dependency> 14 <groupId>com.github.pagehelper</groupId> 15 <artifactId>pagehelper-spring-boot-starter</artifactId> 16 <version>1.2.5</version> 17 </dependency>
2.配置分页插件
下面二者选一配置
2.1.application.properties配置
在application.properties文件中添加如下配置
1 #分页插件 2 pagehelper.helper-dialect=MYSQL 3 pagehelper.reasonable=true 4 pagehelper.support-methods-arguments=true 5 pagehelper.params=count=countSql
2.2.配文件配置对象
1 package com.qiaXXXXXX.config; 2 3 import com.github.pagehelper.PageHelper; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 7 import java.util.Properties; 8 9 /** 10 * @Copyright (C) 四川XXXX 11 * @Author: LI 12 * @Date: 7/1 11:01 13 * @Description: 14 */ 15 @Configuration 16 public class PageHelperConfig { 17 @Bean 18 public PageHelper getPageHelper() { 19 PageHelper pageHelper = new PageHelper(); 20 Properties properties = new Properties(); 21 properties.setProperty("helperDialect", "mysql"); 22 properties.setProperty("reasonable", "true"); 23 properties.setProperty("supportMethodsArguments", "true"); 24 properties.setProperty("params", "count=countSql"); 25 pageHelper.setProperties(properties); 26 return pageHelper; 27 } 28 29 }
3.分页实现
@Override public PageInfo<MyOrderVo> getMyOrder(MyOrderObj obj) { if (StringUtils.isEmpty(obj.getQueryMonth())) { String endMonth = DateUtil.getEndMonth(); obj.setStartMonth(DateUtil.getStartMonth(endMonth)); obj.setEndMonth(endMonth); } else { obj.setStartMonth(null); obj.setEndMonth(null); } //设置分页参数 PageHelper.startPage(obj.getPageNo(), obj.getPageSize()); //查询列表数据 List<MyOrderVo> list = userCenterMapper.getMyOrder(obj); //获取分页对象 PageInfo<MyOrderVo> pageInfo = new PageInfo<>(list); return pageInfo; }
注意:有了分页插件,sql语句不需要写limit,插件会在执行的sql中自动添加,也不需要自己单独写count语句获取总共条数,分页插件会自动获取.