springboot配置两个mysql数据源

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

参考:https://www.jianshu.com/p/735852145580

maven依赖



  org.mybatis.spring.boot
  mybatis-spring-boot-starter
  1.2.0




  mysql
  mysql-connector-java
  5.1.39




  com.alibaba
  druid
  1.0.18

在application.yml文件中配置两个数据源;如

spring:
 datasource:
    url: jdbc:mysql://aaaaaaaaa/aaaaaaaaaa?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
    username: aaaaa
    password: aaaaaaaaaa
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

改为:

spring:
  datasource:
    aaaa:
      url: jdbc:mysql://aaaaaaaaaaa/aaaaaaaa?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
      username: root
      password: aaaaaaaaa
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource
    bbbb:
      url: jdbc:mysql://bbbbbbbbbbbbbb/bbbbbbbb?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
      username: root
      password: bbbbbbb
      driver-class-name: com.mysql.jdbc.Driver
      type: com.alibaba.druid.pool.DruidDataSource

这一步没什么好说的,毕竟两个数据源肯定有两套配置文件,加载的时候区分就好。

application.yaml的话:

# master 数据源配置
master.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
master.datasource.username=root
master.datasource.password=321
master.datasource.driverClassName=com.mysql.jdbc.Driver

# second 数据源配置
second.datasource.url=jdbc:mysql://localhost:3306/springbootdb_second?useUnicode=true&characterEncoding=utf8
second.datasource.username=root
second.datasource.password=321
second.datasource.driverClassName=com.mysql.jdbc.Driver

加载bean

第一个数据源:

@Configuration
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
 
    // 精确到 master 目录,以便跟其他数据源隔离
    static final String PACKAGE = "org.spring.springboot.dao.master";
    static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
//这里重点说一下,因为自己掉坑里了,我是mapper文件夹下有两个文件夹aa,bb,这里必须填到具体的文件夹,如mapper/aa或mapper/bb
 
    @Value("${master.datasource.url}")
    private String url;
 
    @Value("${master.datasource.username}")
    private String user;
 
    @Value("${master.datasource.password}")
    private String password;
 
    @Value("${master.datasource.driverClassName}")
    private String driverClass;
 
    @Bean(name = "masterDataSource")
    @Primary
    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }
 
    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }
 
    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MasterDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

第二个数据源SecondDataSourceConfig的配置如下:

@Configuration
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = SecondDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {

    // 精确到 cluster 目录,以便跟其他数据源隔离
    static final String PACKAGE = "com.springboot.dao.second";
    static final String MAPPER_LOCATION = "classpath:mapper/second/*.xml";

    @Value("${second.datasource.url}")
    private String url;

    @Value("${second.datasource.username}")
    private String user;

    @Value("${second.datasource.password}")
    private String password;

    @Value("${second.datasource.driverClassName}")
    private String driverClass;

    @Bean(name = "secondDataSource")
    public DataSource clusterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "secondTransactionManager")
    public DataSourceTransactionManager clusterTransactionManager() {
        return new DataSourceTransactionManager(clusterDataSource());
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("secondDataSource") DataSource clusterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clusterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(SecondDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}


剩下的就没啥了,博主讲的很详细,我说一下自己遇见的坑吧:

1,

static final String MAPPER_LOCATION = "classpath:mapper/second/*.xml";

我第一次这里配的不详细,我没有配到了master/和second/,只配到mapper/,以为会自动向下扫描,结果并没有。。。

2,@primary

DataSourceTransactionManager 
SqlSessionFactory 

都没有加@primary或者都加了

结论:连个配置类只有一个类可以加,springboot会优先选择这个

 

转载于:https://my.oschina.net/u/3796880/blog/3047175

你可能感兴趣的:(springboot配置两个mysql数据源)