StringBoot多数据源支持

来源:互联网 发布:古墓丽影崛起配置优化 编辑:程序博客网 时间:2024/06/04 19:20

多数据元配置

  1. 首先在配置文件中根据spring.xxx前缀配置两个数据源
#centerspring.ds_center.type=com.alibaba.druid.pool.DruidDataSourcespring.ds_center.driver-class-name=com.mysql.jdbc.Driverspring.ds_center.url=jdbc:mysql://192.168.xx.xx:3306/db1?useSSL=falsespring.ds_center.username=rootspring.ds_center.password=123456# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大spring.ds_center.initialSize=5spring.ds_center.minIdle=5spring.ds_center.maxActive=20# 配置获取连接等待超时的时间spring.ds_center.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.ds_center.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒spring.ds_center.minEvictableIdleTimeMillis=300000spring.ds_center.validationQuery=SELECT 1 FROM DUALspring.ds_center.testWhileIdle=truespring.ds_center.testOnBorrow=falsespring.ds_center.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小spring.ds_center.poolPreparedStatements=truespring.ds_center.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙spring.ds_center.filters=stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录spring.ds_center.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 合并多个DruidDataSource的监控数据#spring.datasource.useGlobalDataSourceStat=true#managespring.ds_manage.type=com.alibaba.druid.pool.DruidDataSourcespring.ds_manage.driver-class-name=com.mysql.jdbc.Driverspring.ds_manage.url=jdbc:mysql://192.168.xx.xx:3306/db2?useSSL=falsespring.ds_manage.username=rootspring.ds_manage.password=123456# 下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大spring.ds_manage.initialSize=5spring.ds_manage.minIdle=5spring.ds_manage.maxActive=20# 配置获取连接等待超时的时间spring.ds_manage.maxWait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.ds_manage.timeBetweenEvictionRunsMillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒spring.ds_manage.minEvictableIdleTimeMillis=300000spring.ds_manage.validationQuery=SELECT 1 FROM DUALspring.ds_manage.testWhileIdle=truespring.ds_manage.testOnBorrow=falsespring.ds_manage.testOnReturn=false# 打开PSCache,并且指定每个连接上PSCache的大小spring.ds_manage.poolPreparedStatements=truespring.ds_manage.maxPoolPreparedStatementPerConnectionSize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙spring.ds_manage.filters=stat,wall,log4j# 通过connectProperties属性来打开mergeSql功能;慢SQL记录spring.ds_manage.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000# 合并多个DruidDataSource的监控数据#spring.datasource.useGlobalDataSourceStat=true
  1. 然后
package com.xx.xx.dao;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;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.jdbc.core.namedparam.NamedParameterJdbcTemplate;import javax.sql.DataSource;/** * 数据库多实例配置 */@Configurationpublic class MultipleDBConfig {    @Bean(name = "centerDb")    @Primary    @ConfigurationProperties(prefix = "spring.ds_center")    public DataSource centerDataSource() {//        return DataSourceBuilder.create().build();        return new DruidDataSource();    }    @Bean(name = "centerJdbcTemplate")    public JdbcTemplate centerJdbcTemplate(@Qualifier("centerDb") DataSource dsMySQL) {        return new JdbcTemplate(dsMySQL);    }    @Bean(name = "centerNamedJdbcTemplate")    public NamedParameterJdbcTemplate centerNamedJdbcTemplate(@Qualifier("centerDb") DataSource dsMySQL) {        return new NamedParameterJdbcTemplate(dsMySQL);    }    @Bean(name = "manageDb")    @ConfigurationProperties(prefix = "spring.ds_manage")    public DataSource manageDataSource() {//        return DataSourceBuilder.create().build();        return new DruidDataSource();    }    @Bean(name = "manageJdbcTemplate")    public JdbcTemplate manageJdbcTemplate(@Qualifier("manageDb") DataSource dsMySQL) {        return new JdbcTemplate(dsMySQL);    }    @Bean(name = "manageNamedJdbcTemplate")    public NamedParameterJdbcTemplate manageNamedJdbcTemplate(@Qualifier("manageDb") DataSource dsMySQL) {        return new NamedParameterJdbcTemplate(dsMySQL);    }}
  1. 通过自动注入的方式使用(同其他组件)
    @Autowired    @Qualifier("manageJdbcTemplate")    protected JdbcTemplate jdbcTemplate;    @Autowired    @Qualifier("manageNamedJdbcTemplate")    protected NamedParameterJdbcTemplate namedJdbcTemplate;    @Autowired    @Qualifier("centerJdbcTemplate")    protected JdbcTemplate centerJdbcTemplate;    @Autowired    @Qualifier("centerNamedJdbcTemplate")    protected NamedParameterJdbcTemplate centerNamedJdbcTemplate;