interceptors, boolean executable) {
this.original = original;
this.interceptors = interceptors;
this.executable = executable;
}
@Override
public BoundSql doBoundSql(BoundSqlInterceptor.Type type, BoundSql boundSql, CacheKey cacheKey) {
if(executable) {
return _doBoundSql(type, boundSql, cacheKey);
} else {
return new BoundSqlInterceptorChain(original, interceptors, true).doBoundSql(type, boundSql, cacheKey);
}
}
private BoundSql _doBoundSql(BoundSqlInterceptor.Type type, BoundSql boundSql, CacheKey cacheKey) {
if (this.interceptors == null || this.interceptors.size() == this.index) {
return this.original != null ? this.original.doBoundSql(type, boundSql, cacheKey) : boundSql;
} else {
return this.interceptors.get(this.index++).boundSql(type, boundSql, cacheKey, this);
}
}
}
================================================
FILE: src/main/java/com/github/pagehelper/Constant.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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;
/**
* @author liuzh
*/
public interface Constant {
//分页的id后缀
String SUFFIX_PAGE = "_PageHelper";
//count查询的id后缀
String SUFFIX_COUNT = SUFFIX_PAGE + "_Count";
//第一个分页参数
String PAGEPARAMETER_FIRST = "First" + SUFFIX_PAGE;
//第二个分页参数
String PAGEPARAMETER_SECOND = "Second" + SUFFIX_PAGE;
}
================================================
FILE: src/main/java/com/github/pagehelper/CountMsIdGen.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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 org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
/**
* 构建当前查询对应的 count 方法 id
*
* 返回的 msId 会先判断是否存在自定义的方法,存在就直接使用
*
* 如果不存在,会根据当前的 msId 创建 MappedStatement
*
* @author liuzh
*/
public interface CountMsIdGen {
/**
* 默认实现
*/
CountMsIdGen DEFAULT = (ms, parameter, boundSql, countSuffix) -> ms.getId() + countSuffix;
/**
* 构建当前查询对应的 count 方法 id
*
* @param ms 查询对应的 MappedStatement
* @param parameter 方法参数
* @param boundSql 查询SQL
* @param countSuffix 配置的 count 后缀
* @return count 查询丢的 msId
*/
String genCountMsId(MappedStatement ms, Object parameter,
BoundSql boundSql, String countSuffix);
}
================================================
FILE: src/main/java/com/github/pagehelper/Dialect.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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 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;
import java.util.concurrent.Callable;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
/**
* 数据库方言,针对不同数据库进行实现
*
* @author liuzh
*/
public interface Dialect {
/**
* 跳过 count 和 分页查询
*
* @param ms MappedStatement
* @param parameterObject 方法参数
* @param rowBounds 分页参数
* @return true 跳过,返回默认查询结果,false 执行分页查询
*/
boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
/**
* 是否使用异步 count 查询,使用异步后不会根据返回的 count 数来判断是否有必要进行分页查询
*
* @return true 异步,false 同步
*/
default boolean isAsyncCount() {
return false;
}
/**
* 执行异步 count 查询
*
* @param task 异步查询任务
* @param
* @return
*/
default Future asyncCountTask(Callable task) {
return ForkJoinPool.commonPool().submit(task);
}
/**
* 执行分页前,返回 true 会进行 count 查询,false 会继续下面的 beforePage 判断
*
* @param ms MappedStatement
* @param parameterObject 方法参数
* @param rowBounds 分页参数
* @return
*/
boolean beforeCount(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
/**
* 生成 count 查询 sql
*
* @param ms MappedStatement
* @param boundSql 绑定 SQL 对象
* @param parameterObject 方法参数
* @param rowBounds 分页参数
* @param countKey count 缓存 key
* @return
*/
String getCountSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey countKey);
/**
* 执行完 count 查询后
*
* @param count 查询结果总数
* @param parameterObject 接口参数
* @param rowBounds 分页参数
* @return true 继续分页查询,false 直接返回
*/
boolean afterCount(long count, Object parameterObject, RowBounds rowBounds);
/**
* 处理查询参数对象
*
* @param ms MappedStatement
* @param parameterObject
* @param boundSql
* @param pageKey
* @return
*/
Object processParameterObject(MappedStatement ms, Object parameterObject, BoundSql boundSql, CacheKey pageKey);
/**
* 执行分页前,返回 true 会进行分页查询,false 会返回默认查询结果
*
* @param ms MappedStatement
* @param parameterObject 方法参数
* @param rowBounds 分页参数
* @return
*/
boolean beforePage(MappedStatement ms, Object parameterObject, RowBounds rowBounds);
/**
* 生成分页查询 sql
*
* @param ms MappedStatement
* @param boundSql 绑定 SQL 对象
* @param parameterObject 方法参数
* @param rowBounds 分页参数
* @param pageKey 分页缓存 key
* @return
*/
String getPageSql(MappedStatement ms, BoundSql boundSql, Object parameterObject, RowBounds rowBounds, CacheKey pageKey);
/**
* 分页查询后,处理分页结果,拦截器中直接 return 该方法的返回值
*
* @param pageList 分页查询结果
* @param parameterObject 方法参数
* @param rowBounds 分页参数
* @return
*/
Object afterPage(List pageList, Object parameterObject, RowBounds rowBounds);
/**
* 完成所有任务后
*/
void afterAll();
/**
* 设置参数
*
* @param properties 插件属性
*/
void setProperties(Properties properties);
}
================================================
FILE: src/main/java/com/github/pagehelper/IPage.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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;
/**
* @author liuzh
*/
public interface IPage {
Integer getPageNum();
Integer getPageSize();
String getOrderBy();
}
================================================
FILE: src/main/java/com/github/pagehelper/ISelect.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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;
/**
* 分页查询接口
*
* @author liuzh_3nofxnp
* @since 2015-12-18 18:51
*/
public interface ISelect {
/**
* 在接口中调用自己的查询方法,不要在该方法内写过多代码,只要一行查询方法最好
*/
void doSelect();
}
================================================
FILE: src/main/java/com/github/pagehelper/Page.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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.util.SqlSafeUtil;
import com.github.pagehelper.util.StackTraceUtil;
import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;
/**
* Mybatis - 分页对象
*
* @author liuzh/abel533/isea533
* @version 3.6.0
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*/
public class Page extends ArrayList implements Closeable {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(Page.class);
/**
* 记录当前堆栈,可查找到page在何处创建
* 需开启pagehelper.debug
*/
private final String stackTrace = PageInterceptor.isDebug() ? StackTraceUtil.current() : null;
/**
* 页码,从1开始
*/
private int pageNum;
/**
* 页面大小
*/
private int pageSize;
/**
* 起始行
*/
private long startRow;
/**
* 末行
*/
private long endRow;
/**
* 总数
*/
private long total;
/**
* 总页数
*/
private int pages;
/**
* 包含count查询
*/
private boolean count = true;
/**
* 分页合理化
*/
private Boolean reasonable;
/**
* 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
*/
private Boolean pageSizeZero;
/**
* 进行count查询的列名
*/
private String countColumn;
/**
* 排序
*/
private String orderBy;
/**
* 只增加排序
*/
private boolean orderByOnly;
/**
* sql拦截处理
*/
private BoundSqlInterceptor boundSqlInterceptor;
private transient BoundSqlInterceptor.Chain chain;
/**
* 分页实现类,可以使用 {@link com.github.pagehelper.page.PageAutoDialect} 类中注册的别名,例如 "mysql", "oracle"
*/
private String dialectClass;
/**
* 转换count查询时保留查询的 order by 排序
*/
private Boolean keepOrderBy;
/**
* 转换count查询时保留子查询的 order by 排序
*/
private Boolean keepSubSelectOrderBy;
/**
* 异步count查询
*/
private Boolean asyncCount;
public Page() {
super();
}
public Page(int pageNum, int pageSize) {
this(pageNum, pageSize, true, null);
}
public Page(int pageNum, int pageSize, boolean count) {
this(pageNum, pageSize, count, null);
}
private Page(int pageNum, int pageSize, boolean count, Boolean reasonable) {
super(0);
if (pageNum == 1 && pageSize == Integer.MAX_VALUE) {
pageSizeZero = true;
pageSize = 0;
}
this.pageNum = pageNum;
this.pageSize = pageSize;
this.count = count;
calculateStartAndEndRow();
setReasonable(reasonable);
}
/**
* int[] rowBounds
* 0 : offset
* 1 : limit
*/
public Page(int[] rowBounds, boolean count) {
super(0);
if (rowBounds[0] == 0 && rowBounds[1] == Integer.MAX_VALUE) {
pageSizeZero = true;
this.pageSize = 0;
this.pageNum = 1;
} else {
this.pageSize = rowBounds[1];
this.pageNum = rowBounds[1] != 0 ? (int) (Math.ceil(((double) rowBounds[0] + rowBounds[1]) / rowBounds[1])) : 0;
}
this.startRow = rowBounds[0];
this.count = count;
this.endRow = this.startRow + rowBounds[1];
}
public String getStackTrace() {
return stackTrace;
}
public List getResult() {
return this;
}
public int getPages() {
return pages;
}
public Page setPages(int pages) {
this.pages = pages;
return this;
}
public long getEndRow() {
return endRow;
}
public Page setEndRow(long endRow) {
this.endRow = endRow;
return this;
}
public int getPageNum() {
return pageNum;
}
public Page setPageNum(int pageNum) {
//分页合理化,针对不合理的页码自动处理
this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
return this;
}
public int getPageSize() {
return pageSize;
}
public Page setPageSize(int pageSize) {
this.pageSize = pageSize;
return this;
}
public long getStartRow() {
return startRow;
}
public Page setStartRow(long startRow) {
this.startRow = startRow;
return this;
}
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
if (total == -1) {
pages = 1;
return;
}
if (pageSize > 0) {
pages = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
} else {
pages = 0;
}
//分页合理化,针对不合理的页码自动处理
if ((reasonable != null && reasonable) && pageNum > pages) {
if (pages != 0) {
pageNum = pages;
}
calculateStartAndEndRow();
}
}
public Boolean getReasonable() {
return reasonable;
}
public Page setReasonable(Boolean reasonable) {
if (reasonable == null) {
return this;
}
this.reasonable = reasonable;
//分页合理化,针对不合理的页码自动处理
if (this.reasonable && this.pageNum <= 0) {
this.pageNum = 1;
calculateStartAndEndRow();
}
return this;
}
public Boolean getPageSizeZero() {
return pageSizeZero;
}
public Page setPageSizeZero(Boolean pageSizeZero) {
if (this.pageSizeZero == null && pageSizeZero != null) {
this.pageSizeZero = pageSizeZero;
}
return this;
}
public String getOrderBy() {
return orderBy;
}
/**
* 设置排序字段,增加 SQL 注入校验,如果需要在 order by 使用函数,可以使用 {@link #setUnsafeOrderBy(String)} 方法
*
* @param orderBy 排序字段
*/
public Page setOrderBy(String orderBy) {
if (SqlSafeUtil.check(orderBy)) {
throw new PageException("order by [" + orderBy + "] has a risk of SQL injection, " +
"if you want to avoid SQL injection verification, you can call Page.setUnsafeOrderBy");
}
this.orderBy = orderBy;
return (Page) this;
}
/**
* 不安全的设置排序方法,如果从前端接收参数,请自行做好注入校验。
*
* 请不要故意使用该方法注入然后提交漏洞!!!
*
* @param orderBy 排序字段
*/
public Page setUnsafeOrderBy(String orderBy) {
this.orderBy = orderBy;
return (Page) this;
}
public boolean isOrderByOnly() {
return orderByOnly;
}
public void setOrderByOnly(boolean orderByOnly) {
this.orderByOnly = orderByOnly;
}
public String getDialectClass() {
return dialectClass;
}
public void setDialectClass(String dialectClass) {
this.dialectClass = dialectClass;
}
public Boolean getKeepOrderBy() {
return keepOrderBy;
}
public Page setKeepOrderBy(Boolean keepOrderBy) {
this.keepOrderBy = keepOrderBy;
return this;
}
public Boolean getKeepSubSelectOrderBy() {
return keepSubSelectOrderBy;
}
public void setKeepSubSelectOrderBy(Boolean keepSubSelectOrderBy) {
this.keepSubSelectOrderBy = keepSubSelectOrderBy;
}
public Boolean getAsyncCount() {
return asyncCount;
}
public void setAsyncCount(Boolean asyncCount) {
this.asyncCount = asyncCount;
}
/**
* 指定使用的分页实现,如果自己使用的很频繁,建议自己增加一层封装再使用
*
* @param dialect 分页实现类,可以使用 {@link com.github.pagehelper.page.PageAutoDialect} 类中注册的别名,例如 "mysql", "oracle"
* @return
*/
public Page using(String dialect) {
this.dialectClass = dialect;
return this;
}
/**
* 计算起止行号
*/
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.count;
}
public Page setCount(boolean count) {
this.count = count;
return this;
}
/**
* 设置页码
*
* @param pageNum
* @return
*/
public Page pageNum(int pageNum) {
//分页合理化,针对不合理的页码自动处理
this.pageNum = ((reasonable != null && reasonable) && pageNum <= 0) ? 1 : pageNum;
return this;
}
/**
* 设置页面大小
*
* @param pageSize
* @return
*/
public Page pageSize(int pageSize) {
this.pageSize = pageSize;
calculateStartAndEndRow();
return this;
}
/**
* 是否执行count查询
*
* @param count
* @return
*/
public Page count(Boolean count) {
this.count = count;
return this;
}
/**
* 设置合理化
*
* @param reasonable
* @return
*/
public Page reasonable(Boolean reasonable) {
setReasonable(reasonable);
return this;
}
/**
* 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
*
* @param pageSizeZero
* @return
*/
public Page pageSizeZero(Boolean pageSizeZero) {
setPageSizeZero(pageSizeZero);
return this;
}
/**
* 设置 BoundSql 拦截器
*
* @param boundSqlInterceptor
* @return
*/
public Page boundSqlInterceptor(BoundSqlInterceptor boundSqlInterceptor) {
setBoundSqlInterceptor(boundSqlInterceptor);
return this;
}
/**
* 指定 count 查询列
*
* @param columnName
* @return
*/
public Page countColumn(String columnName) {
setCountColumn(columnName);
return this;
}
/**
* 转换count查询时保留查询的 order by 排序
*
* @param keepOrderBy
* @return
*/
public Page keepOrderBy(boolean keepOrderBy) {
this.keepOrderBy = keepOrderBy;
return this;
}
public boolean keepOrderBy() {
return this.keepOrderBy != null && this.keepOrderBy;
}
/**
* 转换count查询时保留子查询的 order by 排序
*
* @param keepSubSelectOrderBy
* @return
*/
public Page keepSubSelectOrderBy(boolean keepSubSelectOrderBy) {
this.keepSubSelectOrderBy = keepSubSelectOrderBy;
return this;
}
public boolean keepSubSelectOrderBy() {
return this.keepSubSelectOrderBy != null && this.keepSubSelectOrderBy;
}
/**
* 异步count查询
*
* @param asyncCount
* @return
*/
public Page asyncCount(boolean asyncCount) {
this.asyncCount = asyncCount;
return this;
}
/**
* 使用异步count查询
*
* @return
*/
public Page enableAsyncCount() {
return asyncCount(true);
}
/**
* 不使用异步count查询
*
* @return
*/
public Page disableAsyncCount() {
return asyncCount(false);
}
public boolean asyncCount() {
return this.asyncCount != null && this.asyncCount;
}
public PageInfo toPageInfo() {
return new PageInfo(this);
}
/**
* 数据对象转换
*
* @param function
* @param
* @return
*/
public PageInfo toPageInfo(Function function) {
List list = new ArrayList(this.size());
for (E e : this) {
list.add(function.apply(e));
}
PageInfo pageInfo = new PageInfo(list);
pageInfo.setTotal(this.getTotal());
pageInfo.setPageNum(this.getPageNum());
pageInfo.setPageSize(this.getPageSize());
pageInfo.setPages(this.getPages());
pageInfo.setStartRow(this.getStartRow());
pageInfo.setEndRow(this.getEndRow());
pageInfo.calcByNavigatePages(PageInfo.DEFAULT_NAVIGATE_PAGES);
return pageInfo;
}
public PageSerializable toPageSerializable() {
return new PageSerializable(this);
}
/**
* 数据对象转换
*
* @param function
* @param
* @return
*/
public PageSerializable toPageSerializable(Function function) {
List list = new ArrayList(this.size());
for (E e : this) {
list.add(function.apply(e));
}
PageSerializable pageSerializable = new PageSerializable(list);
pageSerializable.setTotal(this.getTotal());
return pageSerializable;
}
public Page doSelectPage(ISelect select) {
select.doSelect();
return (Page) this;
}
public PageInfo doSelectPageInfo(ISelect select) {
select.doSelect();
return (PageInfo) this.toPageInfo();
}
public PageSerializable doSelectPageSerializable(ISelect select) {
select.doSelect();
return (PageSerializable) this.toPageSerializable();
}
public long doCount(ISelect select) {
this.pageSizeZero = true;
this.pageSize = 0;
select.doSelect();
return this.total;
}
public String getCountColumn() {
return countColumn;
}
public void setCountColumn(String countColumn) {
if (!"0".equals(countColumn) && !"*".equals(countColumn) && SqlSafeUtil.check(countColumn)) {
throw new PageException("count(" + countColumn + ") has a risk of SQL injection");
}
this.countColumn = countColumn;
}
public BoundSqlInterceptor getBoundSqlInterceptor() {
return boundSqlInterceptor;
}
public void setBoundSqlInterceptor(BoundSqlInterceptor boundSqlInterceptor) {
this.boundSqlInterceptor = boundSqlInterceptor;
}
BoundSqlInterceptor.Chain getChain() {
return chain;
}
void setChain(BoundSqlInterceptor.Chain chain) {
this.chain = chain;
}
@Override
public String toString() {
return "Page{" +
"count=" + count +
", pageNum=" + pageNum +
", pageSize=" + pageSize +
", startRow=" + startRow +
", endRow=" + endRow +
", total=" + total +
", pages=" + pages +
", reasonable=" + reasonable +
", pageSizeZero=" + pageSizeZero +
'}' + super.toString();
}
@Override
public void close() {
PageHelper.clearPage();
}
/**
* 兼容低版本 Java 7-
*/
public interface Function {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
T apply(E t);
}
}
================================================
FILE: src/main/java/com/github/pagehelper/PageException.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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;
/**
* 分页插件异常
*/
public class PageException extends RuntimeException {
public PageException() {
super();
}
public PageException(String message) {
super(message);
}
public PageException(String message, Throwable cause) {
super(message, cause);
}
public PageException(Throwable cause) {
super(cause);
}
}
================================================
FILE: src/main/java/com/github/pagehelper/PageHelper.java
================================================
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 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.PageBoundSqlInterceptors;
import com.github.pagehelper.page.PageMethod;
import com.github.pagehelper.page.PageParams;
import com.github.pagehelper.parser.CountSqlParser;
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.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;
import java.util.concurrent.Future;
/**
* Mybatis - 通用分页拦截器
* 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
*
* @author liuzh/abel533/isea533
* @version 5.0.0
*/
public class PageHelper extends PageMethod implements Dialect, BoundSqlInterceptor.Chain {
private PageParams pageParams;
private PageAutoDialect autoDialect;
private PageBoundSqlInterceptors pageBoundSqlInterceptors;
private ForkJoinPool asyncCountService;
@Override
public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
Page page = pageParams.getPage(parameterObject, rowBounds);
if (page == null) {
return true;
} else {
//设置默认的 count 列
if (StringUtil.isEmpty(page.getCountColumn())) {
page.setCountColumn(pageParams.getCountColumn());
}
//设置默认的异步 count 设置
if (page.getAsyncCount() == null) {
page.setAsyncCount(pageParams.isAsyncCount());
}
autoDialect.initDelegateDialect(ms, page.getDialectClass());
return false;
}
}
@Override
public boolean isAsyncCount() {
return getLocalPage().asyncCount();
}
@Override
public Future asyncCountTask(Callable task) {
//异步执行时需要将ThreadLocal值传递,否则会找不到
AbstractHelperDialect dialectThreadLocal = autoDialect.getDialectThreadLocal();
Page