springboot多数据源配置,看这一篇就够了

springboot下多数据源配置实现

不管是两个mysql,还是一个mysql一个oracle,都是一样的操作

目录

  • springboot下多数据源配置实现
    • 配置application.yml文件
    • 数据源配置类
    • 创建mapper接口
    • 创建mapper的xml配置文件
  • 你可能会遇到的问题

配置application.yml文件

application.properties也是一样的

spring:
  datasource:
    db1:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      url: jdbc:oracle:thin:@//localhost:1521/orcl
      username: crm_user
      password: 123456
    db2:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      url: jdbc:oracle:thin:@//localhost:1521/orcl
      username: root
      password: 123456

数据源配置类

  1. 创建一个config包,在这个包下创建db1的数据源配置类
@Configuration
@MapperScan(basePackages = "com.chenyx.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class UserDataSourceConfig {
    @Primary // 表示这个数据源是默认数据源,可以加也可以不加
    @Bean("db1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db1")  // 读取application.yml中的配置参数映射成为一个对象
    public DataSource getUserDBDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean("db1SqlSessionFactory")
    public SqlSessionFactory userDBSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml配置文件位置
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
        return bean.getObject();
    }
    
    @Primary
    @Bean("db1SqlSessionTemplate")
    public SqlSessionTemplate userDBSqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

其中,basePackages是你在该数据源下的mapper接口文件存放的包名

  1. 同理,创建一个db2的数据源配置类
@Configuration
@MapperScan(basePackages = "com.chenyx.mapper.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class HrmDataSourceConfig {

    
    @Bean("db2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.db2")  // 读取application.yml中的配置参数映射成为一个对象
    public DataSource getUserDBDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean("db2SqlSessionFactory")
    public SqlSessionFactory hrmDBSqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
        return bean.getObject();
    }

    @Bean("db2SqlSessionTemplate")
    public SqlSessionTemplate hrmDBSqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

创建mapper接口

根据前面的数据源配置类中的包名,需要创建一个mapper,再分别创建一个db1和db2的包,放对应的mapper接口

在这里插入图片描述

创建mapper的xml配置文件

在resources下创建一个mapper包,同样也是要与数据源配置类中的路径一致

在这里插入图片描述

你可能会遇到的问题

我们在创建数据表时,字段常常会带有下划线符号,而我们在类中对应的字段是用驼峰命名,如果不配置mybatis,那么查询出来的数据是没法找到对应字段的。

因此,在application.yml文件中加入如下配置

mybatis:
  configuration:
    mapUnderscoreToCamelCase: true

那么,到这里一般就结束了,但是我们配置两个数据源时手动配置了数据库连接,是读取不到这个配置的

解决办法:在数据源配置中增加配置,并在对应的SqlSessionFactory中进行设置

@Bean
@ConfigurationProperties(prefix = "mybatis.configuration")
public org.apache.ibatis.session.Configuration globalConfiguration() {
    return new org.apache.ibatis.session.Configuration();
}

@Bean("db1SqlSessionFactory")
public SqlSessionFactory userDBSqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setConfiguration(globalConfiguration());  // 将mybatis配置设置
    // mapper的xml形式文件位置必须要配置,不然将报错:no statement (这种错误也可能是mapper的xml中,namespace与项目的路径不一致导致)
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
    return bean.getObject();
}

其中,@ConfigurationProperties(prefix = "mybatis.configuration")就是yml文件配置的前缀

到此,你应该可以成功地调用两个数据库的数据了

你可能感兴趣的:(spring,boot,Java,MyBatis,spring,boot,后端,java,mybatis,mysql,oracle)