SpringBoot+Mybatis集成多数据源配置及使用教程

最近需要在项目中访问其它数据库,就想到使用多数据源实现,进而不影响以前的整理逻辑,也不需要修改以前的数据库,于是就网上找了一下,自己顺便写个博客记录一下!

修改配置文件

spring:
  datasource:
    first:
      jdbc-url: jdbc:mysql://:3306/?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
      username: 
      password: 
      driver-class-name: com.mysql.jdbc.Driver
    second:
      jdbc-url: jdbc:mysql://:3306/?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&serverTimezone=GMT%2B8
      username: 
      password: 
      driver-class-name: com.mysql.jdbc.Driver
    druid:
      max-active: 30
      pool-prepared-statements: false
      test-while-idle: true
      validation-query: select 1

配置文件中需要注意的地方是jdbc-url,一定要改成这个启动项目才可以成功,不然回报数据库url问题!

增加配置文件

配置文件的作用是保证Mybatis的相关文件都是与正确的数据库相连接的,如果没有配置文件项目启动时进行mapperScan时会报找不到数据库连接,因为不知道连接哪个?

/**
 * @author chenlingl
 */
@Configuration
@MapperScan(basePackages = "com.....mapper", sqlSessionFactoryRef = "firstSqlSessionFactory")
public class FirstDataSourceConfig {
    @Bean(name = "firstDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.first")
    public DataSource firstDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "firstSqlSessionFactory")
    @Primary
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/mybatis.mapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "firstTransactionManager")
    @Primary
    public DataSourceTransactionManager firstTransactionManager(@Qualifier("firstDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

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

这里的配置文件需要分清哪个是主连接,哪个是副连接,我这里把first作为主连接,所以相关数据库配置文件都会加上@Primary(一定要加,不然启动也会直接报错!!!)

/**
 * @author chenlingl
 */
@Configuration
@MapperScan(basePackages = "com.....secondMapper", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {
    @Bean(name = "secondDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource secondDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondSqlSessionFactory")
    public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:META-INF/mybatis.secondMapper/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "secondTransactionManager")
    public DataSourceTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

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

创建两个配置文件之后,当需要连接第二个数据库时,可以在第二个数据库的secondMapper中创建mapper文件以及在mybatis.secondMapper中创建mapper.xml文件,这样就能保证以前的逻辑不需要修改,只要新增需要增加的mapper即可!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值