对hibernate的封装 HibernateTemplate
使用hibernate也有很久一段时间了,做了点简单的封装:
package org.jutil.hibernate.base;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;public class Page<T> {private static final long serialVersionUID = 2728437513842150372L;private static final int DEFAULT_PAGE_SIZE = 10;private int start;private List<T> data;private long totalCount;private int pageSize = DEFAULT_PAGE_SIZE;public Page() {this(0, 0L, Page.DEFAULT_PAGE_SIZE, ((List<T>) (new ArrayList<T>())));}public Page(int pageSize) {this(0, 0L, pageSize, ((List<T>) (new ArrayList<T>())));}public Page(int start, long totalSize, int pageSize, List<T> data) {if (pageSize <= 0 || start < 0 || totalSize < 0L) {} else {this.pageSize = pageSize;this.start = start;totalCount = totalSize;this.data = data;return;}}public long getTotalCount() {return totalCount;}public long getTotalPageCount() {return totalCount % (long) pageSize != 0L ? totalCount/ (long) pageSize + 1L: totalCount/ (long) pageSize;}public void setResult(List<T> data) {this.data = data;}public List<T> getResult() {return data;}public int getCurrentPageNo() {return start / pageSize + 1;}public boolean hasNextPage() {return (long) getCurrentPageNo() < getTotalPageCount();}public boolean hasPreviousPage() {return getCurrentPageNo() > 1;}public boolean isEmpty() {return data == null || data.isEmpty();}public int getStartIndex() {return (getCurrentPageNo() - 1) * pageSize;}public int getEndIndex() {int endIndex = getCurrentPageNo() * pageSize - 1;return (long) endIndex < totalCount ? endIndex : (int) totalCount - 1;}protected static int getStartOfPage(int pageNo) {return getStartOfPage(pageNo, 20);}public static int getStartOfPage(int pageNo, int pageSize) {return (pageNo - 1) * pageSize;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}}