|
@@ -1,18 +1,29 @@
|
|
|
package com.ytpm.irun.config.datasource;
|
|
package com.ytpm.irun.config.datasource;
|
|
|
|
|
|
|
|
|
|
+import com.ytpm.irun.enums.DataSourceType;
|
|
|
|
|
+import com.zaxxer.hikari.HikariDataSource;
|
|
|
|
|
+import org.mybatis.spring.annotation.MapperScan;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.context.annotation.DependsOn;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
|
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
|
+import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
import javax.sql.DataSource;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 多数据源配置
|
|
* 多数据源配置
|
|
|
* @Marx
|
|
* @Marx
|
|
|
*/
|
|
*/
|
|
|
-//@Configuration
|
|
|
|
|
|
|
+@Configuration
|
|
|
|
|
+@EnableTransactionManagement
|
|
|
public class DataSourceConfig {
|
|
public class DataSourceConfig {
|
|
|
|
|
|
|
|
// @Primary
|
|
// @Primary
|
|
@@ -28,5 +39,33 @@ public class DataSourceConfig {
|
|
|
// return DataSourceBuilder.create().build();
|
|
// return DataSourceBuilder.create().build();
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
|
|
+ @Bean(name = "masterDataSource")
|
|
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource.master")
|
|
|
|
|
+ public DataSource masterDataSource() {
|
|
|
|
|
+ return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ @Bean(name = "slaveDataSource")
|
|
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource.slave")
|
|
|
|
|
+ public DataSource slaveDataSource() {
|
|
|
|
|
+ return DataSourceBuilder.create().type(HikariDataSource.class).build();
|
|
|
|
|
+ }
|
|
|
|
|
+ @Primary
|
|
|
|
|
+ @Bean(name = "dynamicDataSource")
|
|
|
|
|
+ @DependsOn({"masterDataSource", "slaveDataSource"})
|
|
|
|
|
+ public DataSource dynamicDataSource() {
|
|
|
|
|
+ Map<Object, Object> dataSources = new HashMap<>();
|
|
|
|
|
+ dataSources.put(DataSourceType.MASTER, masterDataSource());
|
|
|
|
|
+ dataSources.put(DataSourceType.SLAVE, slaveDataSource());
|
|
|
|
|
+
|
|
|
|
|
+ DynamicDataSource ds = new DynamicDataSource();
|
|
|
|
|
+ ds.setDefaultTargetDataSource(masterDataSource());
|
|
|
|
|
+ ds.setTargetDataSources(dataSources);
|
|
|
|
|
+ return ds;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public PlatformTransactionManager transactionManager(@Qualifier("dynamicDataSource")DataSource dynamicDataSource) {
|
|
|
|
|
+ return new DataSourceTransactionManager(dynamicDataSource);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|