一、Maven依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.8</version>
</dependency>
二、分页查询
1、List列表
@Test
public void findPage(){
PageHelper.startPage(1, 2, true);
List<ProductCategory> list = productCategoryMapper.selectAll();
log.info(JSON.toJSONString(list));
}
返回数据:
[{"categoryImg":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2634938388,2218013244&fm=173&app=49&f=JPEG?w=640&h=368&s=1FB4C4041E28230F7C1448840300D0AE","categoryName":"商口","categorySort":0,"id":"1073149717596823552","parentId":"0","status":1,"updateTime":1544694082000},{"categoryImg":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2634938388,2218013244&fm=173&app=49&f=JPEG?w=640&h=368&s=1FB4C4041E28230F7C1448840300D0AE","categoryName":"商口1","categorySort":0,"createTime":1544695821000,"id":"1073158172968820736","parentId":"0","status":1}]
2、带分页参数
1)分页类
@Data
public class PageBean<T> implements Serializable {
private static final long serialVersionUID = 8656597559014685635L;
private long total; //总记录数
private List<T> list; //结果集
private int pageNum; // 第几页
private int pageSize; // 每页记录数
private int pages; // 总页数
private int size; // 当前页的数量 <= pageSize,该属性来自ArrayList的size属性
/**
* @param list
*/
public PageBean(List<T> list) {
if (list instanceof Page) {
Page<T> page = (Page<T>) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.total = page.getTotal();
this.pages = page.getPages();
this.list = page;
this.size = page.size();
}
}
}
2)测试
@Test
public void findPage(){
PageHelper.startPage(1, 2, true);
List<ProductCategory> list = productCategoryMapper.selectAll();
PageBean pageBean=new PageBean<ProductCategory>(list);
log.info(JSON.toJSONString(pageBean));
}
返回数据:
{"list":[{"categoryImg":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2634938388,2218013244&fm=173&app=49&f=JPEG?w=640&h=368&s=1FB4C4041E28230F7C1448840300D0AE","categoryName":"商口","categorySort":0,"id":"1073149717596823552","parentId":"0","status":1,"updateTime":1544694082000},{"categoryImg":"https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=2634938388,2218013244&fm=173&app=49&f=JPEG?w=640&h=368&s=1FB4C4041E28230F7C1448840300D0AE","categoryName":"商口1","categorySort":0,"createTime":1544695821000,"id":"1073158172968820736","parentId":"0","status":1}],"pageNum":1,"pageSize":2,"pages":2,"size":2,"total":3}