Browse Source

重写createCacheKey.

nieqiuqiu 6 years ago
parent
commit
4657d9610f

+ 84 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisBaseExecutor.java

@@ -0,0 +1,84 @@
+package com.baomidou.mybatisplus.core.executor;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+
+import org.apache.ibatis.cache.CacheKey;
+import org.apache.ibatis.executor.BaseExecutor;
+import org.apache.ibatis.executor.ExecutorException;
+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.RowBounds;
+import org.apache.ibatis.transaction.Transaction;
+import org.apache.ibatis.type.TypeHandlerRegistry;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * 重写执行器 {@link org.apache.ibatis.executor.BaseExecutor}
+ *
+ * @author nieqiurong 2019/4/22.
+ */
+public abstract class MybatisBaseExecutor extends BaseExecutor {
+
+    protected MybatisBaseExecutor(Configuration configuration, Transaction transaction) {
+        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;
+    }
+}

+ 8 - 10
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisBatchExecutor.java

@@ -16,7 +16,6 @@
 package com.baomidou.mybatisplus.core.executor;
 
 import org.apache.ibatis.cursor.Cursor;
-import org.apache.ibatis.executor.BaseExecutor;
 import org.apache.ibatis.executor.BatchExecutorException;
 import org.apache.ibatis.executor.BatchResult;
 import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
@@ -39,23 +38,22 @@ import java.util.Collections;
 import java.util.List;
 
 /**
- * 重写执行器
- * {@link org.apache.ibatis.executor.BatchExecutor}
+ * 重写执行器 {@link org.apache.ibatis.executor.BatchExecutor}
  *
  * @author nieqiurong 2019/4/14.
  */
