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 a9c3bf5f..689ac882 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 @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; import java.util.Map; @@ -33,11 +34,11 @@ import java.util.Map; /** * 多数据源的配置支持 */ -@org.springframework.context.annotation.Configuration(proxyBeanMethods = false) -@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) @ConditionalOnMybatisFlexDatasource() +@Configuration(proxyBeanMethods = false) @EnableConfigurationProperties(MybatisFlexProperties.class) -@AutoConfigureBefore({DataSourceAutoConfiguration.class}) +@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) +@AutoConfigureBefore(value = DataSourceAutoConfiguration.class, name = "com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure") public class MultiDataSourceAutoConfiguration { private final Map> dataSourceProperties; @@ -55,12 +56,12 @@ public class MultiDataSourceAutoConfiguration { FlexDataSource flexDataSource = null; if (dataSourceProperties != null && !dataSourceProperties.isEmpty()) { - for (String key : dataSourceProperties.keySet()) { - DataSource dataSource = new DataSourceBuilder(dataSourceProperties.get(key)).build(); + for (Map.Entry> entry : dataSourceProperties.entrySet()) { + DataSource dataSource = new DataSourceBuilder(entry.getValue()).build(); if (flexDataSource == null) { - flexDataSource = new FlexDataSource(key, dataSource); + flexDataSource = new FlexDataSource(entry.getKey(), dataSource); } else { - flexDataSource.addDataSource(key, dataSource); + flexDataSource.addDataSource(entry.getKey(), dataSource); } } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index f6cbcd73..9d216198 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -35,15 +35,21 @@ 2.0.32 - - - - - + + com.alibaba + druid-spring-boot-starter + 1.2.18 + - - - + + + + + + + + + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index c9f31d3f..830e78e2 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -1,13 +1,13 @@ # DataSource Config -#spring: -# h2: -# console: -# enabled: true -# datasource: -# driver-class-name: com.mysql.cj.jdbc.Driver -# url: jdbc:mysql://localhost:3306/flex_test -# username: root -# password: 12345678 +spring: + # h2: + # console: + # enabled: true + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/flex_test + username: root + password: 12345678 # sql: # init: # schema-locations: classpath:schema.sql @@ -15,11 +15,11 @@ mybatis-flex: mapper-locations: - classpath*:/mapper/*.xml - datasource: - data-center: - url: jdbc:mysql://localhost:3306/flex_test - username: root - password: 12345678 +# datasource: +# data-center: +# url: jdbc:mysql://localhost:3306/flex_test +# username: root +# password: 12345678 #mybatis-flex: # datasource: # ds1: