package cn.com.timekey.drugmonitor.utils.db;
import java.util.Collections;
import java.util.List;
import org.hibernate.criterion.CriteriaSpecification;
/**
* 分页常用的bean类。<br>
* 里面包含搜索返回的List,查询条件DetachedCriteria及分页菜单用到的数据等。
*
* @author KennyLee E-mail:kennylee26@gmail.com 2008-10-25
* @param <T>
*/
public class PaginationSupport<T> {
// Default page size
public static final int PAGESIZE = 20;
public static final int MENU_SIZE = 7;
// total Rows
private int totalRowsAmount;
private int pageSize = PAGESIZE;
private int totalPages;
// current page number
private int currentPage = 1;
// next page number
private int nextPage;
// previous page number
private int previousPage;
// is has next page
private boolean hasNext;
// is has previous page
private boolean hasPrevious;
// current page start row number
private int pageStartRow = 0;
// current page end row number
private int pageEndRow;
private String[] pageMenuNum;
private int menuSize = MENU_SIZE;
// Pagination values
@SuppressWarnings("unchecked")
private List<T> items = Collections.EMPTY_LIST;
// select detachedCriteria
private CriteriaSpecification detachedCriteria;
public PaginationSupport() {
}
/**
* 构造函数。
*
* @param totalRows
* 总行数
* @param currentPage
* 当前页数
*/
public PaginationSupport(int totalRows, int currentPage) {
setPaginationSupport(totalRows, currentPage);
}
/**
* general 构造函数。
*
* @param totalRows
* 总行数
* @param currentPage
* 当前页数
* @param pageSize
* 每页显示数量。
*/
public PaginationSupport(int totalRows, int currentPage, int pageSize) {
this.pageSize = pageSize;
this.setPaginationSupport(totalRows, currentPage);
}
/**
* @param items
* @param totalCount
*/
public PaginationSupport(List<T> items, int totalCount) {
setPageSize(PAGESIZE);
setItems(items);
int currentPage = 1;
this.setPaginationSupport(totalCount, currentPage);
}
/**
* @param items
* @param totalCount
* @param startIndex
*/
public PaginationSupport(List<T> items, int totalCount, int startIndex) {
setPageSize(PAGESIZE);
setItems(items);
int currentPage = (startIndex / pageSize) + 1;
this.setPaginationSupport(totalCount, currentPage);
}
/**
* @param items
* 保存的目标List
* @param totalCount
* 查找的总行数
* @param pageSize
* 每页显示的行数
* @param startIndex
* 第几行开始
*
*/
public PaginationSupport(List<T> items, int totalCount, int pageSize,
int startIndex) {
setPageSize(pageSize);
setItems(items);
int currentPage = (startIndex / pageSize) + 1;
this.setPaginationSupport(totalCount, currentPage);
}
/**
* DetachedCriteria构造PS时的初始化方法。
*
* @param items
* 保存的目标List
* @param totalCount
* 查找的总行数
* @param pageSize
* 每页显示的行数
* @param startIndex
* 第几行开始
* @param detachedCriteria
*/
public PaginationSupport(List<T> items, int totalCount, int pageSize,
int startIndex, CriteriaSpecification detachedCriteria) {
setPageSize(pageSize);
setItems(items);
int currentPage = (startIndex / pageSize) + 1;
this.setPaginationSupport(totalCount, currentPage);
setDetachedCriteria(detachedCriteria);
}
/**
* <p>
* 替换items,重新构造PaginationSupport对象。
* </p>
*
* @param ps
* @param newItems
*/
public PaginationSupport(PaginationSupport<?> ps, List<T> newItems) {
this(newItems, ps.getTotalRowsAmount(), ps.getPageSize(), ps
.getPageStartRow());
}
public String[] getPageMenuNum() {
return pageMenuNum;
}
public void setPageMenuNum(String[] pageMenuNum) {
this.pageMenuNum = pageMenuNum;
}
public CriteriaSpecification getDetachedCriteria() {
return detachedCriteria;
}
public void setDetachedCriteria(CriteriaSpecification detachedCriteria) {
this.detachedCriteria = detachedCriteria;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
/**
* 分页导航按钮
*
* @param currentPage
* @return
*/
private String[] getPageMenuNums(int currentPage) {
String[] pageNums = null;
if (totalPages > menuSize) {// 总页数大于导航显示的页数
pageNums = new String[menuSize];
int lastMenuNum = totalPages - menuSize + 1;// 最后一列导航栏按钮
int beginMumNum = menuSize;
int x = menuSize - 1;// 导航显示系数
if ((currentPage < lastMenuNum) && (currentPage > beginMumNum)) {
for (int i = 0; i < menuSize; i++) {
pageNums[i] = String.valueOf(currentPage + i - x / 2);
}
} else if (currentPage > lastMenuNum) {
for (int i = 0; i < menuSize; i++) {
pageNums[i] = String.valueOf(lastMenuNum + i);
}
} else if (currentPage == lastMenuNum) {
if ((lastMenuNum - x / 2) < 1) {
lastMenuNum = x / 2 + 1;
}
for (int i = 0; i < menuSize; i++) {
pageNums[i] = String.valueOf(lastMenuNum + i - x / 2);
}
} else if (currentPage == beginMumNum) {
for (int i = 0; i < menuSize; i++) {
pageNums[i] = String.valueOf(1 + i + x / 2);
}
} else {
for (int i = 0; i < menuSize; i++) {
pageNums[i] = String.valueOf(1 + i);
}
}
} else {// 总页数小于等于导航显示的页数,直接显示。
pageNums = new String[totalPages];
// 分页数比总页数少
for (int i = 0; i < totalPages; i++) {
pageNums[i] = String.valueOf(i + 1);
}
}
return pageNums;
}
/**
* 设置总行数。
*
* @param rows
* 总行数。
*/
private void setTotalRowsAmount(int rows) {
if (rows < 0) {
totalRowsAmount = 0;
} else {
totalRowsAmount = rows;
}
if (totalRowsAmount % pageSize == 0) {
totalPages = totalRowsAmount / pageSize;
} else {
totalPages = totalRowsAmount / pageSize + 1;
}
}
/**
* 设置当前页数。
*
* @param curPage
*/
private void setCurrentPage(int curPage) {
if (curPage <= 0) {
currentPage = 1;
} else if (curPage > totalPages) {
currentPage = totalPages;
} else {
currentPage = curPage;
}
if (currentPage > 1) {
hasPrevious = true;
} else {
hasPrevious = false;
}
if (currentPage == totalPages) {
hasNext = false;
} else {
hasNext = true;
}
if (totalPages != 0) {
nextPage = currentPage + 1;
previousPage = currentPage - 1;
// 计算当前页开始行和结束行
pageStartRow = (currentPage - 1) * pageSize + 1;
// 记录索引从0开始
pageStartRow -= 1;
pageEndRow = pageStartRow + pageSize;
}
}
public void setPaginationSupport(int totalRows, int currentPage) {
// 获取总页码,通过对象总数还是每页多少行的关系
setTotalRowsAmount(totalRows);
setCurrentPage(currentPage);
String[] pageNums = getPageMenuNums(currentPage);
this.setPageMenuNum(pageNums);
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public int getCurrentPage() {
return currentPage;
}
public boolean isHasNext() {
return hasNext;
}
public boolean isHasPrevious() {
return hasPrevious;
}
public int getNextPage() {
return nextPage;
}
public int getPageSize() {
return pageSize;
}
public int getPreviousPage() {
return previousPage;
}
public int getTotalPages() {
return totalPages;
}
public int getTotalRowsAmount() {
return totalRowsAmount;
}
public int getPageStartRow() {
return pageStartRow;
}
public int getPageEndRow() {
return pageEndRow;
}
public String description() {
String description = "Total:" + this.getTotalRowsAmount() + " items "
+ this.getTotalPages() + " pages,Current page:"
+ this.currentPage + " Previous " + this.hasPrevious + " Next:"
+ this.hasNext + " start row:" + this.pageStartRow
+ " end row:" + this.pageEndRow;
return description;
mybatis代码生成器避免生成Example类的配置参数 }
public void init() {
// do some initialization work
}
public int getMenuSize() {
return menuSize;
}
public void setMenuSize(int menuSize) {
this.menuSize = menuSize;
}
}