|
@@ -16,7 +16,6 @@
|
|
package com.baomidou.mybatisplus.core.toolkit;
|
|
package com.baomidou.mybatisplus.core.toolkit;
|
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
import com.baomidou.mybatisplus.annotation.IdType;
|
|
-import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
|
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
|
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
|
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
|
|
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.Configuration;
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
|
|
|
|
|
+import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Mybatis全局缓存工具类
|
|
* Mybatis全局缓存工具类
|
|
@@ -37,6 +38,11 @@ import java.util.Set;
|
|
*/
|
|
*/
|
|
public class GlobalConfigUtils {
|
|
public class GlobalConfigUtils {
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 缓存全局信息
|
|
|
|
+ */
|
|
|
|
+ private static final Map<String, GlobalConfig> GLOBAL_CONFIG = new ConcurrentHashMap<>();
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取当前的SqlSessionFactory
|
|
* 获取当前的SqlSessionFactory
|
|
*
|
|
*
|
|
@@ -55,6 +61,20 @@ public class GlobalConfigUtils {
|
|
return new GlobalConfig().setDbConfig(new GlobalConfig.DbConfig());
|
|
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 (统一所有入口)
|
|
* 获取MybatisGlobalConfig (统一所有入口)
|
|
*
|
|
*
|
|
@@ -62,7 +82,8 @@ public class GlobalConfigUtils {
|
|
*/
|
|
*/
|
|
public static GlobalConfig getGlobalConfig(Configuration configuration) {
|
|
public static GlobalConfig getGlobalConfig(Configuration configuration) {
|
|
Assert.notNull(configuration, "Error: You need Initialize MybatisConfiguration !");
|
|
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) {
|
|
public static IKeyGenerator getKeyGenerator(Configuration configuration) {
|