多數據源配置

項目中由於需要訪問不同的數據庫,所以增加了多數據源配置。

單獨數據源的時候。在application.yml中,會匹配路徑和dev文件中的數據庫配置,但是多數據源之後,application.yml中的mybatis會失效

##mybatis
mybatis:
  mapperLocations: classpath*:mappers/*/*.xml
  configuration:
    mapUnderscoreToCamelCase: true

這時候需要增加MybatisConfig和DataSourceConfig 類來對兩個數據源進行控制,分清主次數據源和指定數據源配置和路徑。

目前多數據源配置是沒問題的,但是存在數據庫連接8小時後會斷開。目前還未解決。

public class MybatisConfig {

    @Configuration

    @MapperScan(basePackages = {"net.wecash.histore.order.currentGoSysRepository"},

            sqlSessionFactoryRef = "sqlSessionFactoryTwo",

            sqlSessionTemplateRef ="sqlSessionTemplateTwo" )

    public static class DBTwo{

        @Autowired

        @Qualifier("secondDatasource")

        DataSource secondDatasource;

 

 

        @Bean

        public SqlSessionFactory sqlSessionFactoryTwo() throws Exception {

            System.out.println("副配");

            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

            factoryBean.setDataSource(secondDatasource);

            factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:currentGoSysMappers/*.xml"));

            factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

            return factoryBean.getObject();

 

        }

 

        @Bean

        public SqlSessionTemplate sqlSessionTemplateTwo() throws Exception {

            SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTwo()); // 使用上面配置的Factory

            return template;

        }

    }

    @Configuration

    @MapperScan(basePackages = {"net.wecash.histore.order.repository"},

            sqlSessionFactoryRef = "sqlSessionFactoryOne",

            sqlSessionTemplateRef = "sqlSessionTemplateOne")

    public static  class DBOne{

        @Autowired

        @Qualifier("dataSource")

        DataSource dataSource;

 

        @Bean

        public SqlSessionFactory sqlSessionFactoryOne() throws Exception {

            System.out.println("主配");

            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();

            factoryBean.setDataSource(dataSource);

            factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));

            factoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

            return factoryBean.getObject();

 

        }

 

        @Bean

        public SqlSessionTemplate sqlSessionTemplateOne() throws Exception {

            SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryOne()); // 使用上面配置的Factory

            return template;

        }

    }

 

}

 

 

//這塊需要在數據源那配置兩個數據源。然後主配添加 @Primary,通過prefix進行指定數據源

@Configuration

public class DataSourceConfig {

    private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);

 

    @Bean(name = "dataSource")

    @Primary

    @Qualifier("dataSource")

    @ConfigurationProperties(prefix = "spring.datasource") // application.properteis中對應屬性的前綴

    public DataSource primaryDataSource() {

        logger.info("第一個數據庫連接池創建中.......");

        return DataSourceBuilder.create().build();

    }

 

 

    @Bean(name = "secondDatasource")

    @Qualifier("secondDatasource")

    @ConfigurationProperties(prefix = "spring.second-datasource") // application.properteis中對應屬性的前綴

    public DataSource dbTwoDataSource() {

        logger.info("第二個數據庫連接池創建中......");

        return DataSourceBuilder.create().build();

    }

}

 

 

spring:
  datasource:
    url: jdbc:mysql://loaclhost:3306/one?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
    name: toot-
    username: root
    password: root
    druid:
      filters: stat
      maxActive: 20
      initialSize: 1
      maxWait: 60000
      minIdle: 1
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 'x'
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 20
#            wait_timeout : 31536000
#            interactive_timeout : 31536000
      testConnectionOnCheckin : true
      #      如果設爲true那麼在取得連接的同時將校驗連接的有效性
      numTestsPerEvictionRun :  10
      #        在每次空閒連接回收器線程(如果有)運行時檢查的連接數量
      removeAbandoned : true
      #        連接泄漏回收參數,當可用連接數少於3個時才執行
      removeAbandonedTimeout :  180
      #        連接泄漏回收參數,180秒,泄露的連接可以被刪除的超時值
      idleConnectionTestPeriod : 600
      #       用來配置測試空閒連接的間隔時間
      testConnectionOnCheckout : true
  second-datasource:

url: jdbc:mysql://localhost:3306/two?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

name: two
username: root
password: root
druid:
filters: stat
maxActive: 50
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
# wait_timeout : 31536000
# interactive_timeout : 31536000
testConnectionOnCheckin : true
# 如果設爲true那麼在取得連接的同時將校驗連接的有效性
numTestsPerEvictionRun : 10
# 在每次空閒連接回收器線程(如果有)運行時檢查的連接數量
removeAbandoned : true
# 連接泄漏回收參數,當可用連接數少於3個時才執行
removeAbandonedTimeout : 180
# 連接泄漏回收參數,180秒,泄露的連接可以被刪除的超時值
idleConnectionTestPeriod : 600
# 用來配置測試空閒連接的間隔時間
testConnectionOnCheckout : true

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章