当前位置:   article > 正文

SpringBoot配置双数据源_springboot starrocks

springboot starrocks

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

相关配置类:

在这里插入图片描述

  • 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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 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());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 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());
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/498862
推荐阅读
相关标签
  

闽ICP备14008679号