本文目的是为了让你快速在Springboot中使用多数据库切换,并非研究类型文章~
本文说的方法也是使用AbstractRoutingDataSource,因此和SpringMVC中使用需要的类完全一样,仅仅是配置略有不同。阅读本文之前请先参考
https://blog.csdn.net/lizhengwei1989/article/details/88081703
了解使用过程中自定义的类
下面假设自定义的类已经有了,接下来的配置如下:
使用到了另一篇文章中一样的类有:
DataSourceContextHolder
DataSourceKey
DynamicDataSource
DynamicDataSourceAspect
TargetDataSource
你可以直接拷贝使用
不一样的改动有
首先,你的启动类上的配置
//指定aop事务执行顺序,已保证在切换数据源的后面
@EnableTransactionManagement(order = 2)
//排除数据源自动配置
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class ToolApplication {
public static void main(String[] args) {
SpringApplication.run(ToolApplication.class, args);
}
}
其次,新增了一个类,这个类和前面提到的5个类放一起就好了
package com.ifeng.tool.dynamic;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class DynamicDataSourceConfiguration {
@Primary
@Bean(name = "clickhouseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.clickhouse")
public DataSource masterDataSource() {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
dataSource.setName("clickhouseDataSource");
return dataSource;
}
@Bean(name = "brandDataSource")
@ConfigurationProperties(prefix = "spring.datasource.brand")
public DataSource slaveDataSource() {
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
dataSource.setName("brandDataSource");
return dataSource;
}
/**
* 动态数据源: 通过AOP在不同数据源之间动态切换
* @return
*/
@Bean(name = "dynamicDataSource")
public DataSource dataSource(@Autowired @Qualifier("clickhouseDataSource") DataSource primery,@Autowired @Qualifier("brandDataSource") DataSource brand) {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 默认数据源
dynamicDataSource.setDefaultTargetDataSource(primery);
// 配置多数据源
Map<Object, Object> dsMap = new HashMap<Object, Object>();
dsMap.put("clickhouse", primery);
dsMap.put("brand", brand);
dynamicDataSource.setTargetDataSources(dsMap);
return dynamicDataSource;
}
@Bean
public PlatformTransactionManager txManager(DataSource dynamicDataSource) {
return new DataSourceTransactionManager(dynamicDataSource);
}
@Bean("sqlSessionFactory")
@ConfigurationProperties(prefix = "mybatis")
public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired @Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dynamicDataSource);
return sqlSessionFactoryBean;
}
@Bean
@ConfigurationProperties(prefix = "mybatis")
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.testxxxx.tool");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
return mapperScannerConfigurer;
}
}
对应的数据库配置是
spring.datasource.clickhouse.driver-class-name:ru.yandex.clickhouse.ClickHouseDriver
spring.datasource.clickhouse.url:jdbc:clickhouse://xxxxxxxx/ods
spring.datasource.clickhouse.username:
spring.datasource.clickhouse.password:
spring.datasource.brand.driver-class-name:com.mysql.jdbc.Driver
spring.datasource.brand.url:jdbc:mysql://xxxxxxx/ibs
spring.datasource.brand.username:xxxxx
spring.datasource.brand.password:xxxxxxx
配置就够了,实测切换没有问题