SpringBoot多数据源配置(oracle)

一.application.properties

商户表数据源

  •      merchant.datasource.type=com.alibaba.druid.pool.DruidDataSource
         merchant.datasource.driver-class-name=oracle.jdbc.OracleDriver
         merchant.datasource.url=jdbc:oracle:thin:@10.8.11.19:1521/dbdata
         merchant.datasource.username=test_merchant
         merchant.datasource.password=123456
         merchant.datasource.initialSize=5
         merchant.datasource.minIdle=5
         merchant.datasource.maxActive=20
    
订单表数据源
  •     orders.datasource.type=com.alibaba.druid.pool.DruidDataSource
        orders.datasource.driver-class-name=oracle.jdbc.OracleDriver
        orders.datasource.url=jdbc:oracle:thin:@10.8.11.19:1521/dbdata
        orders.datasource.username=test_orders
        orders.datasource.password=123456
        orders.datasource.initialSize=5
        orders.datasource.minIdle=5
        orders.datasource.maxActive=20
    
数据库公共配置
  •    commons.datasource.testWhileIdle=true
       commons.datasource.testOnBorrow=false
       commons.datasource.testOnReturn=false
    
打开PSCache,并且指定每个连接上PSCache的大小
  •    commons.datasource.poolPreparedStatements=true
       commons.datasource.maxPoolPreparedStatementPerConnectionSize=20
    
配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  •    commons.datasource.filters=stat,wall,log4j
    
通过connectProperties属性来打开mergeSql功能;慢SQL记录
  •    commons.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    

二.MerchantDataSoucreConfig.java

  •    package com.test.merchant.db;
    
       import java.util.Properties;
    
       import javax.sql.DataSource;
    
       import org.apache.ibatis.session.SqlSessionFactory;
       import org.mybatis.spring.SqlSessionFactoryBean;
       import org.mybatis.spring.annotation.MapperScan;
       import org.springframework.beans.factory.annotation.Qualifier;
       import org.springframework.beans.factory.annotation.Value;
       import org.springframework.context.annotation.Bean;
       import org.springframework.context.annotation.Configuration;
       import org.springframework.context.annotation.Primary;
       import org.springframework.jdbc.datasource.DataSourceTransactionManager;
       import org.springframework.transaction.annotation.EnableTransactionManagement;
    
       import com.alibaba.druid.pool.DruidDataSource;
    
       @Configuration
       @EnableTransactionManagement
       @MapperScan(basePackages = MerchantDataSourceConfig.PACKAGE, sqlSessionFactoryRef ="merchantSqlSessionFactory")
       public class MerchantDataSourceConfig {
    
          static final String PACKAGE = "com.test.merchant.dao.merchant";
    
          @Value("${merchant.datasource.url}")
          private String dbUrl;
    
          @Value("${merchant.datasource.username}")
          private String username;
    
          @Value("${merchant.datasource.password}")
          private String password;
    
          @Value("${merchant.datasource.driver-class-name}")
          private String driverClassName;
    
          @Value("${merchant.datasource.initialSize}")
          private int initialSize;
    
          @Value("${merchant.datasource.minIdle}")
          private int minIdle;
    
          @Value("${merchant.datasource.maxActive}")
          private int maxActive;
    
           @Value("${commons.datasource.testWhileIdle}")
           private boolean testWhileIdle;
    
           @Value("${commons.datasource.testOnBorrow}")
           private boolean testOnBorrow;
    
           @Value("${commons.datasource.testOnReturn}")
           private boolean testOnReturn;
    
           @Value("${commons.datasource.poolPreparedStatements}")
           private boolean poolPreparedStatements;
    
           @Value("${commons.datasource.maxPoolPreparedStatementPerConnectionSize}")
           private int maxPoolPreparedStatementPerConnectionSize;
    
          @Value("${commons.datasource.connectionProperties}")
          private String connectionProperties;
    
          @Value("${commons.datasource.filters}")
          private String filters;
    
           @Bean(name = "merchantDataSource", initMethod = "init", destroyMethod = "close")
          @Primary
          public DataSource dataSource() {
              DruidDataSource dds = new DruidDataSource();
              Properties arg0 = new Properties();
              arg0.setProperty("oracle.net.CONNECT_TIMEOUT", "10000");
              arg0.setProperty("oracle.net.READ_TIMEOUT", "60000");
              arg0.setProperty("oracle.jdbc.ReadTimeout", "60000");
              dds.setConnectProperties(arg0);
              dds.setDriverClassName(driverClassName);
              dds.setUrl(dbUrl);
              dds.setUsername(username);
              dds.setPassword(password);
              dds.setConnectionProperties(connectionProperties);
              dds.setInitialSize(initialSize);
              dds.setMinIdle(minIdle);
              dds.setMaxActive(maxActive);
              dds.setMaxWait(60000);
              dds.setTimeBetweenEvictionRunsMillis(180000);
              dds.setMinEvictableIdleTimeMillis(600000);
              dds.setValidationQuery("SELECT 'x' from dual");
               dds.setTestWhileIdle(testWhileIdle);
              dds.setTestOnBorrow(testOnBorrow);
              dds.setTestOnReturn(testOnReturn);
              dds.setPoolPreparedStatements(poolPreparedStatements);
              dds.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
              try {
                  dds.setFilters(filters);
              } catch (Exception e) {
              }
              return dds;
           }
    
           @Bean(name = "merchantTransactionManager")
           @Primary
           public DataSourceTransactionManager merchantTransactionManager() {
               return new DataSourceTransactionManager(dataSource());
           }
    
           @Bean(name = "merchantSqlSessionFactory")
           @Primary
           public SqlSessionFactory merchantSqlSessionFactory(@Qualifier("merchantDataSource") DataSource merchantDataSource)
                   throws Exception {
               final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
               sessionFactory.setDataSource(merchantDataSource);
      
              return sessionFactory.getObject();
           }
    
       }
    

