springboot配置双数据源

发布时间 2023-03-22 21:14:15作者: 山阴路的秋天

日常开发中,遇到需要从不同数据库中访问数据时,这时需要我们在项目中配置双数据源以满足开发需求。

配置结构如下:

 

 

1.yml 配置文件中配置双数据源链接地址

 datasource:
    database1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://链接地址1
      username: 用户名1
      password: 密码1
      # 使用druid数据源
      type: com.alibaba.druid.pool.DruidDataSource
      druid:
        # 连接池最大使用连接数
        max-active: 20
        # 初始化连接大小
        initial-size: 1
        # 获取连接最大等待时间
        max-wait: 60000
        # 连接池最小空闲
        min-idle: 3
        # 自动清除无用连接
        remove-abandoned: true
        # 清除无用连接的等待时间
        remove-abandoned-timeout: 180
        # 连接属性
        connection-properties: clientEncoding=UTF-8

    database2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://链接地址2
      username: 用户名2
      password: 密码2
      # 使用druid数据源
      type: com.alibaba.druid.pool.DruidDataSource
      druid:
        # 连接池最大使用连接数
        max-active: 20
        # 初始化连接大小
        initial-size: 1
        # 获取连接最大等待时间
        max-wait: 60000
        # 连接池最小空闲
        min-idle: 3
        # 自动清除无用连接
        remove-abandoned: true
        # 清除无用连接的等待时间
        remove-abandoned-timeout: 180
        # 连接属性
        connection-properties: clientEncoding=UTF-8
mybatis:
type-handlers-package:
type-aliases-package:
yml 中mybatis 配置不需要,因为我们将在config中去设置。

2、配置数据库的config 主数据源使用注解@Primary
@Configuration
@MapperScan(basePackages = "com.chunmi.mijia.migration.provider.microwave.oven.dao1", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DatabaseConfig1 {
    @Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.database1")
    @Primary
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory1")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper1/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "transactionManager1")
    @Primary
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource1") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplate1")
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

从数据源配置:

@Configuration
@MapperScan(basePackages = "com.chunmi.mijia.migration.provider.microwave.oven.dao2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DatabaseConfig2 {
    @Bean(name = "dataSource2")
    @ConfigurationProperties(prefix = "spring.datasource.database2")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper2/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "transactionManager2")
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionTemplate2")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

3.然后再对应的dao1 dao2,mapper1 mapper2进行业务代码编写就可以了。