依赖

  1. <!-- 添加mybatis依赖坐标 -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>${mybatis.version}</version>
  6. </dependency>
  7. <!-- 添加AOP坐标 -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-aop</artifactId>
  11. </dependency>

这里使用的数据源为druid,实现数据源之间的切换用@DataSource自定义注解,配置Aop进行切换 application.yml 配置文件。

spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          xiaobin-master: # 主数据源
            driverClassName: com.mysql.jdbc.Driver
            username: root
            password: root
            url: jdbc:mysql://localhost:3306/master_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
          xiaobin-slave: # 从数据源
            driverClassName: com.mysql.jdbc.Driver
            username: root
            password: root
            url: jdbc:mysql://localhost:3306/slave_test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8
mybatis:
 mapper-locations: classpath:mapper/*.xml


多数据源配置类


@Configuration
@Component
public class DynamicDataSourceConfig {

    @Bean
    @ConfigurationProperties("spring.datasource.druid.xiaobin-master")
    public DataSource  xiaobinMasterDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties("spring.datasource.druid.xiaobin-slave")
    public DataSource  xiaobinSlaveDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DynamicDataSource dataSource(DataSource xiaobinMasterDataSource, DataSource xiaobinSlaveDataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("xiaobin-master",xiaobinMasterDataSource);
        targetDataSources.put("xiaobin-slave", xiaobinSlaveDataSource);
        return new DynamicDataSource(xiaobinMasterDataSource, targetDataSources);
    }
}

动态数据源切换类


public class DynamicDataSource  extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return getDataSource();
    }

    public static void setDataSource(String dataSource) {
        contextHolder.set(dataSource);
    }

    public static String getDataSource() {
        return contextHolder.get();
    }

    public static void clearDataSource() {
        contextHolder.remove();
    }
}

https://mp.weixin.qq.com/s?__biz=MzUyODkwMTQyNg==&mid=2247489147&idx=1&sn=8fd05f0f7f748d83d37b8a8846d47a93&chksm=fa686226cd1feb3036819a26faac1bd976fdab51dd78f4a697f8309d160fceea0771a8fcf44a&scene=126&sessionid=1585790684&key=a08075b8a5282dd033408d1be860a99ab958284bd4976064f7821ba95a2f657e8cec2dcd0d8fd2964e11048554ad6366f80dd57f6108098d0516c9ffd2cc8bc0c2d83a48df189efa35b40e0a029f8d66&ascene=1&uin=MjA3NTA5MTI4MQ%3D%3D&devicetype=Windows+10&version=62080079&lang=zh_CN&exportkey=AT2Tncac0IqkyTqA14WY9yU%3D&pass_ticket=CDA%2Bf9%2BQG5q5QQM1Meb64H5Bj5rKTdQb55qvyBwXtwGxAsuOVxEIKqhHrqkqIRee