diff --git a/src/main/java/com/github/pagehelper/util/ExecutorUtil.java b/src/main/java/com/github/pagehelper/util/ExecutorUtil.java index 4fc3050a673dfa49b829a427b631dfb85abb738a..c63f28cd7edff18817ccbe2396384407dafdc2b5 100644 --- a/src/main/java/com/github/pagehelper/util/ExecutorUtil.java +++ b/src/main/java/com/github/pagehelper/util/ExecutorUtil.java @@ -36,6 +36,7 @@ import org.apache.ibatis.session.RowBounds; import java.lang.reflect.Field; import java.sql.SQLException; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -103,6 +104,8 @@ public abstract class ExecutorUtil { CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql); BoundSql countBoundSql = countMs.getBoundSql(parameter); Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql); + //当resultHandler存在的时候,countResultList对象则会变成空集合 + countResultList = ResultHandlerUtils.getInstance().getResult(countResultList, resultHandler); Long count = ((Number) ((List) countResultList).get(0)).longValue(); return count; } @@ -136,6 +139,8 @@ public abstract class ExecutorUtil { } //执行 count 查询 Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql); + //当resultHandler存在的时候,countResultList对象则会变成空集合 + countResultList = ResultHandlerUtils.getInstance().getResult(countResultList, resultHandler); Long count = (Long) ((List) countResultList).get(0); return count; } @@ -158,6 +163,7 @@ public abstract class ExecutorUtil { public static List pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql, CacheKey cacheKey) throws SQLException { + List result = null; //判断是否需要进行分页查询 if (dialect.beforePage(ms, parameter, rowBounds)) { //生成分页的缓存 key @@ -174,11 +180,16 @@ public abstract class ExecutorUtil { pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key)); } //执行分页查询 - return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql); + result = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql); } else { //不执行分页的情况下,也不执行内存分页 - return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql); + result = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql); } + + //当resultHandler存在的时候,countResultList对象则会变成空集合 + result = (List) ResultHandlerUtils.getInstance().getResult(result, resultHandler); + + return result; } } diff --git a/src/main/java/com/github/pagehelper/util/ResultHandlerUtils.java b/src/main/java/com/github/pagehelper/util/ResultHandlerUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..863e6b4d51ff515d90594fc470ccfa53fb26bf22 --- /dev/null +++ b/src/main/java/com/github/pagehelper/util/ResultHandlerUtils.java @@ -0,0 +1,116 @@ +package com.github.pagehelper.util; + +import com.github.pagehelper.PageException; + +import org.apache.ibatis.executor.result.DefaultMapResultHandler; +import org.apache.ibatis.executor.result.DefaultResultHandler; +import org.apache.ibatis.session.ResultHandler; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +/** + * 结果集处理器工具 + * + * @author Egan + * @email egzosn@gmail.com + * @date 2020/6/22 17:34 + */ +public class ResultHandlerUtils { + + private static volatile ResultHandlerUtils INSTANCE = null; + + private Map, Method> cacheMethod = new HashMap, Method>(); + + + public static ResultHandlerUtils getInstance() { + if (null == INSTANCE) { + synchronized (INSTANCE) { + if (null == INSTANCE) { + INSTANCE = new ResultHandlerUtils(); + } + } + } + return INSTANCE; + } + + /** + * 获取结果集 + * 当结果集处理器存在的时候,需要使用结果集处理器返回的结果。 + * + * @param result 结果集 + * @param resultHandler 结果集处理器 + * @return 结果集 + */ + public Object getResult(Object result, ResultHandler resultHandler) { + if (null == resultHandler) { + return result; + } + if (resultHandler instanceof DefaultMapResultHandler) { + return ((DefaultMapResultHandler) resultHandler).getMappedResults(); + } + if (resultHandler instanceof DefaultResultHandler) { + return ((DefaultResultHandler) resultHandler).getResultList(); + } + final Object newResult = getResult(resultHandler); + if (null == newResult) { + throw new PageException("无法对ResultHandler:" + resultHandler.getClass() + "对象进行解析获取结果集"); + } + return newResult; + } + + /** + * 自定义结果处理器获取结果集 + * 当结果集处理器存在的时候,需要使用结果集处理器返回的结果。 + * + * @param resultHandler 结果集处理器 + * @return 结果集 + */ + public Object getResult(ResultHandler resultHandler) { + final Method method = getResultMethod(resultHandler.getClass()); + if (null == method) { + return null; + } + try { + return method.invoke(resultHandler); + } + catch (IllegalAccessException | InvocationTargetException e) { + throw new PageException("获取" + resultHandler + "#getResult失败: ", e); + } + } + + /** + * 获取结果集的方法 + * + * @param resultHandlerClass 处理结果集的class + * @return 方法 + */ + public Method getResultMethod(Class resultHandlerClass) { + + Method method = cacheMethod.get(resultHandlerClass); + if (null != method) { + return method; + } + try { + synchronized (cacheMethod) { + method = cacheMethod.get(resultHandlerClass); + if (null != method) { + return method; + } + method = resultHandlerClass.getMethod("getResultList"); + HashMap, Method> tmpMethod = new HashMap, Method>(cacheMethod.size() + 1); + tmpMethod.putAll(cacheMethod); + tmpMethod.put(resultHandlerClass, method); + cacheMethod = tmpMethod; + } + } + catch (NoSuchMethodException e) { + throw new PageException("自定义ResultHandler:[" + resultHandlerClass.getClass() + "]未找到:getResultList方法 ", e); + } + return method; + } + + +}