浏览代码

Revert "优化分页缓存问题."

This reverts commit 5dc2943967666cc4088090039e550f4118310cb8.

还是不能破坏原生缓存,换个支持方式.
聂秋秋 6 年之前
父节点
当前提交
8da35f3276

+ 61 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisSimpleExecutor.java

@@ -15,23 +15,32 @@
  */
 package com.baomidou.mybatisplus.core.executor;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import org.apache.ibatis.cache.CacheKey;
 import org.apache.ibatis.cursor.Cursor;
 import org.apache.ibatis.executor.BaseExecutor;
 import org.apache.ibatis.executor.BatchResult;
+import org.apache.ibatis.executor.ExecutorException;
 import org.apache.ibatis.executor.statement.StatementHandler;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.mapping.BoundSql;
 import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.ParameterMapping;
+import org.apache.ibatis.mapping.ParameterMode;
+import org.apache.ibatis.reflection.MetaObject;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.RowBounds;
 import org.apache.ibatis.transaction.Transaction;
+import org.apache.ibatis.type.TypeHandlerRegistry;
 
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 
 /**
  * 重写执行器
@@ -45,6 +54,58 @@ public class MybatisSimpleExecutor extends BaseExecutor {
         super(configuration, transaction);
     }
     
+    @Override
+    public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
+        if (super.isClosed()) {
+            throw new ExecutorException("Executor was closed.");
+        }
+        CacheKey cacheKey = new CacheKey();
+        cacheKey.update(ms.getId());
+        Object offset = rowBounds.getOffset();
+        Object limit = rowBounds.getLimit();
+        if (parameterObject instanceof Map) {
+            Map<?, ?> parameterMap = (Map<?, ?>) parameterObject;
+            Optional<? extends Map.Entry<?, ?>> optional = parameterMap.entrySet().stream().filter(entry -> entry.getValue() instanceof IPage).findFirst();
+            if (optional.isPresent()) {
+                IPage<?> page = (IPage) optional.get().getValue();
+                offset = page.getCurrent();
+                limit = page.getTotal();
+            }
+        } else if (parameterObject instanceof IPage) {
+            IPage<?> page = (IPage) parameterObject;
+            offset = page.getCurrent();
+            limit = page.getTotal();
+        }
+        cacheKey.update(offset);
+        cacheKey.update(limit);
+        cacheKey.update(boundSql.getSql());
+        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
+        TypeHandlerRegistry typeHandlerRegistry = ms.getConfiguration().getTypeHandlerRegistry();
+        // mimic DefaultParameterHandler logic
+        for (ParameterMapping parameterMapping : parameterMappings) {
+            if (parameterMapping.getMode() != ParameterMode.OUT) {
+                Object value;
+                String propertyName = parameterMapping.getProperty();
+                if (boundSql.hasAdditionalParameter(propertyName)) {
+                    value = boundSql.getAdditionalParameter(propertyName);
+                } else if (parameterObject == null) {
+                    value = null;
+                } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
+                    value = parameterObject;
+                } else {
+                    MetaObject metaObject = configuration.newMetaObject(parameterObject);
+                    value = metaObject.getValue(propertyName);
+                }
+                cacheKey.update(value);
+            }
+        }
+        if (configuration.getEnvironment() != null) {
+            // issue #176
+            cacheKey.update(configuration.getEnvironment().getId());
+        }
+        return cacheKey;
+    }
+    
     @Override
     public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
         Statement stmt = null;

+ 8 - 22
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/AbstractMethod.java

@@ -20,7 +20,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.core.toolkit.Constants;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
-
 import org.apache.ibatis.builder.MapperBuilderAssistant;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
 import org.apache.ibatis.executor.keygen.NoKeyGenerator;
@@ -238,26 +237,18 @@ public abstract class AbstractMethod implements Constants {
      */
     protected MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
                                                        Class<?> resultType, TableInfo table) {
-        return this.addSelectMappedStatement(mapperClass, id, sqlSource, resultType, table, true);
-    }
-
-    /**
-     * 查询
-     */
-    protected MappedStatement addSelectMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
-                                                       Class<?> resultType, TableInfo table, boolean useCache) {
         if (null != table) {
             String resultMap = table.getResultMap();
             if (null != resultMap) {
                 /* 返回 resultMap 映射结果集 */
                 return addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null,
-                    resultMap, null, new NoKeyGenerator(), null, null, useCache);
+                    resultMap, null, new NoKeyGenerator(), null, null);
             }
         }
 
         /* 普通查询 */
         return addMappedStatement(mapperClass, id, sqlSource, SqlCommandType.SELECT, null,
-            null, resultType, new NoKeyGenerator(), null, null, useCache);
+            null, resultType, new NoKeyGenerator(), null, null);
     }
 
     /**
@@ -294,24 +285,19 @@ public abstract class AbstractMethod implements Constants {
                                                  SqlCommandType sqlCommandType, Class<?> parameterType,
                                                  String resultMap, Class<?> resultType, KeyGenerator keyGenerator,
                                                  String keyProperty, String keyColumn) {
-        return this.addMappedStatement(mapperClass, id, sqlSource, sqlCommandType, parameterType, resultMap, resultType, keyGenerator, keyProperty, keyColumn, true);
-    }
-
-    /**
-     * 添加 MappedStatement 到 Mybatis 容器
-     */
-    protected MappedStatement addMappedStatement(Class<?> mapperClass, String id, SqlSource sqlSource,
-                                                 SqlCommandType sqlCommandType, Class<?> parameterType,
-                                                 String resultMap, Class<?> resultType, KeyGenerator keyGenerator,
-                                                 String keyProperty, String keyColumn, boolean useCache) {
         String statementName = mapperClass.getName() + DOT + id;
         if (hasMappedStatement(statementName)) {
             logger.warn(LEFT_SQ_BRACKET + statementName + "] Has been loaded by XML or SqlProvider or Mybatis's Annotation, so ignoring this injection for [" + getClass() + RIGHT_SQ_BRACKET);
             return null;
         }
+        /* 缓存逻辑处理 */
+        boolean isSelect = false;
+        if (sqlCommandType == SqlCommandType.SELECT) {
+            isSelect = true;
+        }
         return builderAssistant.addMappedStatement(id, sqlSource, StatementType.PREPARED, sqlCommandType,
             null, null, null, parameterType, resultMap, resultType,
-            null, !useCache, useCache, false, keyGenerator, keyProperty, keyColumn,
+            null, !isSelect, isSelect, false, keyGenerator, keyProperty, keyColumn,
             configuration.getDatabaseId(), languageDriver, null);
     }
 

+ 1 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectMapsPage.java

@@ -18,7 +18,6 @@ package com.baomidou.mybatisplus.core.injector.methods;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
-
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
@@ -38,6 +37,6 @@ public class SelectMapsPage extends AbstractMethod {
         String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
             tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class, tableInfo, false);
+        return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class, tableInfo);
     }
 }

+ 1 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectPage.java

@@ -18,7 +18,6 @@ package com.baomidou.mybatisplus.core.injector.methods;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
-
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
@@ -36,6 +35,6 @@ public class SelectPage extends AbstractMethod {
         String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, true),
             tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo));
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
-        return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo, false);
+        return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
     }
 }