feat: 无法顺序读取数据源配置时,可以手动指定默认数据源,关闭 https://gitee.com/mybatis-flex/mybatis-flex/issues/I9VBRJ

This commit is contained in:
Suomm 2024-10-01 17:53:53 +08:00
parent ba73b0cb4a
commit d865f95af7
2 changed files with 62 additions and 31 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
* Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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<String, Map<String, String>> 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<String, Map<String, String>> 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<String, String> 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<String, Map<String, String>> entry : dataSourceProperties.entrySet()) {
flexDataSource = addDataSource(entry, flexDataSource);
}
}
return flexDataSource;
}
private FlexDataSource addDataSource(Map.Entry<String, Map<String, String>> 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} 注解切换数据源切面

View File

@ -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).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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;
/**
* <p>多数据源的配置
*
@ -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
*/