Ver Fonte

GlobalConfig 位移

miemie há 5 anos atrás
pai
commit
334da55c62

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

@@ -21,8 +21,6 @@ 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.executor.Executor;
 import org.apache.ibatis.logging.Log;
@@ -54,9 +52,23 @@ public class MybatisConfiguration extends Configuration {
         this.environment = environment;
     }
 
-    @Setter
-    @Getter
-    private GlobalConfig globalConfig = GlobalConfigUtils.defaults();
+    /**
+     * @return GlobalConfig
+     * @deprecated 3.3.3 please use {@link GlobalConfigUtils#getGlobalConfig(Configuration)}
+     */
+    @Deprecated
+    public GlobalConfig getGlobalConfig() {
+        return GlobalConfigUtils.getGlobalConfig(this);
+    }
+
+    /**
+     * @param globalConfig GlobalConfig
+     * @deprecated 3.3.3 please use {@link GlobalConfigUtils#setGlobalConfig(Configuration, GlobalConfig)}
+     */
+    @Deprecated
+    public void setGlobalConfig(GlobalConfig globalConfig) {
+        GlobalConfigUtils.setGlobalConfig(this, globalConfig);
+    }
 
     /**
      * 初始化调用

+ 23 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/GlobalConfigUtils.java

@@ -16,7 +16,6 @@
 package com.baomidou.mybatisplus.core.toolkit;
 
 import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.core.MybatisConfiguration;
 import com.baomidou.mybatisplus.core.config.GlobalConfig;
 import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
 import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
@@ -26,8 +25,10 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import org.apache.ibatis.session.Configuration;
 import org.apache.ibatis.session.SqlSessionFactory;
 
+import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Mybatis全局缓存工具类
@@ -37,6 +38,11 @@ import java.util.Set;
  */
 public class GlobalConfigUtils {
 
+    /**
+     * 缓存全局信息
+     */
+    private static final Map<String, GlobalConfig> GLOBAL_CONFIG = new ConcurrentHashMap<>();
+
     /**
      * 获取当前的SqlSessionFactory
      *
@@ -55,6 +61,20 @@ public class GlobalConfigUtils {
         return new GlobalConfig().setDbConfig(new GlobalConfig.DbConfig());
     }
 
+    /**
+     * <p>
+     * 设置全局设置(以configuration地址值作为Key)
+     * <p/>
+     *
+     * @param configuration Mybatis 容器配置对象
+     * @param globalConfig  全局配置
+     */
+    public static void setGlobalConfig(Configuration configuration, GlobalConfig globalConfig) {
+        Assert.isTrue(configuration != null && globalConfig != null, "Error: Could not setGlobalConfig");
+        // 设置全局设置
+        GLOBAL_CONFIG.putIfAbsent(Integer.toHexString(configuration.hashCode()), globalConfig);
+    }
+
     /**
      * 获取MybatisGlobalConfig (统一所有入口)
      *
@@ -62,7 +82,8 @@ public class GlobalConfigUtils {
      */
     public static GlobalConfig getGlobalConfig(Configuration configuration) {
         Assert.notNull(configuration, "Error: You need Initialize MybatisConfiguration !");
-        return ((MybatisConfiguration) configuration).getGlobalConfig();
+        final String key = Integer.toHexString(configuration.hashCode());
+        return GLOBAL_CONFIG.computeIfAbsent(key, k -> defaults());
     }
 
     public static IKeyGenerator getKeyGenerator(Configuration configuration) {

+ 1 - 1
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/spring/MybatisSqlSessionFactoryBean.java

@@ -469,7 +469,7 @@ public class MybatisSqlSessionFactoryBean implements FactoryBean<SqlSessionFacto
         this.globalConfig.setDbConfig(Optional.ofNullable(this.globalConfig.getDbConfig()).orElseGet(GlobalConfig.DbConfig::new));
 
         // TODO 初始化 id-work 以及 打印骚东西
-        targetConfiguration.setGlobalConfig(this.globalConfig);
+        GlobalConfigUtils.setGlobalConfig(targetConfiguration, this.globalConfig);
 
         // TODO 自定义枚举类扫描处理
         if (hasLength(this.typeEnumsPackage)) {