Springboot2.0配置多数据源

前提

本文针对SpringBoot2.0以上版本进行说明,本文的数据库连接池采用的是HikariCP,首先在pom.xml中引入springboot的依赖

    
        
            
                
                org.springframework.boot
                spring-boot-dependencies
                2.0.7.RELEASE
                pom
                import
            
        
    


      
            org.springframework.boot
            spring-boot-starter
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
            
        

          
            org.springframework.boot
            spring-boot-starter-jdbc
            
                
                    org.apache.tomcat
                    tomcat-jdbc
                
            
        

       
            com.zaxxer
            HikariCP
            ${hikaricp.version}
        

        
            mysql
            mysql-connector-java
        
    

配置文件application.properties中如下配置.

## 第一数据源
spring.datasource.primary.jdbcUrl =jdbc:mysql://localhost/dv?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=db123
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.primary.minimumIdle=3
spring.datasource.primary.maximumPoolSize=6
spring.datasource.primary.maxLifetime=1500000
spring.datasource.primary.dataSourceProperties.prepStmtCacheSize=9
spring.datasource.primary.dataSourceProperties.prepStmtCacheSqlLimit=12
spring.datasource.primary.dataSourceProperties.cachePrepStmts=true
spring.datasource.primary.dataSourceProperties.useServerPrepStmts=true

## 第二数据源
spring.datasource.second.jdbcUrl =jdbc:mysql://localhost/dv?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true&useSSL=false
spring.datasource.second.username=root
spring.datasource.second.password=db123
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.second.minimumIdle=3
spring.datasource.second.maximumPoolSize=6
spring.datasource.second.maxLifetime=1500000
spring.datasource.second.dataSourceProperties.prepStmtCacheSize=9
spring.datasource.second.dataSourceProperties.prepStmtCacheSqlLimit=12
spring.datasource.second.dataSourceProperties.cachePrepStmts=true
spring.datasource.second.dataSourceProperties.useServerPrepStmts=true

项目配置

新建主数据库配置类PrimaryDataSourceConfig

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ConfigurationProperties("spring.datasource.primary")
@EnableTransactionManagement
public class PrimaryDataSourceConfig extends HikariConfig {

    @Primary
    @Bean(name = "dsPrimary")
    public DataSource primaryDataSource() {
        return new HikariDataSource(this);
    }


    @Bean(name = "jdbcMaster")
    @Autowired
    public JdbcTemplate masterJdbcTemplate(@Qualifier("dsPrimary") DataSource dsMaster) {
        return new JdbcTemplate(dsMaster);
    }
}

在新建第二数据源配置类SecondDataSourceConfig

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ConfigurationProperties(prefix = "spring.datasource.second")
@EnableTransactionManagement
public class SecondDataSourceConfig extends HikariConfig {

    @Bean(name = "dsSecond")
    public DataSource secondDataSource() {
        return new HikariDataSource(this);
    }

    @Bean(name = "jdbcSecondary")
    @Autowired
    public JdbcTemplate secondaryJdbcTemplate(@Qualifier("dsSecond") DataSource dsMaster) {
        return new JdbcTemplate(dsMaster);
    }
}

使用

在使用jdbcTemplate的地方,加入注解 @Qualifier("jdbcMaster") 或者 @Qualifier("jdbcSecondary")就可以使用其对应的数据源操作数据库了。

你可能感兴趣的:(Springboot2.0配置多数据源)