From d865f95af7d0e286514c555388f29a8aa6c08715 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Tue, 1 Oct 2024 17:53:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=97=A0=E6=B3=95=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE=E6=BA=90=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=8F=AF=E4=BB=A5=E6=89=8B=E5=8A=A8=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E9=BB=98=E8=AE=A4=E6=95=B0=E6=8D=AE=E6=BA=90=EF=BC=8C?= =?UTF-8?q?=E5=85=B3=E9=97=AD=20https://gitee.com/mybatis-flex/mybatis-fle?= =?UTF-8?q?x/issues/I9VBRJ=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MultiDataSourceAutoConfiguration.java | 51 ++++++++++++------- .../spring/boot/MybatisFlexProperties.java | 42 ++++++++++----- 2 files changed, 62 insertions(+), 31 deletions(-) diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java index 795fe7cb..cea0bf19 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MultiDataSourceAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com). + * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com). *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ import com.mybatisflex.core.datasource.DataSourceBuilder; import com.mybatisflex.core.datasource.DataSourceDecipher; import com.mybatisflex.core.datasource.DataSourceManager; import com.mybatisflex.core.datasource.FlexDataSource; +import com.mybatisflex.core.exception.FlexExceptions; +import com.mybatisflex.core.util.MapUtil; import com.mybatisflex.spring.boot.MybatisFlexProperties.SeataConfig; import com.mybatisflex.spring.datasource.DataSourceAdvice; import io.seata.rm.datasource.DataSourceProxy; @@ -55,6 +57,7 @@ import java.util.Map; "com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure"}) public class MultiDataSourceAutoConfiguration { + private final String master; private final Map> dataSourceProperties; @@ -70,6 +73,7 @@ public class MultiDataSourceAutoConfiguration { dataSourceProperties = properties.getDatasource(); dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable(); seataConfig = properties.getSeataConfig(); + master = properties.getDefaultDatasourceKey(); } @Bean @@ -84,30 +88,43 @@ public class MultiDataSourceAutoConfiguration { DataSourceManager.setDecipher(dataSourceDecipher); } - for (Map.Entry> entry : dataSourceProperties.entrySet()) { - - DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); - DataSourceManager.decryptDataSource(dataSource); - - if (seataConfig != null && seataConfig.isEnable()) { - if (seataConfig.getSeataMode() == MybatisFlexProperties.SeataMode.XA) { - dataSource = new DataSourceProxyXA(dataSource); - } else { - dataSource = new DataSourceProxy(dataSource); - } - } - - if (flexDataSource == null) { - flexDataSource = new FlexDataSource(entry.getKey(), dataSource, false); + if (master != null) { + Map map = dataSourceProperties.remove(master); + if (map != null) { + flexDataSource = addDataSource(MapUtil.entry(master, map), flexDataSource); } else { - flexDataSource.addDataSource(entry.getKey(), dataSource, false); + throw FlexExceptions.wrap("没有找到默认数据源 \"%s\" 对应的配置,请检查您的多数据源配置。", master); } } + + for (Map.Entry> entry : dataSourceProperties.entrySet()) { + flexDataSource = addDataSource(entry, flexDataSource); + } } return flexDataSource; } + private FlexDataSource addDataSource(Map.Entry> entry, FlexDataSource flexDataSource) { + DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); + DataSourceManager.decryptDataSource(dataSource); + + if (seataConfig != null && seataConfig.isEnable()) { + if (seataConfig.getSeataMode() == MybatisFlexProperties.SeataMode.XA) { + dataSource = new DataSourceProxyXA(dataSource); + } else { + dataSource = new DataSourceProxy(dataSource); + } + } + + if (flexDataSource == null) { + flexDataSource = new FlexDataSource(entry.getKey(), dataSource, false); + } else { + flexDataSource.addDataSource(entry.getKey(), dataSource, false); + } + return flexDataSource; + } + /** * {@link com.mybatisflex.annotation.UseDataSource} 注解切换数据源切面。 diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java index 0b59698d..7fbf5843 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java @@ -1,17 +1,17 @@ /* - * Copyright 2015-2022 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.mybatisflex.spring.boot; @@ -21,7 +21,11 @@ import org.apache.ibatis.io.VFS; import org.apache.ibatis.logging.Log; import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.scripting.LanguageDriver; -import org.apache.ibatis.session.*; +import org.apache.ibatis.session.AutoMappingBehavior; +import org.apache.ibatis.session.AutoMappingUnknownColumnBehavior; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.LocalCacheScope; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -52,6 +56,8 @@ public class MybatisFlexProperties { private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); + private String defaultDatasourceKey; + /** *

多数据源的配置。 * @@ -160,6 +166,14 @@ public class MybatisFlexProperties { this.adminConfig = adminConfig; } + public String getDefaultDatasourceKey() { + return defaultDatasourceKey; + } + + public void setDefaultDatasourceKey(String defaultDatasourceKey) { + this.defaultDatasourceKey = defaultDatasourceKey; + } + /** * @since 1.1.0 */