使用通用mapper按条件查询分页数据(包含Example的使用)

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

相关文章
相关标签/搜索