1、达到目的:
与具体ORM实现无关的分页参数及查询结果封装.
与具体ORM实现无关的分页参数及查询结果封装.
2、分页代码封装:
public class Page<T> implements Iterable<T> {
//-- 公共变量 --//
public static final String ASC = "asc";
public static final String DESC = "desc";
//-- 分页查询参数 --//
public int pageNo = 1;
public int pageSize = -1;
public int totalPage = 1;
public boolean autoCount = true;
public String orderBy = null;
public String order = null;
//-- 返回结果 --//
public List<T> result = Lists.newArrayList();
public Integer totalItems = -1;
@SuppressWarnings("rawtypes")
public Map returnMap;
//-- 构造函数 --//
public Page() {
}
public Page(int pageSize) {
setPageSize(pageSize);
}
public Page(int pageNo, int pageSize) {
setPageNo(pageNo);
setPageSize(pageSize);
}
//-- 分页参数访问函数 --//
/**
* 获得当前页的页号,序号从1开始,默认为1.
*/
public int getPageNo() {
return pageNo;
}
/**
* 设置当前页的页号,序号从1开始,低于1时自动调整为1.
*/
public void setPageNo(final int pageNo) {
this.pageNo = pageNo;
if (pageNo < 1) {
this.pageNo = 1;
}
}
/**
* 获得每页的记录数量, 默认为-1.
*/
public int getPageSize() {
return pageSize;
}
/**
* 设置每页的记录数量.
*/
public void setPageSize(final int pageSize) {
this.pageSize = pageSize;
}
/**
* 获得排序字段,无默认值. 多个排序字段时用','分隔.
*/
public String getOrderBy() {
return orderBy;
}
/**
* 设置排序字段,多个排序字段时用','分隔.
*/
public void setOrderBy(final String orderBy) {
this.orderBy = orderBy;
}
/**
* 获得排序方向, 无默认值.
*/
public String getOrder() {
return order;
}
/**
* 设置排序方式向.
*
* @param order 可选值为desc或asc,多个排序字段时用','分隔.
*/
public void setOrder(final String order) {
String lowcaseOrder = StringUtils.lowerCase(order);
//检查order字符串的合法值
String[] orders = StringUtils.split(lowcaseOrder, ',');
for (String orderStr : orders) {
if (!StringUtils.equals(DESC, orderStr) && !StringUtils.equals(ASC, orderStr)) {
throw new IllegalArgumentException("排序方向" + orderStr + "不是合法值");
}
}
this.order = lowcaseOrder;
}
/**
* 是否已设置排序字段,无默认值.
*/
public boolean isOrderBySetted() {
return (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(order))