分页封装
1)
public class SimplePage implements Serializable {
private static final long serialVersionUID = 1L;public static final int DEF_COUNT = 20;
protected int totalCount = 0;
protected int pageSize = 10;
protected int pageNo = 1;
protected int startIndex = 0;
public static int checkPageNo(Integer pageNo) {
return (pageNo == null || pageNo < 1) ? 1 : pageNo;
}
public SimplePage() {
}
public SimplePage(int pageNo, int pageSize, int totalCount) {
setTotalCount(totalCount);
setPageSize(pageSize);
setPageNo(pageNo);
adjustPageNo();
}
/**
* 调整页码,使其不超过最大页数
*/
public void adjustPageNo() {
if (pageNo == 1) {
return;
}
int totalPage = getTotalPage();
if (pageNo > totalPage) {
pageNo = totalPage;
}
startIndex = this.pageSize * (this.pageNo - 1);
}
public int getTotalCount() {
return totalCount;
}
public int getPageSize() {
return pageSize;
}
public int getPageNo() {
return pageNo;
}
public int getStartIndex() {
return startIndex;
}
public int getTotalPage() {
int totalPage = totalCount / pageSize;
if (totalPage == 0 || totalCount % pageSize != 0) {
totalPage++;
}
return totalPage;
}
/**
* 是否第一页
* @return
*/
public boolean isFirstPage() {
return pageNo <= 1;
}
/**
* 是否最后一页
* @return
*/
public boolean isLastPage() {
return pageNo >= getTotalPage();
}
/**
* 下一页页码
* @return
*/
public int getNextPage() {
if (isLastPage()) {
return pageNo;
} else {
return pageNo + 1;
}
}
public int getPretPage() {
if (isFirstPage()) {
return pageNo;
} else {
return pageNo - 1;
}
}
public void setTotalCount(int totalCount) {
if (totalCount < 0) {
this.totalCount = 0;
} else {
this.totalCount = totalCount;
}
}
public void setPageSize(Integer pageSize) {
if (pageSize == null || pageSize < 1) {
this.pageSize = DEF_COUNT;
} else {
this.pageSize = pageSize;
}
startIndex = this.pageSize * (this.pageNo - 1);
}
public void setPageNo(Integer pageNo) {
if (pageNo == null || pageNo < 1) {
this.pageNo = 1;
} else {
this.pageNo = pageNo;
}
startIndex = this.pageSize * (this.pageNo - 1);
}
}
2)
public class Pagination extends SimplePage implements Serializable {
private static final long serialVersionUID = 1L;
public final static String PAGE_CONDITION_CACHE = "PageCondition";
public final static String PAGE_CURRENT_NO_CACHE = "PageCurrentNo";
public final static String PAGE_SIZE_CACHE = "PageSize";
public final static String PAGE_TOTAL_COUNT_CACHE = "PageTotalCount";
public final static String PAGE_ALL_CONDITION_CACHE = "PageAllCondition";
private String id;
/**
* 检查页码是否超过最后一页
*/
private boolean checkPassLastPageNo = true;
/**
* 是否查询总条目数
*/
private boolean selectTotalCount = true;
private List<?> list;
private Object condition;
private Integer sort;
/**
* 初始条目偏移量
*/
private Integer offset = 0;
public Pagination() {
}
public Pagination(int pageNo, int pageSize, int totalCount) {
super(pageNo, pageSize, totalCount);
}
public Pagination(int pageNo, int pageSize, int totalCount, int offset) {
super(pageNo, pageSize, totalCount);
setOffset(offset);
}
public Pagination(int pageNo, int pageSize, int totalCount, List<?> list) {
super(pageNo, pageSize, totalCount);
this.list = list;
}
public int getFirstResult() {
return (pageNo - 1) * pageSize + offset;
}
public List<?> getList() {
return list;
}
public void setList(List<?> list) {
this.list = list;
}
public Object getCondition() {
return condition;
}
public void setCondition(Object condition) {
this.condition = condition;
}
public Integer getSort() {
return sort;
}
public void setSort(Integer sort) {
this.sort = sort;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean isCheckPassLastPageNo() {
return checkPassLastPageNo;
}
public void setCheckPassLastPageNo(boolean checkPassLastPageNo) {
this.checkPassLastPageNo = checkPassLastPageNo;
}
public boolean isSelectTotalCount() {
return selectTotalCount;
}
public void setSelectTotalCount(boolean selectTotalCount) {
this.selectTotalCount = selectTotalCount;
}
public Integer getOffset() {
return offset;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
}