多数据源配置

目录

方案一:mybatis-plus多数据源配置

1. 引入dynamic-datasource-spring-boot-starter。

2. 配置数据源。

3. 使用 @DS 切换数据源。

方案二:druid连接多个数据库

config文件

配置文件


方案一:mybatis-plus多数据源配置

1. 引入dynamic-datasource-spring-boot-starter。

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  <version>${version}</version>
</dependency>

2. 配置数据源。

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_2:
          url: ENC(xxxxx) # 内置加密,使用请查看详细文档
          username: ENC(xxxxx)
          password: ENC(xxxxx)
          driver-class-name: com.mysql.jdbc.Driver
       #......省略
       #以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2
# 多主多从                      纯粹多库(记得设置primary)                   混合配置
spring:                               spring:                               spring:
  datasource:                           datasource:                           datasource:
    dynamic:                              dynamic:                              dynamic:
      datasource:                           datasource:                           datasource:
        master_1:                             mysql:                                master:
        master_2:                             oracle:                               slave_1:
        slave_1:                              sqlserver:                            slave_2:
        slave_2:                              postgresql:                           oracle_1:
        slave_3:                              h2:                                   oracle_2:

3. 使用 @DS 切换数据源。

@DS 可以注解在方法上或类上,同时存在就近原则 方法上注解 优先于 类上注解

注解

结果

没有@DS

默认数据源

@DS("dsName")

dsName可以为组名也可以为具体某个库的名称

@Service
@DS("slave")
public class UserServiceImpl implements UserService {

  @Autowired
  private JdbcTemplate jdbcTemplate;

  public List selectAll() {
    return  jdbcTemplate.queryForList("select * from user");
  }
  
  @Override
  @DS("slave_1")
  public List selectByCondition() {
    return  jdbcTemplate.queryForList("select * from user where age >10");
  }
}

方案二:druid连接多个数据库

使用包路径来指定多数据源配置

config文件

@Configuration
@MapperScan(basePackages = "com.watt.datamill.dao.mapper", sqlSessionFactoryRef  = "mysqlSqlSessionFactory")
public class DataMillConfig {
    //这里是mapper.xml路径, 根据自己的项目调整
    private static final String MAPPER_LOCATION = "classpath*:mapper/*.xml";
    private static final String TYPE_ALIASES_PACKAGE = "com.watt.datamill.entity.vo.*";


    @Primary //这个注解的意思是默认使用当前数据源
    @Bean
    @Qualifier("first")
    @ConfigurationProperties(prefix = "spring.datasource.druid.first")
    public DataSource mysqlDataSource() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    @Bean
    @Qualifier("second")
    @ConfigurationProperties(prefix = "spring.datasource.druid.second")
    public DataSource olapDataSource() {
        return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    @Primary
    @Bean("mysqlSqlSessionFactory")
    public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("first") DataSource dataSource,
                                                    @Qualifier("globalConfig") GlobalConfig globalConfig) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        bean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);

        bean.setGlobalConfig(globalConfig);
        bean.setPlugins(mybatisPlusInterceptor());

        return bean.getObject();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 配置事务管理
     */
    @Bean(name = "mysqlTransactionManager")
    @Primary
    public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("first") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean("mysqlSqlSessionTemplate")
    public SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    @Qualifier("globalConfig")
    protected GlobalConfig globalConfig(MetaObjectHandler metaObjectHandler) {
        GlobalConfig globalConfig = GlobalConfigUtils.defaults();
        //mybatis-plus全局配置设置元数据对象处理器为自己实现的那个
        globalConfig.setMetaObjectHandler(metaObjectHandler);

        return globalConfig;
    }

}

@Configuration
@MapperScan(basePackages = "com.watt.datamill.databoard.mapper", sqlSessionFactoryRef  = "secondSqlSessionFactory")
public class DataBoardConfig {
    //这里是mapper.xml路径, 根据自己的项目调整
    private static final String MAPPER_LOCATION = "classpath*:mapper/*.xml";
    //这里是数据库表对应的entity实体类所在包路径, 根据自己的项目调整
    private static final String TYPE_ALIASES_PACKAGE = "com.watt.datamill.entity.vo.*";

    @Bean(name = "databoardOlapTransactionManager")
    public DataSourceTransactionManager bigdataTransactionManager(@Qualifier("second") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("second") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();

        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
        bean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
        bean.setPlugins(mybatisPlusInterceptor());
        return bean.getObject();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean("secondSqlSessionTemplate")
    public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

配置文件

spring:
  datasource:
    druid:
      first:
        url: jdbc:mysql://${DB_HOST}:${DB_PORT}/datamill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
        driverClassName: com.mysql.cj.jdbc.Driver
        username: ${DB_USERNAME}
        password: ${DB_PASSWORD}
        validationQuery: SELECT 1
        testOnBorrow: true
      second:
        url: jdbc:mysql://192.168.0.104:9030/databoard_ts?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
        driverClassName: com.mysql.cj.jdbc.Driver
        username: databoard
        password: databoard@watt
        validationQuery: SELECT 1

在对应的mapper包中编写相应mapper类即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值