Spring Boot 配置双数据源

2018-09-10  本文已影响0人  夏_0327

application.yml


# 数据源配置
first:
    datasource:
        type: org.apache.commons.dbcp.BasicDataSource
        driverClassName: oracle.jdbc.driver.OracleDriver
        url: jdbc:oracle:thin:@192.168.100.200:1521:orcl
        username: test
        password: test123
        #连接池配置
        dbcp2:
            maxWait: 120000
            minIdle: 5
            maxIdle: 250
            maxActive: 250
            initial-size: 5
            #以下几条必须配置,防止服务器因网络原因,导致数据库连接失败
            time-between-eviction-runs-millis: 60000 
            min-evictable-idle-time-millis: 120000
            validation-query: SELECT 1 FROM DUAL
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false

second:
    datasource:
        type: org.apache.commons.dbcp.BasicDataSource
        driverClassName: oracle.jdbc.driver.OracleDriver
        url: jdbc:oracle:thin:@192.168.100.200:1521:orcl
        username: test1
        password: test123
        #连接池配置
        dbcp2:
            maxWait: 120000
            minIdle: 5
            maxIdle: 250
            maxActive: 250
            initial-size: 5
            #以下几条必须配置,防止服务器因网络原因,导致数据库连接失败
            time-between-eviction-runs-millis: 60000 
            min-evictable-idle-time-millis: 120000
            validation-query: SELECT 1 FROM DUAL
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false

FirstDataSource 配置,注意指定数据源作用的 basePackages

/**
* 数据源配置文件
*
* @author ysguo
* @date 2018-09-07
*/
@Configuration
@MapperScan(basePackages = {"com.ysguo.test1.mapper", "com.ysguo.test2.mapper"}, sqlSessionTemplateRef = "firstSqlSessionTemplate")

public class FirstDataSourceConfig {

    /**
    * 数据源
    *
    * @return dataSource
    */
    @Bean(name = "firstDataSource")
    @Primary //必须加此注解
    @ConfigurationProperties(prefix = "first.datasource") // prefix值必须是application.properteis中对应属性的前缀
    public DataSource firstDataSource() {

        return DataSourceBuilder.create().build();

    }

    /**
    * SqlSessionFactory
    *
    * @param dataSource
    *            数据源
    *
    * @return SqlSessionFactory
    */
    @Bean
    public SqlSessionFactory firstSqlSessionFactory(
            @Qualifier("firstDataSource") DataSource dataSource) throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    /**
    * 事务管理器
    *
    * @param dataSource
    *            数据源
    *
    * @return 事务管理器
    */
    @Bean(name = "firstDataSourceTransactionManger")
    public DataSourceTransactionManager masterTransactionManger(
            @Qualifier("firstDataSource") DataSource dataSource) {

        return new DataSourceTransactionManager(dataSource);

    }

    /**
    * SqlSessionTemplate
    *
    * @param sqlSessionFactory
    *            sqlSessionFactory
    *
    * @return SqlSessionTemplate
    */
    @Bean
    public SqlSessionTemplate firstSqlSessionTemplate(
            @Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {

        return new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory

    }
}

SecondDataSource 配置,注意指定数据源作用的 basePackages

/**
* 数据源配置文件
*
* @author ysguo
* @date 2018-09-07
*/
@Configuration
@MapperScan(basePackages = {"com.ysguo.test3.mapper"}, sqlSessionTemplateRef = "secondSqlSessionTemplate")
public class SecondDataSourceConfig {

    /**
    * 数据源
    *
    * @return 数据源
    */
    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "second.datasource") // prefix值必须是application.properteis中对应属性的前缀
    public DataSource secondDataSource() {

        return DataSourceBuilder.create().build();

    }



    /**
    * SqlSessionFactory
    *
    * @param dataSource
    *            数据源
    *
    * @return SqlSessionFactory
    */
    @Bean
    public SqlSessionFactory secondSqlSessionFactory(
            @Qualifier("secondDataSource") DataSource dataSource) throws Exception {

        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }


    /**
    * 事务管理器
    *
    * @param dataSource
    *            数据源
    *
    * @return 事务管理器
    */
    @Bean(name = "secondDataSourceTransactionManger")
    public DataSourceTransactionManager masterTransactionManger(
            @Qualifier("secondDataSource") DataSource dataSource) {

        return new DataSourceTransactionManager(dataSource);

    }


    /**
    * SqlSessionTemplate
    *
    * @param sqlSessionFactory
    *            sqlSessionFactory
    *
    * @return SqlSessionTemplate
    */
    @Bean
    public SqlSessionTemplate secondSqlSessionTemplate(
            @Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {

        return new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory

    }
}

上一篇下一篇

猜你喜欢

热点阅读