1.创建一个分页的工具类PageHelper
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 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.github.pagehelper;
import com.github.pagehelper.dialect.AbstractHelperDialect;
import com.github.pagehelper.page.PageAutoDialect;
import com.github.pagehelper.page.PageMethod;
import com.github.pagehelper.page.PageParams;
import com.github.pagehelper.parser.CountSqlParser;
import com.github.pagehelper.util.MSUtils;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.RowBounds;
import java.util.List;
import java.util.Properties;
/**
* Mybatis - 閫氱敤鍒嗛〉鎷︽埅鍣�<br/>
* 椤圭洰鍦板潃 : http://git.oschina.net/free/Mybatis_PageHelper
*
* @author liuzh/abel533/isea533
* @version 5.0.0
*/
public class PageHelper extends PageMethod implements Dialect {
private PageParams pageParams;
private PageAutoDialect autoDialect;
@Override
public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
if (ms.getId().endsWith(MSUtils.COUNT)) {
throw new RuntimeException("鍦ㄧ郴缁熶腑鍙戠幇浜嗗涓垎椤垫彃浠讹紝璇锋鏌ョ郴缁熼厤缃�!");
}
Page page = pageParams.getPage(parameterObject, rowBounds);
if (page == null) {
return true;
} else {
//璁剧疆榛樿鐨� count 鍒�
if (StringUtil.isEmpty(page.getCountColumn())) {
page.setCountColumn(pageParams.getCountColumn());
}
autoDialect.initDelegateDialect(ms);
return false;
}
}
@Override
public boolean beforeCount(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
return autoDialect.getDelegate().beforeCount(ms, parameterObject, rowBounds);
}
@Override
public String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey) {
return autoDialect.getDelegate().getCountSql(ms, boundSql, parameterObject, rowBounds, countKey);
}
@Override
public boolean afterCount(long count, Object parameterObject, RowBounds rowBounds) {
return autoDialect.getDelegate().afterCount(count, parameterObject, rowBounds);
}
@Override
public Object processParameterObject(MappedStatement ms, Object parameterObject, BoundSql boundSql, CacheKey pageKey) {
return autoDialect.getDelegate().processParameterObject(ms, parameterObject, boundSql, pageKey);
}
@Override
public boolean beforePage(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
return autoDialect.getDelegate().beforePage(ms, parameterObject, rowBounds);
}
@Override
public String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey) {
return autoDialect.getDelegate().getPageSql(ms, boundSql, parameterObject, rowBounds, pageKey);
}
public String getPageSql(String sql, Page page, RowBounds rowBounds, CacheKey pageKey) {
return autoDialect.getDelegate().getPageSql(sql, page, pageKey);
}
@Override
public Object afterPage(List pageList, Object parameterObject, RowBounds rowBounds) {
//杩欎釜鏂规硶鍗充娇涓嶅垎椤典篃浼氳鎵ц锛屾墍浠ヨ鍒ゆ柇 null
AbstractHelperDialect delegate = autoDialect.getDelegate();
if (delegate != null) {
return delegate.afterPage(pageList, parameterObject, rowBounds);
}
return pageList;
}
@Override
public void afterAll() {
//杩欎釜鏂规硶鍗充娇涓嶅垎椤典篃浼氳鎵ц锛屾墍浠ヨ鍒ゆ柇 null
AbstractHelperDialect delegate = autoDialect.getDelegate();
if (delegate != null) {
delegate.afterAll();
autoDialect.clearDelegate();
}
clearPage();
}
@Override
public void setProperties(Properties properties) {
setStaticProperties(properties);
pageParams = new PageParams();
autoDialect = new PageAutoDialect();
pageParams.setProperties(properties);
autoDialect.setProperties(properties);
//20180902鏂板 aggregateFunctions, 鍏佽鎵嬪姩娣诲姞鑱氬悎鍑芥暟锛堝奖鍝嶈鏁帮級
CountSqlParser.addAggregateFunctions(properties.getProperty("aggregateFunctions"));
}
}
2.创建PageInfo类
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017 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.github.pagehelper;
import java.util.Collection;
import java.util.List;
/**
* 瀵筆age<E>缁撴灉杩涜鍖呰
* <p/>
* 鏂板鍒嗛〉鐨勫椤瑰睘鎬э紝涓昏鍙傝��:http://bbs.csdn.net/topics/360010907
*
* @author liuzh/abel533/isea533
* @version 3.3.0
* @since 3.2.2
* 椤圭洰鍦板潃 : http://git.oschina.net/free/Mybatis_PageHelper
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class PageInfo<T> extends PageSerializable<T> {
//褰撳墠椤�
private int pageNum;
//姣忛〉鐨勬暟閲�
private int pageSize;
//褰撳墠椤电殑鏁伴噺
private int size;
//鐢变簬startRow鍜宔ndRow涓嶅父鐢紝杩欓噷璇翠釜鍏蜂綋鐨勭敤娉�
//鍙互鍦ㄩ〉闈腑"鏄剧ずstartRow鍒癳ndRow 鍏眘ize鏉℃暟鎹�"
//褰撳墠椤甸潰绗竴涓厓绱犲湪鏁版嵁搴撲腑鐨勮鍙�
private int startRow;
//褰撳墠椤甸潰鏈�鍚庝竴涓厓绱犲湪鏁版嵁搴撲腑鐨勮鍙�
private int endRow;
//鎬婚〉鏁�
private int pages;
//鍓嶄竴椤�
private int prePage;
//涓嬩竴椤�
private int nextPage;
//鏄惁涓虹涓�椤�
private boolean isFirstPage = false;
//鏄惁涓烘渶鍚庝竴椤�
private boolean isLastPage = false;
//鏄惁鏈夊墠涓�椤�
private boolean hasPreviousPage = false;
//鏄惁鏈変笅涓�椤�
private boolean hasNextPage = false;
//瀵艰埅椤电爜鏁�
private int navigatePages;
//鎵�鏈夊鑸〉鍙�
private int[] navigatepageNums;
//瀵艰埅鏉′笂鐨勭涓�椤�
private int navigateFirstPage;
//瀵艰埅鏉′笂鐨勬渶鍚庝竴椤�
private int navigateLastPage;
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) {
super(list);
if (list instanceof Page) {
Page page = (Page) list;
this.pageNum = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.size = page.size();
//鐢变簬缁撴灉鏄�>startRow鐨勶紝鎵�浠ュ疄闄呯殑闇�瑕�+1
if (this.size == 0) {
this.startRow = 0;
this.endRow = 0;
} else {
this.startRow = page.getStartRow() + 1;
//璁$畻瀹為檯鐨別ndRow锛堟渶鍚庝竴椤电殑鏃跺�欑壒娈婏級
this.endRow = this.startRow - 1 + this.size;
}
} else if (list instanceof Collection) {
this.pageNum = 1;
this.pageSize = list.size();
this.pages = this.pageSize > 0 ? 1 : 0;
this.size = list.size();
this.startRow = 0;
this.endRow = list.size() > 0 ? list.size() - 1 : 0;
}
if (list instanceof Collection) {
this.navigatePages = navigatePages;
//璁$畻瀵艰埅椤�
calcNavigatepageNums();
//璁$畻鍓嶅悗椤碉紝绗竴椤碉紝鏈�鍚庝竴椤�
calcPage();
//鍒ゆ柇椤甸潰杈圭晫
judgePageBoudary();
}
}
public static <T> PageInfo<T> of(List<T> list) {
return new PageInfo<T>(list);
}
public static <T> PageInfo<T> of(List<T> list, int navigatePages) {
return new PageInfo<T>(list, navigatePages);
}
/**
* 璁$畻瀵艰埅椤�
*/
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;
//(鏈�鍓峮avigatePages椤�
for (int i = 0; i < navigatePages; i++) {
navigatepageNums[i] = startNum++;
}
} else if (endNum > pages) {
endNum = pages;
//鏈�鍚巒avigatePages椤�
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) {
navigateFirstPage = navigatepageNums[0];
navigateLastPage = 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 || pages == 0;
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 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 int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}
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 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;
}
public int getNavigateFirstPage() {
return navigateFirstPage;
}
public int getNavigateLastPage() {
return navigateLastPage;
}
public void setNavigateFirstPage(int navigateFirstPage) {
this.navigateFirstPage = navigateFirstPage;
}
public void setNavigateLastPage(int navigateLastPage) {
this.navigateLastPage = navigateLastPage;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("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(", prePage=").append(prePage);
sb.append(", nextPage=").append(nextPage);
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(", navigateFirstPage=").append(navigateFirstPage);
sb.append(", navigateLastPage=").append(navigateLastPage);
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();
}
}
3.在创建MyPageInfo类
package com.kldhq.weight.model;
import java.util.List;
import com.github.pagehelper.PageInfo;
public class MyPageInfo<T> {
private int pageNum;
private long total;
private List<T> list;
public MyPageInfo() {
}
public MyPageInfo(PageInfo<T> info) {
this.pageNum = info.getPageNum();
this.total = info.getTotal();
this.list = info.getList();
}
public MyPageInfo(List<T> list) {
PageInfo<T> pageInfo = new PageInfo<T>(list);
this.pageNum = pageInfo.getPageNum();
this.total = pageInfo.getTotal();
this.list = list;
}
public MyPageInfo(int pageNum, long total, List<T> list) {
super();
this.pageNum = pageNum;
this.total = total;
this.list = list;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((list == null) ? 0 : list.hashCode());
result = prime * result + pageNum;
result = prime * result + (int) (total ^ (total >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyPageInfo<?> other = (MyPageInfo<?>) obj;
if (list == null) {
if (other.list != null)
return false;
} else if (!list.equals(other.list))
return false;
if (pageNum != other.pageNum)
return false;
if (total != other.total)
return false;
return true;
}
@Override
public String toString() {
return "MyPageInfo [pageNum=" + pageNum + ", total=" + total + ", list=" + list + "]";
}
}
4.正式使用。CarGpsAlarms是List集合,CarGpsAlarmAndCar是实体类,
//分页
PageHelper.startPage(params.getPageNum(),params.getPageSize());
PageInfo<CarGpsAlarmAndCar> pageInfo = new PageInfo<>(CarGpsAlarms);
MyPageInfo<CarGpsAlarmAndCar> myPageInfo = new MyPageInfo<CarGpsAlarmAndCar>(pageInfo.getPageNum(),pageInfo.getTotal(), CarGpsAlarms);