Skip to content

Commit 2bcbae7

Browse files
committed
executor类型选择
1 parent f9aca83 commit 2bcbae7

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616
package com.baomidou.mybatisplus.core;
1717

1818
import com.baomidou.mybatisplus.core.config.GlobalConfig;
19+
import com.baomidou.mybatisplus.core.executor.MybatisBatchExecutor;
20+
import com.baomidou.mybatisplus.core.executor.MybatisCachingExecutor;
21+
import com.baomidou.mybatisplus.core.executor.MybatisReuseExecutor;
22+
import com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor;
1923
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
2024
import lombok.Getter;
2125
import lombok.Setter;
2226
import org.apache.ibatis.binding.MapperRegistry;
2327
import org.apache.ibatis.cache.Cache;
28+
import org.apache.ibatis.executor.Executor;
2429
import org.apache.ibatis.executor.keygen.KeyGenerator;
2530
import org.apache.ibatis.logging.Log;
2631
import org.apache.ibatis.logging.LogFactory;
@@ -31,7 +36,9 @@
3136
import org.apache.ibatis.parsing.XNode;
3237
import org.apache.ibatis.scripting.LanguageDriver;
3338
import org.apache.ibatis.session.Configuration;
39+
import org.apache.ibatis.session.ExecutorType;
3440
import org.apache.ibatis.session.SqlSession;
41+
import org.apache.ibatis.transaction.Transaction;
3542

3643
import java.util.Collection;
3744
import java.util.HashMap;
@@ -69,6 +76,10 @@ public class MybatisConfiguration extends Configuration {
6976
@Getter
7077
private boolean useGeneratedShortKey = true;
7178

79+
@Setter
80+
@Getter
81+
private boolean useNewExecutor = true;
82+
7283
public MybatisConfiguration(Environment environment) {
7384
this();
7485
this.environment = environment;
@@ -297,6 +308,27 @@ public boolean hasStatement(String statementName, boolean validateIncompleteStat
297308
return mappedStatements.containsKey(statementName);
298309
}
299310

311+
@Override
312+
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
313+
if (useNewExecutor) {
314+
executorType = executorType == null ? defaultExecutorType : executorType;
315+
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
316+
Executor executor;
317+
if (ExecutorType.BATCH == executorType) {
318+
executor = new MybatisBatchExecutor(this, transaction);
319+
} else if (ExecutorType.REUSE == executorType) {
320+
executor = new MybatisReuseExecutor(this, transaction);
321+
} else {
322+
executor = new MybatisSimpleExecutor(this, transaction);
323+
}
324+
if (cacheEnabled) {
325+
executor = new MybatisCachingExecutor(executor);
326+
}
327+
executor = (Executor) interceptorChain.pluginAll(executor);
328+
return executor;
329+
}
330+
return super.newExecutor(transaction, executorType);
331+
}
300332

301333
// Slow but a one time cost. A better solution is welcome.
302334
protected void checkGloballyForDiscriminatedNestedResultMaps(ResultMap rm) {
@@ -342,10 +374,12 @@ public StrictMap(String name) {
342374
super();
343375
this.name = name;
344376
}
377+
345378
/**
346379
* Assign a function for producing a conflict error message when contains value with the same key.
347380
* <p>
348381
* function arguments are 1st is saved value and 2nd is target value.
382+
*
349383
* @param conflictMessageProducer A function for producing a conflict error message
350384
* @return a conflict error message
351385
* @since 3.5.0

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

+3
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ private void settingsElement(Properties props) {
271271
configuration.setLogPrefix(props.getProperty("logPrefix"));
272272
configuration.setConfigurationFactory(resolveClass(props.getProperty("configurationFactory")));
273273
configuration.setShrinkWhitespacesInSql(booleanValueOf(props.getProperty("shrinkWhitespacesInSql"), false));
274+
// TODO MybatisConfiguration 独有的属性
275+
((MybatisConfiguration) configuration).setUseNewExecutor(booleanValueOf(props.getProperty("useNewExecutor"), true));
276+
((MybatisConfiguration) configuration).setUseGeneratedShortKey(booleanValueOf(props.getProperty("useGeneratedShortKey"), true));
274277
}
275278

276279
private void environmentsElement(XNode context) throws Exception {

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

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public BaseDbTest() {
6262
MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
6363
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
6464
MybatisConfiguration configuration = new MybatisConfiguration(environment);
65+
configuration.setUseNewExecutor(false);
6566
GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);
6667
configuration.setLogImpl(Slf4jImpl.class);
6768
if (StringUtils.isNotBlank(mapperXml)) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
3838
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
3939
sqlSessionFactory.setDataSource(dataSource);
4040
MybatisConfiguration configuration = new MybatisConfiguration();
41+
configuration.setUseNewExecutor(false);
4142
configuration.setJdbcTypeForNull(JdbcType.NULL);
4243
configuration.setMapUnderscoreToCamelCase(true);
4344
configuration.setDefaultExecutorType(ExecutorType.REUSE);

0 commit comments

Comments
 (0)