1.pom依赖的引入
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.7</version>
</dependency>
2.在项目的入口类application.java中设置PageHelper
//配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","true");
properties.setProperty("rowBoundsWithCount","true");
properties.setProperty("reasonable","true");
properties.setProperty("dialect","mysql"); //配置mysql数据库的方言
pageHelper.setProperties(properties);
return pageHelper;
}
3.PageBean工具类
package com.fykj.cloud.epaper.entity.bean;
import java.util.List;
/**
* 分页bean
*/
public class PageBean<T> {
// 当前页
private Integer currentPage = 1;
// 每页显示的总条数
private Integer pageSize = 10;
// 总条数
private Integer totalNum;
// 是否有下一页
private Integer isMore;
// 总页数
private Integer totalPage;
// 开始索引
private Integer startIndex;
// 分页结果
private List<T> 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 PageBean(Integer currentPage, Integer pageSize, Integer totalNum,List<T> list){
this(currentPage,pageSize, totalNum);
this.items = list;
}
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;
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 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<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
}
4.分页功能代码实现(servicece层)
@Override
public PageBean<UserBlackList> selectUserBlackListList(Integer currentPage, Integer pageSize, UserBlackList userBlackList) {
try {
PageBean<UserBlackList> entity = new PageBean();
if (null != pageSize) {
entity.setPageSize(pageSize);
}
if (null != currentPage) {
entity.setCurrentPage(currentPage);
}
PageHelper.startPage(entity.getCurrentPage(), entity.getPageSize());
List<UserBlackList> list = userBlackListMapper.queryModelList(userBlackList);
Integer count = userBlackListMapper.queryModelListCount(userBlackList);
entity.setItems(list);
entity.setTotalNum(count);
return entity;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}