springboot多数据源配置使用


配置application.yml

spring:
  datasource:
#   第一个数据源
    first:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.41.128:3306/first?useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: 123456
    #   第二个数据源
    second:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://172.16.0.9:3306/second?useUnicode=true&characterEncoding=utf8&useSSL=false
      username: root
      password: 123456

获取DB配置

@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.first")
public class FirstDataBaseProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}
@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.second")
public class SecondDataBaseProperties {
    private String url;
    private String username;
    private String password;
    private String driverClassName;
}

配置数据源

@Configuration
// 第一个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.first",sqlSessionTemplateRef ="firstSqlSessionTemplate")
public class FirstDataSourceConfig {
    @Autowired
    FirstDataBaseProperties firstDataBaseProperties;

    @Bean(name = "firstDS")
    // 多数据源情况下需要指定主数据源
    @Primary
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(firstDataBaseProperties.getDriverClassName())
                .url(firstDataBaseProperties.getUrl())
                .username(firstDataBaseProperties.getUsername())
                .password(firstDataBaseProperties.getPassword())
                .build();
        return build;
    }

    @Bean(name = "firstSqlSessionFactory")
    @Primary
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("firstTransactionManger")
    @Primary
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("firstDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "firstSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
@Configuration
// 第二个数据源与Mapper接口绑定
@MapperScan(basePackages = "com.deri.task.dao.second",sqlSessionTemplateRef ="secondSqlSessionTemplate")
public class FirstDataSourceConfig {
    @Autowired
    SecondDataBaseProperties secondDataBaseProperties;

    @Bean(name = "secondDS")
    public DataSource getFirstDataSource() {
        DataSource build =  DataSourceBuilder.create()
                .driverClassName(secondDataBaseProperties.getDriverClassName())
                .url(secondDataBaseProperties.getUrl())
                .username(secondDataBaseProperties.getUsername())
                .password(secondDataBaseProperties.getPassword())
                .build();
        return build;
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }

    @Bean("secondTransactionManger")
    public DataSourceTransactionManager firstTransactionManger(@Qualifier("secondDS") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    // 创建SqlSessionTemplate

    @Bean(name = "secondSqlSessionTemplate")
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

参考链接

已知问题

  • mybatis驼峰命名转换失效问题
    # 默认在application.yml中配置,但是在多数据源场景下该配置失效,需要在代码中显式指定.
    mybatis:
    configuration:
      map-underscore-to-camel-case: true
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("secondDS") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    // 指定驼峰转换
    bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return bean.getObject();
}

另一种配置方法

不用创建FirstDataBasePropertiesSecondDataBaseProperties.其余保持一样.

    @Primary
    @Bean(name = "publicDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.public")
    public DataSourceProperties publicDataSourceProperties() {
        return new DataSourceProperties();
    }
    @Bean(name = "publicDS")
    @Primary
    public DataSource getFirstDataSource(@Qualifier("publicDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

文章作者: wuzhiyong
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 wuzhiyong !
评论
 上一篇
semaphore信号量使用 semaphore信号量使用
锁的说明java中锁有Synchronized、ReentrantLock,但是这两种锁一次只能允许一个线程访问资源,而信号量semaphore可以控制多个线程同时获取资源,实现更高级别的限流. semaphore使用// 创建信号量,pe
2020-12-04
下一篇 
linux查询目录下指定格式最大文件 linux查询目录下指定格式最大文件
命令# 指定格式 ls -lSh `find /var/lib/docker -type f -name "*.log"` | head -n 5 # 所有文件格式按大小排序 find /var/lib/docker -type f -pr
2020-11-23
  目录