[TOC]
首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而不为?通常引入它们只需三步骤,不管是Spring集成还是SpringBoot集成都是老套路,这里就分开总结了。。。
Spring集成PageHelper
第一步:pom文件引入依赖
1 2 3 4 5
| <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> </dependency>
|
第二步:MyBatis的核心配置文件中引入配置项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases> <package name="com.liuurick.manager.po" /> </typeAliases>
<plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins>
</configuration>
|
第三步:业务逻辑实现分页功能,我们只需将当前查询的页数page和每页显示的总条数rows传进去,然后Pagehelper已经帮我们分好数据了,只需在web层获取数据即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @Override public DatagridResult itemList(Integer page, Integer rows) { if(page == null){ page = 1; } if(page <= 0){ page = 1; } if(rows == null){ rows = 30; } PageHelper.startPage(page, rows); TbItemExample example = new TbItemExample(); List<TbItem> list = tbItemMapper.selectByExample(example); PageInfo<TbItem> pageInfo = new PageInfo<>(list); DatagridResult result = new DatagridResult(); result.setTotal(pageInfo.getTotal()); result.setRows(list); return result; }
|
springboot集成PageHelper
第一步:pom文件还是需要引入依赖
1 2 3 4 5
| <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
|
第二步:这次直接是在项目的入口类application.java中直接设置PageHelper插件即可
1 2 3 4 5 6 7 8 9 10 11 12
| @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"); pageHelper.setProperties(properties); return pageHelper; }
|
第三步:同理,使用插件实现分页功能,方式还是一样,只需将当前查询的页数和每页显示的条数穿进去即可,直接源码
这是需要用到的分页实体,各位可以直接享用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| package com.riemann.utils;
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层)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @Override public List<Item> findItemByPage(int currentPage,int pageSize) { 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(); }
@RequestMapping("/itemsPage") @ResponseBody public List<Item> itemsPage(int currentPage,int pageSize){ return itemService.findItemByPage(currentPage, pageSize); }
|