|
| 1 | +package cn.lijunkui.config; |
| 2 | + |
| 3 | +import cn.lijunkui.enums.DataSourceTypeEnum; |
| 4 | +import com.alibaba.druid.pool.DruidDataSource; |
| 5 | +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; |
| 6 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 7 | +import org.mybatis.spring.SqlSessionFactoryBean; |
| 8 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 9 | +import org.springframework.boot.context.properties.ConfigurationProperties; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; |
| 13 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 14 | +import org.springframework.transaction.PlatformTransactionManager; |
| 15 | + |
| 16 | +import javax.sql.DataSource; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * https://cloud.tencent.com/developer/article/1480366 |
| 22 | + * 多数据库源配置类 |
| 23 | + * @Author jkli |
| 24 | + * @Date 2020/6/14 2:57 下午 |
| 25 | + **/ |
| 26 | +@Configuration |
| 27 | +public class DataSourceConfig { |
| 28 | + |
| 29 | + /** |
| 30 | + * 商品库的数据源 |
| 31 | + * @return |
| 32 | + */ |
| 33 | + @Bean(name = "dataSourceForProduct") |
| 34 | + @ConfigurationProperties(prefix="spring.datasource.druid.product") |
| 35 | + public DruidDataSource dataSourceForProduct() { |
| 36 | + return DruidDataSourceBuilder.create().build(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * 旅馆库的数据源 |
| 41 | + * @return |
| 42 | + */ |
| 43 | + @Bean(name = "dataSourceForHotel") |
| 44 | + @ConfigurationProperties(prefix="spring.datasource.druid.hotel") |
| 45 | + public DruidDataSource dataSourceForHotel() { |
| 46 | + return DruidDataSourceBuilder.create().build(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 动态切换的数据源 |
| 51 | + * @return |
| 52 | + */ |
| 53 | + @Bean(name = "dynamicDataSource") |
| 54 | + public DataSource dynamicDataSource() { |
| 55 | + |
| 56 | + Map<Object, Object> targetDataSource = new HashMap<>(); |
| 57 | + targetDataSource.put(DataSourceTypeEnum.PRODUCT, dataSourceForProduct()); |
| 58 | + targetDataSource.put(DataSourceTypeEnum.HOTEL, dataSourceForHotel()); |
| 59 | + //设置默认的数据源和以及多数据源的Map信息 |
| 60 | + DynamicDataSourceRouting dataSource = new DynamicDataSourceRouting(); |
| 61 | + dataSource.setTargetDataSources(targetDataSource); |
| 62 | + dataSource.setDefaultTargetDataSource(dataSourceForProduct()); |
| 63 | + return dataSource; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 多数据源的 SqlSessionFactory |
| 68 | + * @param dynamicDataSource |
| 69 | + * @return |
| 70 | + * @throws Exception |
| 71 | + */ |
| 72 | + @Bean(name = "sqlSessionFactory") |
| 73 | + public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) |
| 74 | + throws Exception { |
| 75 | + SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); |
| 76 | + bean.setDataSource(dynamicDataSource); |
| 77 | + //设置数据数据源的Mapper.xml路径 |
| 78 | + bean.setMapperLocations( |
| 79 | + new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); |
| 80 | + //设置Mybaties查询数据自动以驼峰式命名进行设值 |
| 81 | + org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session |
| 82 | + .Configuration(); |
| 83 | + configuration.setMapUnderscoreToCamelCase(true); |
| 84 | + bean.setConfiguration(configuration); |
| 85 | + |
| 86 | + return bean.getObject(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * 注入 DataSourceTransactionManager 用于事务管理 |
| 91 | + */ |
| 92 | + @Bean |
| 93 | + public PlatformTransactionManager transactionManager(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) { |
| 94 | + DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dynamicDataSource); |
| 95 | + return new DataSourceTransactionManager(dynamicDataSource); |
| 96 | + } |
| 97 | +} |
0 commit comments