|
@@ -1,6 +1,8 @@
|
|
|
package com.tzld.piaoquan.api.config;
|
|
package com.tzld.piaoquan.api.config;
|
|
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
|
|
+import com.tzld.piaoquan.growth.common.config.datasource.DynamicDataSource;
|
|
|
|
|
+import com.tzld.piaoquan.growth.common.config.datasource.DynamicDataSourceContextHolder;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
|
import org.mybatis.spring.SqlSessionFactoryBean;
|
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
import org.mybatis.spring.annotation.MapperScan;
|
|
@@ -14,6 +16,8 @@ import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
import org.springframework.transaction.PlatformTransactionManager;
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
import javax.sql.DataSource;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
@@ -22,12 +26,42 @@ import javax.sql.DataSource;
|
|
|
public class DataSourceConfig {
|
|
public class DataSourceConfig {
|
|
|
static final String MAPPER_LOCATION_MASTER = "classpath*:mapper/**/*.xml";
|
|
static final String MAPPER_LOCATION_MASTER = "classpath*:mapper/**/*.xml";
|
|
|
|
|
|
|
|
- @Bean(name = "dataSource")
|
|
|
|
|
- @ConfigurationProperties("spring.datasource")
|
|
|
|
|
- public DataSource getDataSource(){
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 主数据源(MySQL)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean(name = "primaryDataSource")
|
|
|
|
|
+ @ConfigurationProperties("spring.datasource.primary")
|
|
|
|
|
+ public DataSource primaryDataSource(){
|
|
|
return new DruidDataSource();
|
|
return new DruidDataSource();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ADB 数据源
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean(name = "adbDataSource")
|
|
|
|
|
+ @ConfigurationProperties("spring.datasource.adb")
|
|
|
|
|
+ public DataSource adbDataSource(){
|
|
|
|
|
+ return new DruidDataSource();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 动态数据源
|
|
|
|
|
+ */
|
|
|
|
|
+ @Primary
|
|
|
|
|
+ @Bean(name = "dataSource")
|
|
|
|
|
+ public DataSource dynamicDataSource(@Qualifier("primaryDataSource") DataSource primaryDataSource,
|
|
|
|
|
+ @Qualifier("adbDataSource") DataSource adbDataSource) {
|
|
|
|
|
+ DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
|
|
|
|
+ // 设置默认数据源
|
|
|
|
|
+ dynamicDataSource.setDefaultTargetDataSource(primaryDataSource);
|
|
|
|
|
+ // 设置多数据源
|
|
|
|
|
+ Map<Object, Object> targetDataSources = new HashMap<>();
|
|
|
|
|
+ targetDataSources.put(DynamicDataSourceContextHolder.DEFAULT_DATASOURCE, primaryDataSource);
|
|
|
|
|
+ targetDataSources.put(DynamicDataSourceContextHolder.ADB_DATASOURCE, adbDataSource);
|
|
|
|
|
+ dynamicDataSource.setTargetDataSources(targetDataSources);
|
|
|
|
|
+ return dynamicDataSource;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Primary
|
|
@Primary
|
|
|
@Bean(name = "growthTransactionManager")
|
|
@Bean(name = "growthTransactionManager")
|
|
|
public PlatformTransactionManager growthTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
|
|
public PlatformTransactionManager growthTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
|
|
@@ -45,4 +79,26 @@ public class DataSourceConfig {
|
|
|
return sessionFactoryBean.getObject();
|
|
return sessionFactoryBean.getObject();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ADB 专用的 SqlSessionFactory
|
|
|
|
|
+ * 用于需要明确使用 ADB 数据源的 Mapper
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean(name = "adbSqlSessionFactory")
|
|
|
|
|
+ public SqlSessionFactory adbSqlSessionFactory(@Qualifier("adbDataSource") DataSource adbDataSource) throws Exception {
|
|
|
|
|
+ final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
|
|
|
|
|
+ sessionFactoryBean.setDataSource(adbDataSource);
|
|
|
|
|
+ sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DataSourceConfig.MAPPER_LOCATION_MASTER));
|
|
|
|
|
+ sessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
|
|
|
|
|
+ sessionFactoryBean.getObject().getConfiguration().setUseGeneratedKeys(true);
|
|
|
|
|
+ return sessionFactoryBean.getObject();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * ADB 专用的事务管理器
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean(name = "adbTransactionManager")
|
|
|
|
|
+ public PlatformTransactionManager adbTransactionManager(@Qualifier("adbDataSource") DataSource adbDataSource) {
|
|
|
|
|
+ return new DataSourceTransactionManager(adbDataSource);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|