java 分頁工具類+Mybatis攔截SQL實現分頁
阿新 • • 發佈:2019-01-04
轉載!一個比較好的分頁!
/** * 對Page<E>結果進行包裝 * <p/> * 新增分頁的多項屬性,主要參考:http://bbs.csdn.net/topics/360010907 * @version 3.3.0 * @since 3.2.2 * 專案地址 : http://git.oschina.net/free/Mybatis_PageHelper */ @SuppressWarnings({"rawtypes", "unchecked"}) public class PageInfo<T> implements Serializable { private static final long serialVersionUID = 1L; //當前頁 private int pageNum; //每頁的數量 private int pageSize; //當前頁的數量 private int size; //排序 private String orderBy; //由於startRow和endRow不常用,這裡說個具體的用法 //可以在頁面中"顯示startRow到endRow 共size條資料" //當前頁面第一個元素在資料庫中的行號 private int startRow; //當前頁面最後一個元素在資料庫中的行號 private int endRow; //總記錄數 private long total; //總頁數 private int pages; //結果集 private List<T> list; //第一頁 private int firstPage; //前一頁 private int prePage; //下一頁 private int nextPage; //最後一頁 private int lastPage; //是否為第一頁 private boolean isFirstPage = false; //是否為最後一頁 private boolean isLastPage = false; //是否有前一頁 private boolean hasPreviousPage = false; //是否有下一頁 private boolean hasNextPage = false; //導航頁碼數 private int navigatePages; //所有導航頁號 private int[] navigatepageNums; public PageInfo() { } /** * 包裝Page物件 * * @param list */ public PageInfo(List<T> list) { this(list, 8); } /** * 包裝Page物件 * * @param list page結果 * @param navigatePages 頁碼數量 */ public PageInfo(List<T> list, int navigatePages) { if (list instanceof Page) { Page page = (Page) list; this.pageNum = page.getPageNum(); this.pageSize = page.getPageSize(); this.orderBy = page.getOrderBy(); this.pages = page.getPages(); this.list = page; this.size = page.size(); this.total = page.getTotal(); //由於結果是>startRow的,所以實際的需要+1 if (this.size == 0) { this.startRow = 0; this.endRow = 0; } else { this.startRow = page.getStartRow() + 1; //計算實際的endRow(最後一頁的時候特殊) this.endRow = this.startRow - 1 + this.size; } } else if (list instanceof Collection) { this.pageNum = 1; this.pageSize = list.size(); this.pages = 1; this.list = list; this.size = list.size(); this.total = list.size(); this.startRow = 0; this.endRow = list.size() > 0 ? list.size() - 1 : 0; } if (list instanceof Collection) { this.navigatePages = navigatePages; //計算導航頁 calcNavigatepageNums(); //計算前後頁,第一頁,最後一頁 calcPage(); //判斷頁面邊界 judgePageBoudary(); } } /** * 計算導航頁 */ private void calcNavigatepageNums() { //當總頁數小於或等於導航頁碼數時 if (pages <= navigatePages) { navigatepageNums = new int[pages]; for (int i = 0; i < pages; i++) { navigatepageNums[i] = i + 1; } } else { //當總頁數大於導航頁碼數時 navigatepageNums = new int[navigatePages]; int startNum = pageNum - navigatePages / 2; int endNum = pageNum + navigatePages / 2; if (startNum < 1) { startNum = 1; //(最前navigatePages頁 for (int i = 0; i < navigatePages; i++) { navigatepageNums[i] = startNum++; } } else if (endNum > pages) { endNum = pages; //最後navigatePages頁 for (int i = navigatePages - 1; i >= 0; i--) { navigatepageNums[i] = endNum--; } } else { //所有中間頁 for (int i = 0; i < navigatePages; i++) { navigatepageNums[i] = startNum++; } } } } /** * 計算前後頁,第一頁,最後一頁 */ private void calcPage() { if (navigatepageNums != null && navigatepageNums.length > 0) { firstPage = navigatepageNums[0]; lastPage = navigatepageNums[navigatepageNums.length - 1]; if (pageNum > 1) { prePage = pageNum - 1; } if (pageNum < pages) { nextPage = pageNum + 1; } } } /** * 判定頁面邊界 */ private void judgePageBoudary() { isFirstPage = pageNum == 1; isLastPage = pageNum == pages; hasPreviousPage = pageNum > 1; hasNextPage = pageNum < pages; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public String getOrderBy() { return orderBy; } public void setOrderBy(String orderBy) { this.orderBy = orderBy; } public int getStartRow() { return startRow; } public void setStartRow(int startRow) { this.startRow = startRow; } public int getEndRow() { return endRow; } public void setEndRow(int endRow) { this.endRow = endRow; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } public int getPages() { return pages; } public void setPages(int pages) { this.pages = pages; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } public int getFirstPage() { return firstPage; } public void setFirstPage(int firstPage) { this.firstPage = firstPage; } public int getPrePage() { return prePage; } public void setPrePage(int prePage) { this.prePage = prePage; } public int getNextPage() { return nextPage; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public int getLastPage() { return lastPage; } public void setLastPage(int lastPage) { this.lastPage = lastPage; } public boolean isIsFirstPage() { return isFirstPage; } public void setIsFirstPage(boolean isFirstPage) { this.isFirstPage = isFirstPage; } public boolean isIsLastPage() { return isLastPage; } public void setIsLastPage(boolean isLastPage) { this.isLastPage = isLastPage; } public boolean isHasPreviousPage() { return hasPreviousPage; } public void setHasPreviousPage(boolean hasPreviousPage) { this.hasPreviousPage = hasPreviousPage; } public boolean isHasNextPage() { return hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; } public int getNavigatePages() { return navigatePages; } public void setNavigatePages(int navigatePages) { this.navigatePages = navigatePages; } public int[] getNavigatepageNums() { return navigatepageNums; } public void setNavigatepageNums(int[] navigatepageNums) { this.navigatepageNums = navigatepageNums; } @Override public String toString() { final StringBuffer sb = new StringBuffer("PageInfo{"); sb.append("pageNum=").append(pageNum); sb.append(", pageSize=").append(pageSize); sb.append(", size=").append(size); sb.append(", startRow=").append(startRow); sb.append(", endRow=").append(endRow); sb.append(", total=").append(total); sb.append(", pages=").append(pages); sb.append(", list=").append(list); sb.append(", firstPage=").append(firstPage); sb.append(", prePage=").append(prePage); sb.append(", nextPage=").append(nextPage); sb.append(", lastPage=").append(lastPage); sb.append(", isFirstPage=").append(isFirstPage); sb.append(", isLastPage=").append(isLastPage); sb.append(", hasPreviousPage=").append(hasPreviousPage); sb.append(", hasNextPage=").append(hasNextPage); sb.append(", navigatePages=").append(navigatePages); sb.append(", navigatepageNums="); if (navigatepageNums == null) sb.append("null"); else { sb.append('['); for (int i = 0; i < navigatepageNums.length; ++i) sb.append(i == 0 ? "" : ", ").append(navigatepageNums[i]); sb.append(']'); } sb.append('}'); return sb.toString(); } }
分頁類:
/** * 要分頁類繼承基類 * */ public abstract class AbstractQueryParameter{ /** * 頁碼,從1開始 */ private int pageNum; /** * 頁面大小 */ private int pageSize; public AbstractQueryParameter() { } public AbstractQueryParameter(int pageNum, int pageSize) { super(); this.pageNum = pageNum; this.pageSize = pageSize; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { if(pageNum==0){ pageNum=1; } this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { if(pageSize==0){ pageSize=10; } this.pageSize = pageSize; } }
實體類需要繼承分頁類:
public class Query extends AbstractQueryParameter{ private String id; @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DateJsonFormat.PATTERN, timezone = DateJsonFormat.TIMEZONE_BEIJING) private Date startTime;//查詢開始時間 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DateJsonFormat.PATTERN, timezone = DateJsonFormat.TIMEZONE_BEIJING) private Date endTime;//查詢結束時間 public String getId() { return id; } public void setId(String id) { this.areaId = areaId; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } }
service層呼叫方式:
@Transactional(readOnly = true)
public PageResponse<AreaReportResult> findCompanyByArea(AreaQuery query) {
PageHelper.startPage(query.getPageNum(), query.getPageSize());
PageInfo<AreaReportResult> pager = new PageInfo<AreaReportResult>(companyMapper.findCompanyByArea(query));
return new PageResponse<AreaReportResult>(query.getPageNum(), query.getPageSize(), pager.getTotal(),
pager.getPages(), pager.getList());
}