import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Iterator; import java.util.List; import net.sf.json.JSONObject; /** * 分页类 * @author rubekid * * @param <T> */ public class Page<T> extends QueryParameter { public static final int PAGE_SIZE = 20; private List<T> result = null; private long totalCount = -1; private JSONObject queryParams = new JSONObject(); public Page() { setPageSize(PAGE_SIZE); } public Page(int pageSize) { setPageSize(pageSize); } public Page(int pageSize, boolean autoCount) { setPageSize(pageSize); this.autoCount = autoCount; } /** * 获取反向排序 * @return */ public String getInverseOrder() { if (order.endsWith("desc")){ return "asc"; } return "desc"; } public List<T> getResult() { return result; } public void setResult(List<T> result) { this.result = result; } public long getTotalCount() { return this.totalCount; } public void setTotalCount(long totalCount) { this.totalCount = totalCount; } 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()); } public int getNextPage() { if (isHasNext()){ return pageNo + 1; } return this.pageNo; } public boolean isHasPre() { return (pageNo - 1 >= 1); } public int getPrePage() { if (isHasPre()){ return pageNo - 1; } return pageNo; } public void addQueryParam(String key, Object value) { queryParams.put(key, value); } @SuppressWarnings("unchecked") public String getQueryString() throws UnsupportedEncodingException{ String result = ""; for (Iterator<String> iterator = queryParams.keys(); iterator.hasNext();) { String key = iterator.next(); result += key + "=" + URLEncoder.encode(queryParams.getString(key), "utf-8") + "&"; } if (result.length() > 0){ result = result.substring(0, result.length() - 1); } return result; } public String getQueryParamString() { return queryParams.toString(); } public void setQueryString(String queryString) { queryParams = JSONObject.fromObject(queryString); } public JSONObject getQueryParams() { return queryParams; } }
时间: 2024-11-05 05:55:54