//查询数量
int selectForCount(QueryObject qo);
//查询指定页数据
List<Product> selectForList(QueryObject qo);
Service部分
public class ProductServiceImpl implements IProductService {
private IProductDao productDao = new ProductDaoImpl();
@Override
public PageResult<Product> query(QueryObject ob) {
int count = productDao.selectForCount(ob);
List<Product> products= productDao.selectForList(ob);
PageResult<Product> result =new PageResult<>(count,ob.getPageSize(),ob.getCurrentPage(),products);
return result;
}
}
封装对象
QueryObject封装对象(分页数据)
@Setter
@Getter
public class QueryObject {
private int currentPage = 1;
private int pageSize =5;
//用Limit 子句第一个参数取值使用
public int getStart(){
return (currentPage - 1 ) * pageSize;
}
}
ProductQueryObject封装对象(过滤数据)
@Getter
@Setter
public class ProductQueryObject extends QueryObject{
private String productName;
private BigDecimal minSalePrice;
private BigDecimal maxSalePrice;
public String getProductName() {
if (StringUtil.hasLength(productName)) {
return productName;
}
return null;
}
}
PageResult封装对象(返回给前端的)
@Getter
@Setter
public class PageResult<T> {
private int totalCount; //总数量
private List<T> data; //某页数据
//3个计算出来的
private int totalPage; //总页数
private int prePage;//上一页
private int nextPage;//下一页
//用户传递的
private int currentPage;
private int pageSize;
public PageResult(int totalCount,int pageSize , int currentPage,List<T> data ) {
this.totalCount = totalCount;
this.data = data;
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalPage = totalCount % pageSize ==0 ? (totalCount / pageSize) : (totalCount / pageSize +1);
this.prePage = currentPage - 1 >= 1 ? currentPage - 1 : 1;
this.nextPage = currentPage + 1 <= this.totalPage ? currentPage + 1 : this.totalPage;
}
}