SpringBoot+MyBatis+Maven项目中集成PageHelper分页插件
一、集成步骤
1、添加PageHelper插件依赖
2、注册分页对象PageHelper到Spring容器中
3、创建分页实体类
4、分页功能实现只需要添加 Page page= PageHelper.startPage(currentPage,PageBean.PAGE_SIZE,true);一行代码,既可自动实现分页功能。重点注意该行代码必须在业务SQL语句代码上一行。
二、实现
1、添加PageHelper插件依赖
com.github.pagehelper pagehelper-spring-boot-starter 1.2.32、注册分页对象PageHelper到Spring容器中
package org.cm.channelmanage.config;import com.github.pagehelper.PageHelper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.Properties;@Configurationpublic class PageHelperConfig { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties();// 该参数默认为false// 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用// 和startPage中的pageNum效果一样// properties.setProperty("offsetAsPageNum","true");// properties.setProperty("rowBoundsWithCount","true");// properties.setProperty("reasonable","true"); properties.setProperty("dialect","mysql"); //配置mysql数据库的方言 pageHelper.setProperties(properties); return pageHelper; }}3、创建分页实体类
package org.cm.channelmanage.result;import java.util.List;/** * 分页实体 */public class PageBean { public static final Integer PAGE_SIZE = 15; // 当前页 private Integer currentPage = 1; // 每页显示的总条数 private Integer pageSize; // 总条数 private Integer totalNum; // 是否有下一页 private Integer isMore; // 总页数 private Integer totalPage; // 开始索引 private Integer startIndex; // 分页结果 private List items; public PageBean() { super(); } public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) { super(); this.currentPage = currentPage; this.pageSize = pageSize; this.totalNum = totalNum; this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize; this.startIndex = (this.currentPage-1)*this.pageSize; this.isMore = this.currentPage >= this.totalPage?0:1; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage = currentPage; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } public Integer getTotalNum() { return totalNum; } public void setTotalNum(Integer totalNum) { this.totalNum = totalNum; } public Integer getIsMore() { return isMore; } public void setIsMore(Integer isMore) { this.isMore = isMore; } public Integer getTotalPage() { return totalPage; } public void setTotalPage(Integer totalPage) { this.totalPage = totalPage; } public Integer getStartIndex() { return startIndex; } public void setStartIndex(Integer startIndex) { this.startIndex = startIndex; } public List getItems() { return items; } public void setItems(List items) { this.items = items; }}4、业务实现类
@Override public ResultBundle queryApp(Integer currentPage) { //当前页 currentPage = currentPage==null?1:currentPage; //currentPage为当前页,PageBean.PAGE_SIZE为每页显示数据条数 //添加分页插件依赖包后,只需要在每个业务SQL语句前添加下行一句代码,既可自动实现分页功能。重点注意该行代码必须在业务SQL语句代码上一行 Page page= PageHelper.startPage(currentPage,PageBean.PAGE_SIZE,true); //SQL查询语句 List list = applicationInfoMapper.selectList(); PageBean pageBean = new PageBean(currentPage,PageBean.PAGE_SIZE,Long.valueOf(page.getTotal()).intValue()); pageBean.setItems(list); return ResultBundle.success(pageBean); }