美文网首页程序员
SpringBoot、Mybatis和Druid配置多数据源

SpringBoot、Mybatis和Druid配置多数据源

作者: 戴墨镜的吉娃娃 | 来源:发表于2019-02-23 22:31 被阅读0次

    最近做项目需要连两个不同地址的数据库,于是研究了一下Spring Boot如何配置多数据源。

    • 在application.properties配置
    #主数据源
    primary.spring.datasource.type =
    primary.spring.datasource.driver-class-name =
    primary.spring.datasource.url =
    primary.spring.datasource.username =
    primary.spring.datasource.password =
    #其他数据源
    secondary.spring.datasource.type =
    secondary.spring.datasource.driver-class-name =
    secondary.spring.datasource.url =
    secondary.spring.datasource.username =
    secondary.spring.datasource.password =
    
    • @Configuration进行自定义配置
      这里我将需要连主数据源mybatis中的mapper文件放在online.chenke.primary包中,第二个数据源放在online.chenke.secondary包中
      • 在DataSourceConfig中配置主数据源
    @Configuration
    @MapperScan(value = "online.chenke.primary", sqlSessionFactoryRef =  "sessionFactory")
    public class DataSourceConfig {
        @Value("${primary.spring.datasource.type}")
        private Class<? extends DataSource> dataSourceType;
    
        @Bean("dataSourceProperties")
        @Primary
        @ConfigurationProperties("primary.spring.datasource")
        public DataSourceProperties dataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean("dataSource")
        @Primary
        @ConfigurationProperties("primary.spring.datasource")
        public DataSource dataSource(@Qualifier("dataSourceProperties") DataSourceProperties dataSourceProperties) {
            return dataSourceProperties.initializeDataSourceBuilder().type(dataSourceType).build();
        }
    
        @Bean("sessionFactory")
        @Primary
        @ConfigurationProperties("primary.spring.datasource")
        public SqlSessionFactory sessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
    
            return sqlSessionFactoryBean.getObject();
        }
    }
    
    • 在SecondaryDataSourceConfig中配置第二个数据源
    @Configuration
    @MapperScan(value = "online.chenke.secondary", sqlSessionFactoryRef =  "secondarySessionFactory")
    public class SecondaryDataSourceConfig {
        @Value("${secondary.spring.datasource.type}")
        private Class<? extends DataSource> dataSourceType;
    
        @Bean("secondaryDataSourceProperties")
        @ConfigurationProperties("secondary.spring.datasource")
        public DataSourceProperties dataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean("secondaryDataSource")
        @ConfigurationProperties("secondary.spring.datasource")
        public DataSource dataSource(@Qualifier("secondaryDataSourceProperties") DataSourceProperties dataSourceProperties) {
            return dataSourceProperties.initializeDataSourceBuilder().type(dataSourceType).build();
        }
    
        @Bean("secondarySessionFactory")
        @ConfigurationProperties("secondary.spring.datasource")
        public SqlSessionFactory sessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            sqlSessionFactoryBean.setDataSource(dataSource);
    
            return sqlSessionFactoryBean.getObject();
        }
    }
    

    配置完成,正常使用即可

    相关文章

      网友评论

        本文标题:SpringBoot、Mybatis和Druid配置多数据源

        本文链接:https://www.haomeiwen.com/subject/qgymyqtx.html