SpringBoot配置双数据源

SpringBoot配置双数据源

properties文件配置

## mysql
spring.datasource.mysql.jdbc-url=jdbc:mysql://47.242.248.80:3306/alert_console?userUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.mysql.username=root
spring.datasource.mysql.password=mmt123456
spring.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver

## starrocks
spring.datasource.starrocks.jdbc-url=jdbc:mysql://172.16.3.134:9030/tracelog?userUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.starrocks.username=trace
spring.datasource.starrocks.password=123456
spring.datasource.starrocks.driver-class-name=com.mysql.cj.jdbc.Driver

相关配置类:

SpringBoot配置双数据源_第1张图片

  • DataSourceConfig
@Configuration
public class DataSourceConfig {

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

    @Bean(name = "starRocks")
    @ConfigurationProperties(prefix = "spring.datasource.starrocks")
    public DataSource starRocksDbDataSource() {
        return DataSourceBuilder.create().build();
    }
}
  • MysqlConfig
@Configuration
@MapperScan(basePackages = {"cn.wangoon.fd.tracking.management.console.mybatis.mapper.mysql"}, sqlSessionFactoryRef = "sqlSessionFactoryForMysql")
public class MysqlConfig {

    @Resource
    @Qualifier("mysql")
    private DataSource mysqlDataSource;


    @Bean
    public SqlSessionFactory sqlSessionFactoryForMysql() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(mysqlDataSource);
//        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateForMysql() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryForMysql());
    }

}
  • StarRocksConfig
@Configuration
@MapperScan(basePackages = {"cn.wangoon.fd.tracking.management.console.mybatis.mapper.starrocks"}, sqlSessionFactoryRef = "sqlSessionFactoryForStarRocks")
public class StarRocksConfig {

    @Resource
    @Qualifier("starRocks")
    private DataSource starRocksDataSource;


    @Bean
    public SqlSessionFactory sqlSessionFactoryForStarRocks() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(starRocksDataSource);
//        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplateForStarRocks() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryForStarRocks());
    }

}
rows Exception {
        return new SqlSessionTemplate(sqlSessionFactoryForStarRocks());
    }

}

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