mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
refactor: optimize FlexGlobalConfig.java
This commit is contained in:
parent
10986e357a
commit
8c39797eac
@ -110,7 +110,7 @@ public class FlexGlobalConfig {
|
||||
public void setConfiguration(Configuration configuration) {
|
||||
this.configuration = configuration;
|
||||
DataSource dataSource = configuration.getEnvironment().getDataSource();
|
||||
if (dataSource instanceof FlexDataSource){
|
||||
if (dataSource instanceof FlexDataSource) {
|
||||
this.dbType = ((FlexDataSource) dataSource).getDefaultDbType();
|
||||
}
|
||||
}
|
||||
@ -340,7 +340,7 @@ public class FlexGlobalConfig {
|
||||
this.defaultRelationQueryDepth = defaultRelationQueryDepth;
|
||||
}
|
||||
|
||||
public FlexDataSource getDataSource(){
|
||||
public FlexDataSource getDataSource() {
|
||||
return (FlexDataSource) getConfiguration().getEnvironment().getDataSource();
|
||||
}
|
||||
|
||||
@ -412,34 +412,21 @@ public class FlexGlobalConfig {
|
||||
return globalConfigs.get(environmentId);
|
||||
}
|
||||
|
||||
public static synchronized void setConfig(String id, FlexGlobalConfig config) {
|
||||
setConfig(id, config, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局配置
|
||||
*
|
||||
* @param id 环境id
|
||||
* @param config 全局配置
|
||||
* @param copyToDefault 自动指定默认全局配置(在多源时,方便由注解指定默认源)
|
||||
* @param id 环境id
|
||||
* @param config 全局配置
|
||||
* @param isDefault 自动指定默认全局配置(在多源时,方便由注解指定默认源)
|
||||
*/
|
||||
public static synchronized void setConfig(String id, FlexGlobalConfig config, boolean copyToDefault) {
|
||||
//first setConfig,copy the config to default
|
||||
if (copyToDefault && globalConfigs.isEmpty()) {
|
||||
|
||||
public static synchronized void setConfig(String id, FlexGlobalConfig config, boolean isDefault) {
|
||||
if (isDefault) {
|
||||
defaultConfig.setSqlSessionFactory(config.sqlSessionFactory);
|
||||
defaultConfig.setDbType(config.dbType);
|
||||
defaultConfig.setConfiguration(config.configuration);
|
||||
|
||||
if (defaultConfig.getKeyConfig() == null
|
||||
&& config.keyConfig != null) {
|
||||
defaultConfig.setKeyConfig(config.keyConfig);
|
||||
}
|
||||
|
||||
config = defaultConfig;
|
||||
}
|
||||
|
||||
globalConfigs.put(id, config);
|
||||
globalConfigs.put(id, isDefault ? defaultConfig : config);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -109,17 +109,21 @@ public class FlexSqlSessionFactoryBuilder extends SqlSessionFactoryBuilder {
|
||||
/**
|
||||
* 设置全局配置
|
||||
*
|
||||
* @param config
|
||||
* @param configuration
|
||||
* @param sessionFactory
|
||||
*/
|
||||
private void initGlobalConfig(Configuration config, SqlSessionFactory sessionFactory) {
|
||||
String environmentId = config.getEnvironment().getId();
|
||||
FlexGlobalConfig flexGlobalConfig = FlexGlobalConfig.getGlobalConfigs().get(environmentId);
|
||||
flexGlobalConfig.setSqlSessionFactory(sessionFactory);
|
||||
private void initGlobalConfig(Configuration configuration, SqlSessionFactory sessionFactory) {
|
||||
String environmentId = configuration.getEnvironment().getId();
|
||||
|
||||
FlexGlobalConfig globalConfig = FlexGlobalConfig.getConfig(environmentId);
|
||||
if (globalConfig == null){
|
||||
globalConfig = new FlexGlobalConfig();
|
||||
}
|
||||
|
||||
FlexGlobalConfig.setConfig(environmentId, flexGlobalConfig);
|
||||
FlexGlobalConfig.setDefaultConfig(flexGlobalConfig);
|
||||
globalConfig.setSqlSessionFactory(sessionFactory);
|
||||
globalConfig.setConfiguration(configuration);
|
||||
|
||||
FlexGlobalConfig.setConfig(environmentId, globalConfig,true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user