mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 17:48:25 +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) {
|
public void setConfiguration(Configuration configuration) {
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
DataSource dataSource = configuration.getEnvironment().getDataSource();
|
DataSource dataSource = configuration.getEnvironment().getDataSource();
|
||||||
if (dataSource instanceof FlexDataSource){
|
if (dataSource instanceof FlexDataSource) {
|
||||||
this.dbType = ((FlexDataSource) dataSource).getDefaultDbType();
|
this.dbType = ((FlexDataSource) dataSource).getDefaultDbType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -340,7 +340,7 @@ public class FlexGlobalConfig {
|
|||||||
this.defaultRelationQueryDepth = defaultRelationQueryDepth;
|
this.defaultRelationQueryDepth = defaultRelationQueryDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FlexDataSource getDataSource(){
|
public FlexDataSource getDataSource() {
|
||||||
return (FlexDataSource) getConfiguration().getEnvironment().getDataSource();
|
return (FlexDataSource) getConfiguration().getEnvironment().getDataSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,34 +412,21 @@ public class FlexGlobalConfig {
|
|||||||
return globalConfigs.get(environmentId);
|
return globalConfigs.get(environmentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void setConfig(String id, FlexGlobalConfig config) {
|
|
||||||
setConfig(id, config, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置全局配置
|
* 设置全局配置
|
||||||
*
|
*
|
||||||
* @param id 环境id
|
* @param id 环境id
|
||||||
* @param config 全局配置
|
* @param config 全局配置
|
||||||
* @param copyToDefault 自动指定默认全局配置(在多源时,方便由注解指定默认源)
|
* @param isDefault 自动指定默认全局配置(在多源时,方便由注解指定默认源)
|
||||||
*/
|
*/
|
||||||
public static synchronized void setConfig(String id, FlexGlobalConfig config, boolean copyToDefault) {
|
public static synchronized void setConfig(String id, FlexGlobalConfig config, boolean isDefault) {
|
||||||
//first setConfig,copy the config to default
|
if (isDefault) {
|
||||||
if (copyToDefault && globalConfigs.isEmpty()) {
|
|
||||||
|
|
||||||
defaultConfig.setSqlSessionFactory(config.sqlSessionFactory);
|
defaultConfig.setSqlSessionFactory(config.sqlSessionFactory);
|
||||||
defaultConfig.setDbType(config.dbType);
|
|
||||||
defaultConfig.setConfiguration(config.configuration);
|
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
|
* @param sessionFactory
|
||||||
*/
|
*/
|
||||||
private void initGlobalConfig(Configuration config, SqlSessionFactory sessionFactory) {
|
private void initGlobalConfig(Configuration configuration, SqlSessionFactory sessionFactory) {
|
||||||
String environmentId = config.getEnvironment().getId();
|
String environmentId = configuration.getEnvironment().getId();
|
||||||
FlexGlobalConfig flexGlobalConfig = FlexGlobalConfig.getGlobalConfigs().get(environmentId);
|
|
||||||
flexGlobalConfig.setSqlSessionFactory(sessionFactory);
|
|
||||||
|
|
||||||
|
FlexGlobalConfig globalConfig = FlexGlobalConfig.getConfig(environmentId);
|
||||||
|
if (globalConfig == null){
|
||||||
|
globalConfig = new FlexGlobalConfig();
|
||||||
|
}
|
||||||
|
|
||||||
FlexGlobalConfig.setConfig(environmentId, flexGlobalConfig);
|
globalConfig.setSqlSessionFactory(sessionFactory);
|
||||||
FlexGlobalConfig.setDefaultConfig(flexGlobalConfig);
|
globalConfig.setConfiguration(configuration);
|
||||||
|
|
||||||
|
FlexGlobalConfig.setConfig(environmentId, globalConfig,true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user