1 步驟: 分頁 , 添加條件, 返回page對象, 封裝爲須要的對象app
2 通常分頁數據須要三個參數: 總頁數, 總條數, 對象的集合, this
所以能夠創建一個通用類,封裝上面的三個參數,具體以下: code
public class PageResult<T> {
private Long total;// 總條數
private Long totalPage;// 總頁數
private List<T> items;// 當前頁數據
public PageResult() {
}
public PageResult(Long total, List<T> items) {
this.total = total;
this.items = items;
}
public PageResult(Long total, Long totalPage, List<T> items) {
this.total = total;
this.totalPage = totalPage;
this.items = items;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
public Long getTotalPage() {
return totalPage;
}
public void setTotalPage(Long totalPage) {
this.totalPage = totalPage;
}
}
3 所以須要返回的數據類型就是PageResult<Goods> (以商品類爲例)
// 分頁,最多容許查100條
PageHelper.startPage(page, Math.min(rows, 100));對象
// 建立查詢條件
Example example = new Example(Goods.class);
Example.Criteria criteria = example.createCriteria();get
// 是否模糊查詢
if (StringUtils.isNotBlank(key)) {
criteria.andLike("title", "%" + key + "%");
}it
//獲得page對象
Page<Goods> pageInfo = (Page<Goods>) this.spuMapper.selectByExample(example);class
//封裝爲pageResult對象List
return new PageResult<>(pageInfo.getTotal(),pageInfo.getResult());select