|
@@ -1,6 +1,9 @@
|
|
|
package com.baomidou.mybatisplus.test.h2.tenant;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.executor.MybatisBatchExecutor;
|
|
|
+import com.baomidou.mybatisplus.core.executor.MybatisReuseExecutor;
|
|
|
+import com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor;
|
|
|
import com.baomidou.mybatisplus.core.parser.ISqlParser;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
|
|
@@ -8,8 +11,10 @@ import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
|
|
|
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.LongValue;
|
|
|
+import org.apache.ibatis.executor.Executor;
|
|
|
import org.apache.ibatis.session.ExecutorType;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
+import org.apache.ibatis.transaction.Transaction;
|
|
|
import org.apache.ibatis.type.EnumOrdinalTypeHandler;
|
|
|
import org.apache.ibatis.type.JdbcType;
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
@@ -34,15 +39,6 @@ public class TenantConfig {
|
|
|
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
|
|
|
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
|
|
|
sqlSessionFactory.setDataSource(dataSource);
|
|
|
- MybatisConfiguration configuration = new MybatisConfiguration();
|
|
|
- configuration.setJdbcTypeForNull(JdbcType.NULL);
|
|
|
- configuration.setMapUnderscoreToCamelCase(true);
|
|
|
- configuration.setDefaultExecutorType(ExecutorType.REUSE);
|
|
|
- configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
|
|
|
- configuration.setCacheEnabled(true);
|
|
|
- sqlSessionFactory.setConfiguration(configuration);
|
|
|
- PaginationInterceptor pagination = new PaginationInterceptor();
|
|
|
- List<ISqlParser> sqlParserList = new ArrayList<>();
|
|
|
TenantSqlParser tenantSqlParser = new TenantSqlParser();
|
|
|
tenantSqlParser.setTenantHandler(new TenantHandler() {
|
|
|
@Override
|
|
@@ -60,6 +56,34 @@ public class TenantConfig {
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
+ MybatisConfiguration configuration = new MybatisConfiguration() {
|
|
|
+ @Override
|
|
|
+ public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
|
|
|
+ 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 CustomCacheExecutor(executor, tenantSqlParser.getTenantHandler());
|
|
|
+ }
|
|
|
+ executor = (Executor) interceptorChain.pluginAll(executor);
|
|
|
+ return executor;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ configuration.setJdbcTypeForNull(JdbcType.NULL);
|
|
|
+ configuration.setMapUnderscoreToCamelCase(true);
|
|
|
+ configuration.setDefaultExecutorType(ExecutorType.REUSE);
|
|
|
+ configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
|
|
|
+ configuration.setCacheEnabled(true);
|
|
|
+ sqlSessionFactory.setConfiguration(configuration);
|
|
|
+ PaginationInterceptor pagination = new PaginationInterceptor();
|
|
|
+ List<ISqlParser> sqlParserList = new ArrayList<>();
|
|
|
sqlParserList.add(tenantSqlParser);
|
|
|
pagination.setSqlParserList(sqlParserList);
|
|
|
sqlSessionFactory.setPlugins(pagination);
|