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> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.DataSourceDecipher;
import com.mybatisflex.core.datasource.DataSourceManager; import com.mybatisflex.core.datasource.DataSourceManager;
import com.mybatisflex.core.datasource.FlexDataSource; 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.boot.MybatisFlexProperties.SeataConfig;
import com.mybatisflex.spring.datasource.DataSourceAdvice; import com.mybatisflex.spring.datasource.DataSourceAdvice;
import io.seata.rm.datasource.DataSourceProxy; import io.seata.rm.datasource.DataSourceProxy;
@ -55,6 +57,7 @@ import java.util.Map;
"com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure"}) "com.alibaba.druid.spring.boot3.autoconfigure.DruidDataSourceAutoConfigure"})
public class MultiDataSourceAutoConfiguration { public class MultiDataSourceAutoConfiguration {
private final String master;
private final Map<String, Map<String, String>> dataSourceProperties; private final Map<String, Map<String, String>> dataSourceProperties;
@ -70,6 +73,7 @@ public class MultiDataSourceAutoConfiguration {
dataSourceProperties = properties.getDatasource(); dataSourceProperties = properties.getDatasource();
dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable(); dataSourceDecipher = dataSourceDecipherProvider.getIfAvailable();
seataConfig = properties.getSeataConfig(); seataConfig = properties.getSeataConfig();
master = properties.getDefaultDatasourceKey();
} }
@Bean @Bean
@ -84,8 +88,24 @@ public class MultiDataSourceAutoConfiguration {
DataSourceManager.setDecipher(dataSourceDecipher); DataSourceManager.setDecipher(dataSourceDecipher);
} }
for (Map.Entry<String, Map<String, String>> entry : dataSourceProperties.entrySet()) { if (master != null) {
Map<String, String> map = dataSourceProperties.remove(master);
if (map != null) {
flexDataSource = addDataSource(MapUtil.entry(master, map), flexDataSource);
} else {
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(); DataSource dataSource = new DataSourceBuilder(entry.getValue()).build();
DataSourceManager.decryptDataSource(dataSource); DataSourceManager.decryptDataSource(dataSource);
@ -102,9 +122,6 @@ public class MultiDataSourceAutoConfiguration {
} else { } else {
flexDataSource.addDataSource(entry.getKey(), dataSource, false); flexDataSource.addDataSource(entry.getKey(), dataSource, false);
} }
}
}
return flexDataSource; return flexDataSource;
} }

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2015-2022 the original author or authors. * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* https://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,7 +21,11 @@ import org.apache.ibatis.io.VFS;
import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.Log;
import org.apache.ibatis.mapping.ResultSetType; import org.apache.ibatis.mapping.ResultSetType;
import org.apache.ibatis.scripting.LanguageDriver; 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.JdbcType;
import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandler;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
@ -52,6 +56,8 @@ public class MybatisFlexProperties {
private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(); private static final ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
private String defaultDatasourceKey;
/** /**
* <p>多数据源的配置 * <p>多数据源的配置
* *
@ -160,6 +166,14 @@ public class MybatisFlexProperties {
this.adminConfig = adminConfig; this.adminConfig = adminConfig;
} }
public String getDefaultDatasourceKey() {
return defaultDatasourceKey;
}
public void setDefaultDatasourceKey(String defaultDatasourceKey) {
this.defaultDatasourceKey = defaultDatasourceKey;
}
/** /**
* @since 1.1.0 * @since 1.1.0
*/ */