dynamic-datasource多数据源

动态数据源

项目使用多数据源,dynamic-datasource-spring-boot-starter实现动态切换数据源。

准备

maven

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.2.2</version>
</dependency>
<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>3.3.2</version>
</dependency>

application.yaml

spring:
  datasource:
    dynamic:
      primary: hub_exampledb
      strict: false
      datasource:
        hub_exampledb:
          url: jdbc:mysql://127.0.0.1:3306/hub_exampledb
          username: hub_example
          password: h12345678
          driver-class-name: com.mysql.cj.jdbc.Driver
        hub_example02db:
          url: jdbc:mysql://127.0.0.1:3306/hub_example02db
          username: hub_example02
          password: h12345678
          driver-class-name: com.mysql.cj.jdbc.Driver

使用

动态加载数据源

1 数据源服务
 
public class DataSourceService{

    private final DataSourceCreator dataSourceCreator
    /**
     * 更新(新增)动态数据的数据源列表
     *
     * @param entity 数据源
     */
    private void addDynamicDataSource(DataSource entity) {
        DataSourceProperty dataSourceProperty = new DataSourceProperty();
        dataSourceProperty.setPoolName(entity.getDsName());
        dataSourceProperty.setUrl(entity.getDsUrl());
        dataSourceProperty.setUsername(entity.getDsUserName());
        dataSourceProperty.setPassword(entity.getDsPassword());
        DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
        if(!checkDataSource(dataSource)){
            throw new RuntimeException("检查数据源是否配置正确");
        }

        DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
        dynamicRoutingDataSource.addDataSource(dataSourceProperty.getPoolName(), dataSource);
    }

    /**
     * 校验数据源配置是否有效
     *
     * @param entity 数据源信息
     * @return Boolean 有效/无效
     */
    private Boolean checkDataSource(DataSource entity) {
        try {
            DriverManager.getConnection(entity.getUrl(), entity.getDsUserName(), entity.getDsPassword());
        } catch (SQLException e) {
            log.error("数据源配置 {} , 获取链接失败", entity.getDsName(), e);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    /**
     * 删除动态数据的数据源
     *
     * @param entity 数据源
     */
    private void removeDynamicDataSource(DataSource entity) {
        DynamicRoutingDataSource dynamicRoutingDataSource = SpringContextHolder.getBean(DynamicRoutingDataSource.class);
        dynamicRoutingDataSource.removeDataSource(mdmSysDataSource.getDsName());
    }
}

注解方式切换数据源

@DS(“dataSourceName”)注解作用于类,方法上, 实现数据源切换

//作用类
@DS("hub_example02db")
@Service
public class ProvinceServiceImpl implements ProvinceService {
  @Autowired
  private ProvinceMapper provinceMapper;
 
  @Override
  public ProvinceDTO queryProvinceByProvinceId(String provinceId) {
    return provinceMapper.queryProvinceByProvinceId(provinceId);
  }
 
}

//或者作用方法上
@Service
public class ProvinceServiceImpl implements ProvinceService {
  @Autowired
  private ProvinceMapper provinceMapper;
 
  @DS("hub_exampledb")
  @Override
  public ProvinceDTO queryProvinceByProvinceId(String provinceId) {
    return provinceMapper.queryProvinceByProvinceId(provinceId);
  }
 
}

编码方式切换数据源

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
// 切换数据源
DynamicDataSourceContextHolder.push("dsName");
// todo some thing
DynamicDataSourceContextHolder.clear();


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值