Browse Source

executor类型选择

miemie 5 years ago
parent
commit
2bcbae788b

+ 34 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisConfiguration.java

@@ -16,11 +16,16 @@
 package com.baomidou.mybatisplus.core;
 
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
+import com.baomidou.mybatisplus.core.executor.MybatisBatchExecutor;
+import com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor;
+import com.baomidou.mybatisplus.core.executor.MybatisReuseExecutor;
+import com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor;
 import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
 import lombok.Getter;
 import lombok.Setter;
 import org.apache.ibatis.binding.MapperRegistry;
 import org.apache.ibatis.cache.Cache;
+import org.apache.ibatis.executor.Executor;
 import org.apache.ibatis.executor.keygen.KeyGenerator;
 import org.apache.ibatis.logging.Log;
 import org.apache.ibatis.logging.LogFactory;
@@ -31,7 +36,9 @@ import org.apache.ibatis.mapping.ResultMap;
 import org.apache.ibatis.parsing.XNode;
 import org.apache.ibatis.scripting.LanguageDriver;
 import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.session.ExecutorType;
 import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.transaction.Transaction;
 
 import java.util.Collection;
 import java.util.HashMap;
@@ -69,6 +76,10 @@ public class MybatisConfiguration extends Configuration {
     @Getter
     private boolean useGeneratedShortKey = true;
 
+    @Setter
+    @Getter
+    private boolean useNewExecutor = true;
+
     public MybatisConfiguration(Environment environment) {
         this();
         this.environment = environment;
@@ -297,6 +308,27 @@ public class MybatisConfiguration extends Configuration {
         return mappedStatements.containsKey(statementName);
     }
 
+    @Override
+    public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
+        if (useNewExecutor) {
+            executorType = executorType == null ? defaultExecutorType : executorType;
+            executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
+            Executor executor;
+            if (ExecutorType.BATCH == executorType) {
+                executor = new MybatisBatchExecutor(this, transaction);
+            } else if (ExecutorType.REUSE == executorType) {
+                executor = new MybatisReuseExecutor(this, transaction);
+            } else {
+                executor = new MybatisSimpleExecutor(this, transaction);
+            }
+            if (cacheEnabled) {
+                executor = new MybatisCachingExecutor(executor);
+            }
+            executor = (Executor) interceptorChain.pluginAll(executor);
+            return executor;
+        }
+        return super.newExecutor(transaction, executorType);
+    }
 
     // Slow but a one time cost. A better solution is welcome.
     protected void checkGloballyForDiscriminatedNestedResultMaps(ResultMap rm) {
@@ -342,10 +374,12 @@ public class MybatisConfiguration extends Configuration {
             super();
             this.name = name;
         }
+
         /**
          * Assign a function for producing a conflict error message when contains value with the same key.
          * <p>
          * function arguments are 1st is saved value and 2nd is target value.
+         *
          * @param conflictMessageProducer A function for producing a conflict error message
          * @return a conflict error message
          * @since 3.5.0

+ 3 - 0
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/MybatisXMLConfigBuilder.java

@@ -271,6 +271,9 @@ public class MybatisXMLConfigBuilder extends BaseBuilder {
         configuration.setLogPrefix(props.getProperty("logPrefix"));
         configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
         configuration.setShrinkWhitespacesInSql(booleanValueOf(props.getProperty("shrinkWhitespacesInSql"), false));
+        // TODO MybatisConfiguration 独有的属性
+        ((MybatisConfiguration) configuration).setUseNewExecutor(booleanValueOf(props.getProperty("useNewExecutor"), true));
+        ((MybatisConfiguration) configuration).setUseGeneratedShortKey(booleanValueOf(props.getProperty("useGeneratedShortKey"), true));
     }
 
     private void environmentsElement(XNode context) throws Exception {

+ 1 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/BaseDbTest.java

@@ -62,6 +62,7 @@ public abstract class BaseDbTest<T> extends TypeReference<T> {
         MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
         Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
         MybatisConfiguration configuration = new MybatisConfiguration(environment);
+        configuration.setUseNewExecutor(false);
         GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);
         configuration.setLogImpl(Slf4jImpl.class);
         if (StringUtils.isNotBlank(mapperXml)) {

+ 1 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/cache/CacheConfig.java

@@ -38,6 +38,7 @@ public class CacheConfig {
         MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
         sqlSessionFactory.setDataSource(dataSource);
         MybatisConfiguration configuration = new MybatisConfiguration();
+        configuration.setUseNewExecutor(false);
         configuration.setJdbcTypeForNull(JdbcType.NULL);
         configuration.setMapUnderscoreToCamelCase(true);
         configuration.setDefaultExecutorType(ExecutorType.REUSE);