SpringBoot+MyBatis多数据源配置

1.先在配置文件application.yml中配置好数据源

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    db1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: root
      jdbc-url: jdbc:mysql://192.168.110.128:3306/CampusHelp?useUnicode=yes&characterEncoding=utf8
    db2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: root
      jdbc-url: jdbc:mysql://192.168.110.128:3306/itcast?useUnicode=yes&characterEncoding=utf8

其次要配置一个工具类DataSourceUtil,可以进行写入数据源、获取数据源、移除数据源等。

代码如下:

public class DataSourceUtil {
    private static final ThreadLocal contextHolder = new ThreadLocal<>();
    //写入数据源
    public static void setDataSource(String dataSource) {
        contextHolder.set(dataSource);
    }
    //获取数据源
    public static String getDataSource() {
        return contextHolder.get();
    }
    //移除数据源
    public static void clearDataSource() {
        contextHolder.remove();
    }
}

然后定义动态数据源类DynamicDataSource,继承AbstractRoutingDataSource接口,重写determineCurrentLookupKey方法,返回数据源

package com.hua.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;

/**
 * @Author 华子
 * @Date 2023/8/8 21:08
 * @Version 1.0
 */
@Slf4j
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        log.info("写入数据源->{}",DataSourceUtil.getDataSource());
        return DataSourceUtil.getDataSource();
    }
}

接着配置数据源,定义db1,db2两个bean,设置默认数据源和数据源集合

package com.hua.config;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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 javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 华子
 * @Date 2023/8/8 21:04
 * @Version 1.0
 */
@Configuration
public class DataSourceConfig {

    @Bean(name = "db1")
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource dataSourceOne(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "db2")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource dataSourceTwo(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "dynamicDataSource")
    @Primary
    public DataSource dynamicDataSource(@Qualifier("db1") DataSource db1,@Qualifier("db2") DataSource db2){
        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setDefaultTargetDataSource(db1);
        Map dbMap = new HashMap<>();
        dbMap.put("db1",db1);
        dbMap.put("db2",db2);
        dataSource.setTargetDataSources(dbMap);
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DataSource dynamicDataSource){
        return new DataSourceTransactionManager(dynamicDataSource);
    }
}

最后需要在启动类上关闭SpringBoot原先的自动获取数据源

SpringBoot+MyBatis多数据源配置_第1张图片

 使用方法:

SpringBoot+MyBatis多数据源配置_第2张图片

 

你可能感兴趣的:(瞎研究,spring,boot,mybatis,java)