-public class MybatisBatchExecutor extends BaseExecutor {
+public class MybatisBatchExecutor extends MybatisBaseExecutor {
     public static final int BATCH_UPDATE_RETURN_VALUE = Integer.MIN_VALUE + 1002;
-    
+
     private final List<Statement> statementList = new ArrayList<>();
     private final List<BatchResult> batchResultList = new ArrayList<>();
     private String currentSql;
     private MappedStatement currentStatement;
-    
+
     public MybatisBatchExecutor(Configuration configuration, Transaction transaction) {
         super(configuration, transaction);
     }
-    
+
     @Override
     public int doUpdate(MappedStatement ms, Object parameterObject) throws SQLException {
         final Configuration configuration = ms.getConfiguration();
@@ -85,7 +83,7 @@ public class MybatisBatchExecutor extends BaseExecutor {
         handler.batch(stmt);
         return BATCH_UPDATE_RETURN_VALUE;
     }
-    
+
     @Override
     public <E> List<E> doQuery(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
         throws SQLException {
@@ -105,7 +103,7 @@ public class MybatisBatchExecutor extends BaseExecutor {
             closeStatement(stmt);
         }
     }
-    
+
     @Override
     protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
         flushStatements();
@@ -118,7 +116,7 @@ public class MybatisBatchExecutor extends BaseExecutor {
         handler.parameterize(stmt);
         return handler.queryCursor(stmt);
     }
-    
+
     @Override
     public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
         try {

+ 12 - 14
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisReuseExecutor.java

@@ -16,7 +16,6 @@
 package com.baomidou.mybatisplus.core.executor;
 
 import org.apache.ibatis.cursor.Cursor;
-import org.apache.ibatis.executor.BaseExecutor;
 import org.apache.ibatis.executor.BatchResult;
 import org.apache.ibatis.executor.statement.StatementHandler;
 import org.apache.ibatis.logging.Log;
@@ -36,19 +35,18 @@ import java.util.List;
 import java.util.Map;
 
 /**
- * 重写执行器
- * {@link org.apache.ibatis.executor.ReuseExecutor}
+ * 重写执行器 {@link org.apache.ibatis.executor.ReuseExecutor}
  *
  * @author nieqiurong 2019/4/14.
  */
-public class MybatisReuseExecutor extends BaseExecutor {
-    
+public class MybatisReuseExecutor extends MybatisBaseExecutor {
+
     private final Map<String, Statement> statementMap = new HashMap<>();
-    
+
     public MybatisReuseExecutor(Configuration configuration, Transaction transaction) {
         super(configuration, transaction);
     }
-    
+
     @Override
     public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
         Configuration configuration = ms.getConfiguration();
@@ -56,7 +54,7 @@ public class MybatisReuseExecutor extends BaseExecutor {
         Statement stmt = prepareStatement(handler, ms.getStatementLog(), false);
         return stmt == null ? 0 : handler.update(stmt);
     }
-    
+
     @Override
     public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
         Configuration configuration = ms.getConfiguration();
@@ -64,7 +62,7 @@ public class MybatisReuseExecutor extends BaseExecutor {
         Statement stmt = prepareStatement(handler, ms.getStatementLog(), false);
         return stmt == null ? Collections.emptyList() : handler.query(stmt, resultHandler);
     }
-    
+
     @Override
     protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
         Configuration configuration = ms.getConfiguration();
@@ -72,7 +70,7 @@ public class MybatisReuseExecutor extends BaseExecutor {
         Statement stmt = prepareStatement(handler, ms.getStatementLog(), true);
         return handler.queryCursor(stmt);
     }
-    
+
     @Override
     public List<BatchResult> doFlushStatements(boolean isRollback) {
         for (Statement stmt : statementMap.values()) {
@@ -81,7 +79,7 @@ public class MybatisReuseExecutor extends BaseExecutor {
         statementMap.clear();
         return Collections.emptyList();
     }
-    
+
     private Statement prepareStatement(StatementHandler handler, Log statementLog, boolean isCursor) throws SQLException {
         Statement stmt;
         BoundSql boundSql = handler.getBoundSql();
@@ -101,7 +99,7 @@ public class MybatisReuseExecutor extends BaseExecutor {
         handler.parameterize(stmt);
         return stmt;
     }
-    
+
     private boolean hasStatementFor(String sql) {
         try {
             return statementMap.keySet().contains(sql) && !statementMap.get(sql).getConnection().isClosed();
@@ -109,11 +107,11 @@ public class MybatisReuseExecutor extends BaseExecutor {
             return false;
         }
     }
-    
+
     private Statement getStatement(String s) {
         return statementMap.get(s);
     }
-    
+
     private void putStatement(String sql, Statement stmt) {
         statementMap.put(sql, stmt);
     }

+ 9 - 71
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/executor/MybatisSimpleExecutor.java

@@ -15,97 +15,35 @@
  */
 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;
 
 /**
- * 重写执行器
- * {@link org.apache.ibatis.executor.SimpleExecutor}
+ * 重写执行器 {@link org.apache.ibatis.executor.SimpleExecutor}
  *
  * @author nieqiurong 2019/4/14.
  */
-public class MybatisSimpleExecutor extends BaseExecutor {
-    
+public class MybatisSimpleExecutor extends MybatisBaseExecutor {
+
     public MybatisSimpleExecutor(Configuration configuration, Transaction transaction) {
         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;
@@ -118,7 +56,7 @@ public class MybatisSimpleExecutor extends BaseExecutor {
             closeStatement(stmt);
         }
     }
-    
+
     @Override
     public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
         Statement stmt = null;
@@ -131,7 +69,7 @@ public class MybatisSimpleExecutor extends BaseExecutor {
             closeStatement(stmt);
         }
     }
-    
+
     @Override
     protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
         Configuration configuration = ms.getConfiguration();
@@ -144,12 +82,12 @@ public class MybatisSimpleExecutor extends BaseExecutor {
             return null;
         }
     }
-    
+
     @Override
     public List<BatchResult> doFlushStatements(boolean isRollback) {
         return Collections.emptyList();
     }
-    
+
     private Statement prepareStatement(StatementHandler handler, Log statementLog, boolean isCursor) throws SQLException {
         Statement stmt;
         Connection connection = getConnection(statementLog);