packagecom.xxxmon.util;importjava.io.Serializable;importjava.util.ArrayList;importjava.util.List;importorg.apachemons.lang.StringUtils;/***

*@authorfym

*

*@param*/

public class Page implementsSerializable {/****/

private static final long serialVersionUID = -3355049623232655831L;//-- 公共变量 --// public static final String ASC = "asc";public static final String DESC = "desc";//-- 分页参数 --// protected int pageNo = 1;protected int pageSize = -1;protected String orderBy = null;protected String order = null;protected boolean autoCount = true;//-- 返回结果 --// protected List result = new ArrayList();protected long totalCount = -1;//-- 构造函数 --// publicPage() {

}public Page(intpageSize) {this.pageSize =pageSize;

}//-- 分页参数访问函数 --// /*** 获得当前页的页号,序号从1开始,默认为1.*/

public intgetPageNo() {returnpageNo;

}/*** 设置当前页的页号,序号从1开始,低于1时自动调整为1.*/

public void setPageNo(final intpageNo) {this.pageNo =pageNo;if (pageNo < 1) {this.pageNo = 1;

}

}/*** 返回Page对象自身的setPageNo函数,可用于连续设置。*/

public Page pageNo(final intthePageNo) {

setPageNo(thePageNo);return this;

}/*** 获得每页的记录数量, 默认为-1.*/

public intgetPageSize() {returnpageSize;

}/*** 设置每页的记录数量.*/

public void setPageSize(final intpageSize) {this.pageSize =pageSize;

}/*** 返回Page对象自身的setPageSize函数,可用于连续设置。*/

public Page pageSize(final intthePageSize) {

setPageSize(thePageSize);return this;

}/*** 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从1开始.*/

public intgetFirst() {return ((pageNo - 1) * pageSize) + 1;

}/*** 获得排序字段,无默认值. 多个排序字段时用','分隔.*/

publicString getOrderBy() {returnorderBy;

}/*** 设置排序字段,多个排序字段时用','分隔.*/

public void setOrderBy(finalString orderBy) {this.orderBy =orderBy;

}/*** 返回Page对象自身的setOrderBy函数,可用于连续设置。*/

public Page orderBy(finalString theOrderBy) {

setOrderBy(theOrderBy);return this;

}/*** 获得排序方向, 无默认值.*/

publicString getOrder() {returnorder;

}/*** 设置排序方式向.

*

*@paramorder

* 可选值为desc或asc,多个排序字段时用','分隔.*/

public void setOrder(finalString 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;

}/*** 返回Page对象自身的setOrder函数,可用于连续设置。*/

public Page order(finalString theOrder) {

setOrder(theOrder);return this;

}/*** 是否已设置排序字段,无默认值.*/

public booleanisOrderBySetted() {return (StringUtils.isNotBlank(orderBy) &&StringUtils

.isNotBlank(order));

}/*** 获得查询对象时是否先自动执行count查询获取总记录数, 默认为false.*/

public booleanisAutoCount() {returnautoCount;

}/*** 设置查询对象时是否自动先执行count查询获取总记录数.*/

public void setAutoCount(final booleanautoCount) {this.autoCount =autoCount;

}/*** 返回Page对象自身的setAutoCount函数,可用于连续设置。*/

public Page autoCount(final booleantheAutoCount) {

setAutoCount(theAutoCount);return this;

}//-- 访问查询结果函数 --//

/*** 获得页内的记录列表.*/

public ListgetResult() {returnresult;

}/*** 设置页内的记录列表.*/

public void setResult(final Listresult) {this.result =result;

}/*** 获得总记录数, 默认值为-1.*/

public longgetTotalCount() {returntotalCount;

}/*** 设置总记录数.*/

public void setTotalCount(final longtotalCount) {this.totalCount =totalCount;

}/*** 根据pageSize与totalCount计算总页数, 默认值为-1.*/

public longgetTotalPages() {if (totalCount < 0) {return -1;

}long count = totalCount /pageSize;if (totalCount % pageSize > 0) {

count++;

}returncount;

}/*** 是否还有下一页.*/

public booleanisHasNext() {return (pageNo + 1 <=getTotalPages());

}/*** 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.*/

public intgetNextPage() {if(isHasNext()) {return pageNo + 1;

}else{returnpageNo;

}

}/*** 是否还有上一页.*/

public booleanisHasPre() {return (pageNo - 1 >= 1);

}/*** 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.*/

public intgetPrePage() {if(isHasPre()) {return pageNo - 1;

}else{returnpageNo;

}

}

}

更多推荐

java通过page获取值_Page.java