Page.java
package com.xxx.common.util; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; /** * * @author fym * * @param <T> */ public class Page<T> implements Serializable { /** * */ 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<T> result = new ArrayList<T>(); protected long totalCount = -1; // -- 構造函數 --// public Page() { } public Page(int pageSize) { this.pageSize = 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; } } /** * 返回Page對象自身的setPageNo函數,可用於連續設置。 */ public Page<T> pageNo(final int thePageNo) { setPageNo(thePageNo); return this; } /** * 獲得每頁的記錄數量, 默認為-1. */ public int getPageSize() { return pageSize; } /** * 設置每頁的記錄數量. */ public void setPageSize(final int pageSize) { this.pageSize = pageSize; } /** * 返回Page對象自身的setPageSize函數,可用於連續設置。 */ public Page<T> pageSize(final int thePageSize) { setPageSize(thePageSize); return this; } /** * 根據pageNo和pageSize計算當前頁第一條記錄在總結果集中的位置,序號從1開始. */ public int getFirst() { return ((pageNo - 1) * pageSize) + 1; } /** * 獲得排序字段,無默認值. 多個排序字段時用','分隔. */ public String getOrderBy() { return orderBy; } /** * 設置排序字段,多個排序字段時用','分隔. */ public void setOrderBy(final String orderBy) { this.orderBy = orderBy; } /** * 返回Page對象自身的setOrderBy函數,可用於連續設置。 */ public Page<T> orderBy(final String theOrderBy) { setOrderBy(theOrderBy); return this; } /** * 獲得排序方向, 無默認值. */ 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; } /** * 返回Page對象自身的setOrder函數,可用於連續設置。 */ public Page<T> order(final String theOrder) { setOrder(theOrder); return this; } /** * 是否已設置排序字段,無默認值. */ public boolean isOrderBySetted() { return (StringUtils.isNotBlank(orderBy) && StringUtils .isNotBlank(order)); } /** * 獲得查詢對象時是否先自動執行count查詢獲取總記錄數, 默認為false. */ public boolean isAutoCount() { return autoCount; } /** * 設置查詢對象時是否自動先執行count查詢獲取總記錄數. */ public void setAutoCount(final boolean autoCount) { this.autoCount = autoCount; } /** * 返回Page對象自身的setAutoCount函數,可用於連續設置。 */ public Page<T> autoCount(final boolean theAutoCount) { setAutoCount(theAutoCount); return this; } // -- 訪問查詢結果函數 --// /** * 獲得頁內的記錄列表. */ public List<T> getResult() { return result; } /** * 設置頁內的記錄列表. */ public void setResult(final List<T> result) { this.result = result; } /** * 獲得總記錄數, 默認值為-1. */ public long getTotalCount() { return totalCount; } /** * 設置總記錄數. */ public void setTotalCount(final long totalCount) { this.totalCount = totalCount; } /** * 根據pageSize與totalCount計算總頁數, 默認值為-1. */ public long getTotalPages() { if (totalCount < 0) { return -1; } long count = totalCount / pageSize; if (totalCount % pageSize > 0) { count++; } return count; } /** * 是否還有下一頁. */ public boolean isHasNext() { return (pageNo + 1 <= getTotalPages()); } /** * 取得下頁的頁號, 序號從1開始. 當前頁為尾頁時仍返回尾頁序號. */ public int getNextPage() { if (isHasNext()) { return pageNo + 1; } else { return pageNo; } } /** * 是否還有上一頁. */ public boolean isHasPre() { return (pageNo - 1 >= 1); } /** * 取得上頁的頁號, 序號從1開始. 當前頁為首頁時返回首頁序號. */ public int getPrePage() { if (isHasPre()) { return pageNo - 1; } else { return pageNo; } } }