日期:2014-05-17 浏览次数:20954 次
public class Page<T> {
// -- 分页参数 --//
/**
* 当前页
*/
protected int pageNum = 1;
/**
* 每页的记录条数
*/
protected int pageSize = 10;
protected int totalPages=1;
protected List<Sort> orders = new ArrayList<Sort>();
protected boolean autoCount = true;
// -- 返回结果 --//
protected List<T> rows = new ArrayList<T>();
/**
* 总记录数
*/
protected long total = -1;
// -- 构造函数 --//
public Page() {
}
public Page(int pageSize) {
this.pageSize = pageSize;
}
public Page(int pageSize, int currentPage, long total) {
this.pageSize = pageSize;
this.pageNum = currentPage;
this.total = total;
}
// -- 访问查询参数函数 --//
/**
* 获得当前页的页号,序号从1开始,默认为1.
*/
public int getPageNum() {
return pageNum;
}
/**
* 设置当前页的页号,序号从1开始,低于1时自动调整为1.
*/
public void setPageNum(final int page) {
this.pageNum = page;
if (page < 1) {
this.pageNum = 1;
}
}
public Page<T> page(final int thePage) {
setPageNum(thePage);
return this;
}
/**
* 获得每页的记录数量,默认为1.
*/
public int getPageSize() {
return pageSize;
}
/**
* 设置每页的记录数量,低于1时自动调整为1.
*/
public void setPageSize(final int pageSize) {
this.pageSize = pageSize;
if (pageSize < 1) {
this.pageSize = 1;
}
}
public Page<T> pageSize(final int thePageSize) {
setPageSize(thePageSize);
return this;
}
/**
* 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.
*/
public int getFirst() {
return ((pageNum - 1) * pageSize);
}
/**
* 获得排序方向.
*/
public Sort[] getOrders() {
Sort[] sorts = new Sort[orders.size()];
return orders.toArray(sorts);
}
/**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final Sort order) {
orders.add(order);
return this;
}
/**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final String field) {
orders.add(Sort.add(field, Sort.OrderStyle.ASC));
return this;
}
/**
* 设置排序方式向.
*
* @param order
*
*/
public Page<T> addOrder(final String field, final Sort.OrderStyle style) {
orders.add(Sort.add(field, style));
return this;
}
/**
* 是否已设置排序字段,无默认值.
*/
public boolean isOrderBySetted() {
return orders.size() > 0;
}
/**
* 查询对象时是否自动另外执行count查询获取总记录数, 默认为false.
*/
public boolean isAutoCount() {
return autoCount;
}
/**
* 查询对象时是否自动另外执行count查询获取总记录数.
*/
public Page<T> setAutoCount(final boolean autoCount) {
this.autoCount = autoCount;
return this;
}
public Page<T> autoCount(final boolean theAutoCount) {
setAutoCount(theAutoCount);
return this;
}
// -- 访问查询结果函数 --//
/**
* 取得页内的记录列表.
*/
public List<T> getRows() {
return rows;
}
/**
* 设置页内的记录列表.
*/
public Page<T> setRows(final List<T> rows) {
this.rows = rows;
return this;
}
/**
* 取得总记录数, 默认值为-1.
*/
publi