三.OrdersDataSoucreConfig.java

  •    package com.test.merchant.db;
    
       import java.util.Properties;
    
       import javax.sql.DataSource;
    
       import org.apache.ibatis.session.SqlSessionFactory;
       import org.mybatis.spring.SqlSessionFactoryBean;
       import org.mybatis.spring.annotation.MapperScan;
       import org.springframework.beans.factory.annotation.Qualifier;
       import org.springframework.beans.factory.annotation.Value;
       import org.springframework.context.annotation.Bean;
       import org.springframework.context.annotation.Configuration;
       import org.springframework.jdbc.datasource.DataSourceTransactionManager;
       import org.springframework.transaction.annotation.EnableTransactionManagement;
    
       import com.alibaba.druid.pool.DruidDataSource;
    
       @Configuration
       @EnableTransactionManagement
       @MapperScan(basePackages = OrdersDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "ordersSqlSessionFactory")
       public class OrdersDataSourceConfig {
    
          static final String PACKAGE = "com.test.merchant.dao.orders";
    
          @Value("${orders.datasource.url}")
          private String dbUrl;
    
          @Value("${orders.datasource.username}")
          private String username;
    
          @Value("${orders.datasource.password}")
          private String password;
    
          @Value("${orders.datasource.driver-class-name}")
          private String driverClassName;
    
           @Value("${orders.datasource.initialSize}")
           private int initialSize;
    
           @Value("${orders.datasource.minIdle}")
           private int minIdle;
    
          @Value("${orders.datasource.maxActive}")
          private int maxActive;
    
          @Value("${commons.datasource.testWhileIdle}")
          private boolean testWhileIdle;
    
          @Value("${commons.datasource.testOnBorrow}")
          private boolean testOnBorrow;
    
           @Value("${commons.datasource.testOnReturn}")
           private boolean testOnReturn;
    
          @Value("${commons.datasource.poolPreparedStatements}")
          private boolean poolPreparedStatements;
    
          @Value("${commons.datasource.maxPoolPreparedStatementPerConnectionSize}")
          private int maxPoolPreparedStatementPerConnectionSize;
    
          @Value("${commons.datasource.connectionProperties}")
          private String connectionProperties;
    
          @Value("${commons.datasource.filters}")
          private String filters;
    
          @Bean(name = "ordersDataSource", initMethod = "init", destroyMethod = "close")
          public DataSource dataSource() {
              DruidDataSource dds = new DruidDataSource();
              Properties arg0 = new Properties();
              arg0.setProperty("oracle.net.CONNECT_TIMEOUT", "10000");
              arg0.setProperty("oracle.net.READ_TIMEOUT", "60000");
              arg0.setProperty("oracle.jdbc.ReadTimeout", "60000");
              dds.setConnectProperties(arg0);
              dds.setDriverClassName(driverClassName);
              dds.setUrl(dbUrl);
              dds.setUsername(username);
              dds.setPassword(password);
              dds.setConnectionProperties(connectionProperties);
               dds.setInitialSize(initialSize);
               dds.setMinIdle(minIdle);
               dds.setMaxActive(maxActive);
               dds.setMaxWait(60000);
               dds.setTimeBetweenEvictionRunsMillis(180000);
               dds.setMinEvictableIdleTimeMillis(600000);
               dds.setValidationQuery("SELECT 'x' from dual");
               dds.setTestWhileIdle(testWhileIdle);
               dds.setTestOnBorrow(testOnBorrow);
               dds.setTestOnReturn(testOnReturn);
               dds.setPoolPreparedStatements(poolPreparedStatements);
               dds.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
              try {
                  dds.setFilters(filters);
              } catch (Exception e) {
              }
              return dds;
          }
    
           @Bean(name = "ordersTransactionManager")
           public DataSourceTransactionManager ordersTransactionManager() {
               return new DataSourceTransactionManager(dataSource());
           }
    
           @Bean(name = "ordersSqlSessionFactory")
           public SqlSessionFactory ordersSqlSessionFactory(@Qualifier("ordersDataSource") DataSource ordersDataSource)
                   throws Exception {
               final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
               sessionFactory.setDataSource(ordersDataSource);
      
               return sessionFactory.getObject();
           }
    
       }
    

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