springboot配置多数据源

ぃ、小莉子 提交于 2020-01-24 03:30:47

springboot配置多数据源

  1. 配置yml文件多数据源
spring:
  datasource:
    ## A数据源配置
    A:
      jdbc-url: jdbc:XXX://XXXXXX:XXXX/XXX
      username: AAA
      password: AAA
      driverClassName:com.mysql.cj.jdbc.Driver
    ## B数据源配置
    B:
      jdbc-url:  jdbc:XXX://XXXXXX:XXXX/XXX
      username: BBB
      password: BBB
      driverClassName: com.mysql.cj.jdbc.Driver
  mybatis:
      mapperLocations: classpath:mapper/A/*.xml,classpath:mapper/B/*.xml,
      type-aliases-package: com.xxx.model
  1. 一共要配置2种文件
  • DataSourceConfig.java——罗列n个数据源
  • XDBConfig.java——x数据源具体配置
  1. DataSourceConfig文件代码
    罗列设置n个数据源
@Configuration
public class DataSourceConfig {
	//只有一个主数据源
    @Bean(name = "ADataSource")
    @Qualifier("ADataSource")
    @Primary // 定义主数据源
    @ConfigurationProperties(prefix = "spring.datasource.A")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    //B数据源
    @Bean(name = "BDataSource")
    @Qualifier("BDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.B")
    public DataSource thirdDataSource() {
        return DataSourceBuilder.create().build();
    }

}
  1. A数据源的具体配置
@Configuration
@MapperScan(basePackages = "com.xxx.mapper.A", sqlSessionTemplateRef = "ASqlSessionTemplate")
public class LipsDBConfig {
    @Bean(name = "ASqlSessionFactory")
    @Primary
    public SqlSessionFactory lipsSqlSessionFactory(@Qualifier("ADataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        try {
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/A/*.xml"));
        } catch (Exception e) {
            bean.setMapperLocations(null);
        }

        return bean.getObject();
    }

    //配置声明式事务管理器
    @Bean(name = "ATransactionManager")
    @Primary
    public PlatformTransactionManager lipsTransactionManager(@Qualifier("ADataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ASqlSessionTemplate")
    @Primary
    public SqlSessionTemplate lipsSqlSessionTemplate(
            @Qualifier("ASqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
  1. B数据源的具体配置
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = "com.xxx.mapper.B", sqlSessionTemplateRef = "BSqlSessionTemplate")
public class OipsDBConfig {

    @Bean(name = "BSqlSessionFactory")
    public SqlSessionFactory oipsSqlSessionFactory(@Qualifier("BDataSource") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        try{
            bean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath:mapper/B/*.xml"));
        }catch (Exception e){
            bean.setMapperLocations(null);
        }
        return bean.getObject();
    }

    @Bean(name = "BTransactionManager")
    public PlatformTransactionManager oipsTransactionManager(
            @Qualifier("BDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "BSqlSessionTemplate")
    public SqlSessionTemplate oipsSqlSessionTemplate(
            @Qualifier("BSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

文件层级是
com
=xxx
==controller
==config->多数据源配置文件
==mapper
===A->A数据源的mapper
===B->B数据源的mapper
resource
=mapper
==A->A数据源的xml
==B->B数据源的xml
.yml->配置文件

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!