码迷,mamicode.com
首页 > 编程语言 > 详细

spring-boot集成hikari多数据源

时间:2021-05-03 13:19:41      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:framework   str   frame   jdb   EDA   mys   das   session   xxxx   

maven依赖


        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>4.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.4</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
 	<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>ru.yandex.clickhouse</groupId>
            <artifactId>clickhouse-jdbc</artifactId>
            <version>0.3.0</version>
        </dependency>

注意:移除spring-boot-starter-jdbc、mybatis-spring-boot-starter依赖

application.yml

spring:
  datasource:
    tidb:
      type: com.zaxxer.hikari.HikariDataSource
      driverClassName: com.mysql.jdbc.Driver
      jdbcUrl: jdbc:mysql://xxx:4000/dbname?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
      username: root
      password: ‘root‘
      idle-timeout: 60000
      maximum-pool-size: 100
      minimum-idle: 10
      connection-timeout: 120000
      max-lifetime: 200000
      pool-name: ‘tidb-connection-pool‘
      keepaliveTime: 30000
    clickhouse:
      type: com.zaxxer.hikari.HikariDataSource
      driverClassName: ru.yandex.clickhouse.ClickHouseDriver
      jdbcUrl: jdbc:clickhouse://xxxx/xxx?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
      username: root
      password: ‘root‘
      idle-timeout: 60000
      maximum-pool-size: 100
      minimum-idle: 10
      connection-timeout: 30000
      max-lifetime: 600000
      pool-name: ‘clickhouse-connection-pool‘
      keepaliveTime: 30000

spring-boot代码手动装配

tidb 数据源

@Configuration
@MapperScan(basePackages = "com.demo.das.tidb.mapper", sqlSessionFactoryRef = "tidbSqlSessionFactory")
public class TidbDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.tidb")
    public HikariConfig tidbConfig() {
        return new HikariConfig();
    }

    @Bean
    public HikariDataSource tidbDataSource(HikariConfig tidbConfig) {
        HikariDataSource dataSource = new HikariDataSource(tidbConfig);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory tidbSqlSessionFactory(HikariDataSource tidbDataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(tidbDataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
        bean.setMapperLocations(resolver.getResources("classpath:mapper/tidb/*.xml"));
        bean.setTypeAliasesPackage("com.demo.entity");
        return bean.getObject();
    }

    @Bean("tidbTransactionManger")
    public DataSourceTransactionManager tidbTransactionManger(DataSource tidbDataSource) {
        return new DataSourceTransactionManager(tidbDataSource);
    }
}

clickhouse数据源

@Configuration
@MapperScan(basePackages = "com.demo.das.clickhouse.mapper", sqlSessionFactoryRef = "clickHouseSqlSessionFactory")
public class ClickHouseDataSourceConfig {


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.clickhouse")
    public HikariConfig clickHouseConfig() {
        return new HikariConfig();
    }

    @Bean
    public HikariDataSource clickHouseDataSource(HikariConfig clickHouseConfig) {
        HikariDataSource dataSource = new HikariDataSource(clickHouseConfig);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory clickHouseSqlSessionFactory(DataSource clickHouseDataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(clickHouseDataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
        bean.setMapperLocations(resolver.getResources("classpath:mapper/clickhouse/*.xml"));
        bean.setTypeAliasesPackage("com.demo.entity");
        return bean.getObject();
    }
}

spring-boot集成hikari多数据源

标签:framework   str   frame   jdb   EDA   mys   das   session   xxxx   

原文地址:https://www.cnblogs.com/Devonmusa/p/14724530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!