第一步:pom文件还是需要引入依赖
<!--mybatis的分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
第二步:这次直接是在项目的入口类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;
}
第三步:同理,使用插件实现分页功能,方式还是一样,只需将当前查询的页数和每页显示的条数穿进去即可,直接源码
/**
* 分页bean
*/ import java.util.List; 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 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<T> getItems() {
return items;
} public void setItems(List<T> items) {
this.items = items;
}
}
分页功能源码(web层和service层)。
@Override
public List<Item> findItemByPage(int currentPage,int pageSize) {
//设置分页信息,分别是当前页数和每页显示的总记录数【记住:必须在mapper接口中的方法执行之前设置该分页信息】
PageHelper.startPage(currentPage, pageSize); List<Item> allItems = itemMapper.findAll(); //全部商品
int countNums = itemMapper.countItem(); //总记录数
PageBean<Item> pageData = new PageBean<>(currentPage, pageSize, countNums);
pageData.setItems(allItems);
return pageData.getItems();
}
/**
* 商品分页功能(集成mybatis的分页插件pageHelper实现)
*
* @param currentPage :当前页数
* @param pageSize :每页显示的总记录数
* @return
*/
@RequestMapping("/itemsPage")
@ResponseBody
public List<Item> itemsPage(int currentPage,int pageSize){
return itemService.findItemByPage(currentPage, pageSize);
}
到这儿呢,MyBatis的分页插件PageHelper就完全和SpringBoot集成到一起了,确实没有什么新鲜的。第一次使用,留下下次使用