springboot多数据源配置

yml文件不多写

数据库名枚举

package com.springboot.db;

public enum DBName
{
    DBVv("dbvv"),
    IMM("imm");
    private String key;

    DBName(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

用来切换数据库的方法

package com.springboot.db;

/**
 * FileName: DBContext
 * Description: 更改数据库方法配置
 */
public class DBContext
{
    private static ThreadLocal<String> dbName=new ThreadLocal<>();

    public static String getTargetDataSource(){
        return dbName.get();
    }

    public static void changeDB(String name){
        dbName.set(name);
    }

    //清除数据源名
    public static void clearDB(){
        dbName.remove();
    }
}

继承类实现动态切换

package com.springboot.db;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * FileName: DynamicDataSource
 * Description:动态切换数据源
 */
public class DynamicDataSource extends AbstractRoutingDataSource
{

    @Override
    protected Object determineCurrentLookupKey() {
        return DBContext.getTargetDataSource();
    }
}

数据源配置

package com.springboot.db;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * FileName: ContextConfig
 * Description: 数据源配置
 */
@SpringBootApplication
@ComponentScan(basePackages = "com.springboot.testdemo.*")
@MapperScan(basePackages = "com.springboot.testdemo.*.dao")
@EnableTransactionManagement
public class ContextConfig
{
    @ConfigurationProperties("spring.datasource.druid.dbvv")
    @Bean("dbvv.mysql")
    @Qualifier("dbvv.mysql")
    public DataSource mysqlDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @ConfigurationProperties("spring.datasource.druid.imm")
    @Bean("imm.oracle")
    @Qualifier("imm.oracle")
    public DataSource oracleDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean("dynamicDataSource")
    @Qualifier("dynamicDataSource")
    @Primary
    public DataSource dynamicDataSource(){
        DynamicDataSource dataSource = new DynamicDataSource();
        HashMap<Object, Object> targetDataSource = new HashMap<>();
        targetDataSource.put(DBName.DBVv.getKey(),mysqlDataSource());
        targetDataSource.put(DBName.IMM.getKey(),oracleDataSource());
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(targetDataSource.get(DBName.DBVv.getKey()));
        return dataSource;
    }
}

配置数据库拦截器,Tomcat8默认使用nio的线程连接池处理请求,一个线程会串行依次的处理多个请求,那么给每个线程独特标识数据源的Threadlocal就会紊乱;拦截在每次请求进入业务层代码的时候,保证都是mysql数据库

package com.springboot.db;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * FileName: DataSourceInterceptor
 * Description: 数据库拦截器
 */
public class DataSourceInterceptor implements HandlerInterceptor
{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        DBContext.clearDB();
        DBContext.changeDB(DBName.DBVv.getKey());
        return HandlerInterceptor.super.preHandle(request,response,handler);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值