Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014 abel533@gmail.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.dao.genericdao.mybatis.plugins.page.support; import org.apache.ibatis.session.RowBounds; import java.util.ArrayList; import java.util.List; /** * Mybatis - */ public class PageHelper<E> extends ArrayList<E> { private static final long serialVersionUID = 1L; /** * ?count */ private static final int NO_SQL_COUNT = -1; /** * count */ private static final int SQL_COUNT = 0; /** * ?1 */ private int pageNum; /** * ?? */ private int pageSize; /** * */ private int startRow; /** * */ private int endRow; /** * */ private long total; /** * */ private int pages; /** * sqlserver - ? */ private String orderBy; /** * ?? */ private Boolean reasonable; /** * truepagesize0RowBoundslimit=0? */ private Boolean pageSizeZero; public PageHelper() { super(); } public PageHelper(int pageNum, int pageSize) { this(pageNum, pageSize, SQL_COUNT, null); } public PageHelper(int pageNum, int pageSize, boolean count) { this(pageNum, pageSize, count ? PageHelper.SQL_COUNT : PageHelper.NO_SQL_COUNT, null); } public PageHelper(int pageNum, int pageSize, long total, Boolean reasonable) { super(pageSize > -1 ? pageSize : 0); this.pageNum = pageNum; this.pageSize = pageSize; this.total = total; calculateStartAndEndRow(); setReasonable(reasonable); } public PageHelper(RowBounds rowBounds, boolean count) { this(rowBounds, count ? PageHelper.SQL_COUNT : PageHelper.NO_SQL_COUNT); } public PageHelper(RowBounds rowBounds, int total) { super(rowBounds.getLimit() > -1 ? rowBounds.getLimit() : 0); this.pageSize = rowBounds.getLimit(); this.startRow = rowBounds.getOffset(); //RowBounds??countcount,?SQL_COUNT this.total = total; this.endRow = this.startRow + this.pageSize; } public List<E> getResult() { return this; } public int getPages() { return pages; } public int getEndRow() { return endRow; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { //??????? this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getStartRow() { return startRow; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; if (pageSize > 0) { pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1)); } else { pages = 0; } //??????? if ((reasonable != null && reasonable) && pageNum > pages) { pageNum = pages; calculateStartAndEndRow(); } } public void setReasonable(Boolean reasonable) { if (reasonable == null) { return; } this.reasonable = reasonable; //??????? if (this.reasonable && this.pageNum <= 0) { this.pageNum = 1; calculateStartAndEndRow(); } } public Boolean getReasonable() { return reasonable; } public Boolean getPageSizeZero() { return pageSizeZero; } public void setPageSizeZero(Boolean pageSizeZero) { this.pageSizeZero = pageSizeZero; } public String getOrderBy() { return orderBy; } /** * ? */ private void calculateStartAndEndRow() { this.startRow = this.pageNum > 0 ? (this.pageNum - 1) * this.pageSize : 0; this.endRow = this.startRow + this.pageSize * (this.pageNum > 0 ? 1 : 0); } public boolean isCount() { return this.total > NO_SQL_COUNT; } //? /** * ? * * @param pageNum * @return */ public PageHelper<E> pageNum(int pageNum) { //??????? this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum; return this; } /** * ?? * * @param pageSize * @return */ public PageHelper<E> pageSize(int pageSize) { this.pageSize = pageSize; calculateStartAndEndRow(); return this; } /** * ?count * * @param count * @return */ public PageHelper<E> count(Boolean count) { this.total = count ? PageHelper.SQL_COUNT : PageHelper.NO_SQL_COUNT; return this; } /** * sqlserver * * @param orderBy * @return */ public PageHelper<E> orderBy(String orderBy) { this.orderBy = orderBy; return this; } /** * ?? * * @param reasonable * @return */ public PageHelper<E> reasonable(Boolean reasonable) { setReasonable(reasonable); return this; } /** * truepagesize0RowBoundslimit=0? * * @param pageSizeZero * @return */ public PageHelper<E> pageSizeZero(Boolean pageSizeZero) { setPageSizeZero(pageSizeZero); return this; } @Override public String toString() { final StringBuffer sb = new StringBuffer("Page{"); sb.append("pageNum=").append(pageNum); sb.append(", pageSize=").append(pageSize); sb.append(", startRow=").append(startRow); sb.append(", endRow=").append(endRow); sb.append(", total=").append(total); sb.append(", pages=").append(pages); sb.append(", orderBy='").append(orderBy).append('\''); sb.append(", reasonable=").append(reasonable); sb.append(", pageSizeZero=").append(pageSizeZero); sb.append('}'); return sb.toString(); } }