From 65640816870b9896046e68d85a53b19172143e8e Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 24 Apr 2023 17:14:24 +0800 Subject: [PATCH 1/3] =?UTF-8?q?MybatisAdapterFlex=20=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=97=B6=EF=BC=8C=E7=BB=91=E5=AE=9A=20globalConfig::s?= =?UTF-8?q?qlSessionFactory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/solon/integration/MybatisAdapterFlex.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java index afa3a7d4..1afb8c5b 100644 --- a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java +++ b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java @@ -32,6 +32,8 @@ public class MybatisAdapterFlex extends MybatisAdapterDefault { dsWrap.context().getBeanAsync(FlexSqlSessionFactoryBuilder.class, bean -> { factoryBuilderPlus = bean; }); + + globalConfig.setSqlSessionFactory(getFactory()); } protected MybatisAdapterFlex(BeanWrap dsWrap, Props dsProps) { From 2a841b5f112a5a31c2e3a13847df874a70abd66b Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 24 Apr 2023 17:59:48 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=B0=83=E6=95=B4=20FlexGlobalConfig?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E6=9B=B4=E8=87=AA=E7=94=B1=E7=9A=84?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/core/FlexGlobalConfig.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index 3a14b96e..74cd0e4a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -173,7 +173,7 @@ public class FlexGlobalConfig { } public void setNormalValueOfLogicDelete(Object normalValueOfLogicDelete) { - if (normalValueOfLogicDelete == null){ + if (normalValueOfLogicDelete == null) { throw new NullPointerException("normalValueOfLogicDelete can not be null."); } this.normalValueOfLogicDelete = normalValueOfLogicDelete; @@ -184,7 +184,7 @@ public class FlexGlobalConfig { } public void setDeletedValueOfLogicDelete(Object deletedValueOfLogicDelete) { - if (deletedValueOfLogicDelete == null){ + if (deletedValueOfLogicDelete == null) { throw new NullPointerException("deletedValueOfLogicDelete can not be null."); } this.deletedValueOfLogicDelete = deletedValueOfLogicDelete; @@ -241,6 +241,30 @@ public class FlexGlobalConfig { return defaultConfig; } + /** + * 指定默认全局配置(允许手动。在多源时,方便由注解指定默认源) + * + *
+     * @Configuration
+     * public class Config{
+     *     @Bean(value = "db1", typed = true) //默认
+     *     public DataSource db1(@Inject("${demo.db1}") HikariDataSource ds) {
+     *         return ds;
+     *     }
+     *
+     *     @Bean("db2")
+     *     public DataSource db1(@Inject("${demo.db2}") HikariDataSource ds) {
+     *         return ds;
+     *     }
+     * }
+     * 
+ * + * @param config 全局配置 + */ + public static void setDefaultConfig(FlexGlobalConfig config) { + defaultConfig = config; + } + public static FlexGlobalConfig getConfig(Configuration configuration) { return getConfig(configuration.getEnvironment().getId()); } @@ -249,9 +273,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 autoDefault 自动指定默认全局配置(在多源时,方便由注解指定默认源) + */ + public static synchronized void setConfig(String id, FlexGlobalConfig config, boolean autoDefault) { //first setConfig,copy the config to default - if (globalConfigs.isEmpty()) { + if (autoDefault && globalConfigs.isEmpty()) { defaultConfig.setSqlSessionFactory(config.sqlSessionFactory); defaultConfig.setDbType(config.dbType); @@ -267,6 +303,4 @@ public class FlexGlobalConfig { globalConfigs.put(id, config); } - - -} +} \ No newline at end of file From 7da9aaaa63a7cd314c5e1843c20ca5d4f5ebda01 Mon Sep 17 00:00:00 2001 From: noear Date: Mon, 24 Apr 2023 18:00:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=B0=83=E6=95=B4=20MybatisAdapterFlex=20?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=EF=BC=8C=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=20globalConfig::sqlSessionFactory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../solon/integration/MybatisAdapterFlex.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java index 1afb8c5b..7ba7a787 100644 --- a/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java +++ b/mybatis-flex-solon-plugin/src/main/java/com/mybatisflex/solon/integration/MybatisAdapterFlex.java @@ -33,7 +33,7 @@ public class MybatisAdapterFlex extends MybatisAdapterDefault { factoryBuilderPlus = bean; }); - globalConfig.setSqlSessionFactory(getFactory()); + initAfter(dsWrap); } protected MybatisAdapterFlex(BeanWrap dsWrap, Props dsProps) { @@ -44,6 +44,16 @@ public class MybatisAdapterFlex extends MybatisAdapterDefault { dsWrap.context().getBeanAsync(FlexSqlSessionFactoryBuilder.class, bean -> { factoryBuilderPlus = bean; }); + + initAfter(dsWrap); + } + + protected void initAfter(BeanWrap dsWrap) { + globalConfig.setSqlSessionFactory(getFactory()); + + if (dsWrap.typed()) { + FlexGlobalConfig.setDefaultConfig(globalConfig); + } } @Override @@ -53,8 +63,6 @@ public class MybatisAdapterFlex extends MybatisAdapterDefault { @Override protected void initConfiguration(Environment environment) { - - //for configuration section config = new FlexConfiguration(environment); @@ -74,7 +82,8 @@ public class MybatisAdapterFlex extends MybatisAdapterDefault { Utils.injectProperties(globalConfig, globalProps); } globalConfig.setConfiguration(config); - FlexGlobalConfig.setConfig(environment.getId(), globalConfig); + + FlexGlobalConfig.setConfig(environment.getId(), globalConfig, false); } /**