SpringBoot配置多数据源

Druid数据源配置类

import java.util.Properties;

public class DruidConfig {

    private String url;

    private String username;

    private String password;

    private String driverClassName;

    private int initialSize;

    private int minIdle;

    private int maxActive;

    private int maxWait;

    private int timeBetweenEvictionRunsMillis;

    private int minEvictableIdleTimeMillis;

    private String validationQuery;

    private boolean testWhileIdle;

    private boolean testOnBorrow;

    private boolean testOnReturn;

    private boolean poolPreparedStatements;

    private int maxPoolPreparedStatementPerConnectionSize;

    private String filters;

    private String connectionProperties;


    public Properties getProperties() {
        Properties properties = new Properties();
        properties.setProperty("url", this.url);
        properties.setProperty("username", this.username);
        properties.setProperty("password", this.password);
        properties.setProperty("driverClassName", this.driverClassName);
        properties.setProperty("initialSize", String.valueOf(this.initialSize));
        properties.setProperty("minIdle", String.valueOf(this.minIdle));
        properties.setProperty("maxActive", String.valueOf(this.maxActive));
        properties.setProperty("maxWait", String.valueOf(this.maxWait));
        properties.setProperty("timeBetweenEvictionRunsMillis", String.valueOf(this.timeBetweenEvictionRunsMillis));
        properties.setProperty("minEvictableIdleTimeMillis", String.valueOf(this.minEvictableIdleTimeMillis));
        properties.setProperty("validationQuery", this.validationQuery);
        properties.setProperty("testWhileIdle", String.valueOf(this.testWhileIdle));
        properties.setProperty("testOnBorrow", String.valueOf(this.testOnBorrow));
        properties.setProperty("testOnReturn", String.valueOf(this.testOnReturn));
        properties.setProperty("poolPreparedStatements", String.valueOf(this.poolPreparedStatements));
        properties.setProperty("maxPoolPreparedStatementPerConnectionSize", String.valueOf(this.maxPoolPreparedStatementPerConnectionSize));
        properties.setProperty("filters", this.filters);
        properties.setProperty("connectionProperties", this.connectionProperties);

        return properties;
    }

    public DruidConfig setUrl(String url) {
        this.url = url;
        return this;
    }

    public DruidConfig setUsername(String username) {
        this.username = username;
        return this;
    }

    public DruidConfig setPassword(String password) {
        this.password = password;
        return this;
    }

    public DruidConfig setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
        return this;
    }

    public DruidConfig setInitialSize(int initialSize) {
        this.initialSize = initialSize;
        return this;
    }

    public DruidConfig setMinIdle(int minIdle) {
        this.minIdle = minIdle;
        return this;
    }

    public DruidConfig setMaxActive(int maxActive) {
        this.maxActive = maxActive;
        return this;
    }

    public DruidConfig setMaxWait(int maxWait) {
        this.maxWait = maxWait;
        return this;
    }

    public DruidConfig setTimeBetweenEvictionRunsMillis(int timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
        return this;
    }

    public DruidConfig setMinEvictableIdleTimeMillis(int minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
        return this;
    }

    public DruidConfig setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
        return this;
    }

    public DruidConfig setTestWhileIdle(boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
        return this;
    }

    public DruidConfig setTestOnBorrow(boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
        return this;
    }

    public DruidConfig setTestOnReturn(boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
        return this;
    }

    public DruidConfig setPoolPreparedStatements(boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
        return this;
    }

    public DruidConfig setMaxPoolPreparedStatementPerConnectionSize(int maxPoolPreparedStatementPerConnectionSize) {
        this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
        return this;
    }

    public DruidConfig setFilters(String filters) {
        this.filters = filters;
        return this;
    }

    public DruidConfig setConnectionProperties(String connectionProperties) {
        this.connectionProperties = connectionProperties;
        return this;
    }
}

application.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://${host}:${port}/${database}?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.datasource.username=${username}
spring.datasource.password=${password}
spring.datasource.initialSize=2
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=3000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 1
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=true
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

spring.second-datasource.driverClassName=com.mysql.jdbc.Driver
spring.second-datasource.url=jdbc:mysql://${host}:${port}/${database}?useUnicode=true&characterEncoding=utf8&useSSL=false
spring.second-datasource.username=${username}
spring.second-datasource.password=${password}
spring.second-datasource.initialSize=2
spring.second-datasource.minIdle=5
spring.second-datasource.maxActive=20
spring.second-datasource.maxWait=3000
spring.second-datasource.timeBetweenEvictionRunsMillis=60000
spring.second-datasource.minEvictableIdleTimeMillis=300000
spring.second-datasource.validationQuery=select 1
spring.second-datasource.testWhileIdle=true
spring.second-datasource.testOnBorrow=true
spring.second-datasource.testOnReturn=false
spring.second-datasource.poolPreparedStatements=true
spring.second-datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.second-datasource.filters=stat
spring.second-datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

配置数据源bean

import com.alibaba.druid.pool.DruidDataSourceFactory;
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.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class DruidDBConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidConfig druidConfig() {
        return new DruidConfig();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.second-datasource")
    public DruidConfig secondDruidConfig() {
        return new DruidConfig();
    }

    @Bean
    @Primary
    public DataSource dataSource() {
        DruidConfig config = druidConfig();
        Properties properties = config.getProperties();
        DataSource dataSource;

        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return dataSource;
    }

    @Bean
    @Qualifier(value = "secondDataSource")
    public DataSource secondDataSource() {
        DruidConfig config = secondDruidConfig();

        Properties properties = config.getProperties();
        DataSource dataSource;

        try {
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return dataSource;
    }

    @Bean
    @Primary
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    @Primary
    @Qualifier("jdbcTemplate")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(dataSource());
    }

    @Bean
    @Qualifier("secondJdbcTemplate")
    public NamedParameterJdbcTemplate secondNamedParameterJdbcTemplate() {
        return new NamedParameterJdbcTemplate(secondDataSource());
    }
}

bean注入

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class TestDao {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    @Qualifier("secondJdbcTemplate")
    private NamedParameterJdbcTemplate secondJdbcTemplate;


}

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