springboot配置多数据源

在做项目的过程中难免会遇到这种情况:一个项目需要两个数据库中的数据,希望这篇文章能给遇到这些问题的小伙伴一点帮助

第一步:将两个数据源的mapper接口和xml文件分别放入不同的文件夹下;

第二步:在application.yml文件中加入双数据源,一定要指定主数据源,不然会报错

spring:
  datasource:
    primary:
      driver-class-name: com.mysql.jdbc.Driver
      url: url地址
      username: 用户名
      password: 密码
    secondary:
      driver-class-name: com.mysql.jdbc.Driver
      url: url地址
      username: 用户名
      password: 密码

第三步:config类:

@Configuration
@MapperScan(basePackages = "数据源1的mapper路径:com.dao.mapper.interface1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
public class DataSource1Config {

    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    @Primary
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/数据源1/*.xml"));//指定mapper.xml路径
        return bean.getObject();
    }

    @Bean(name = "test1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

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

}
@Configuration
@MapperScan(basePackages = "数据源2的mapper路径:com.dao.mapper.interface2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
public class DataSource2Config {

    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/数据源2/*.xml"));//指定mapper.xml路径
        return bean.getObject();
    }

    @Bean(name = "test2TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "test2SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

此时,项目是可以运行起来的,但是MySql有一个问题,就是配置双数据源后,当连接池空闲时间超过8小时,数据库连接就会自动断掉,为了避免这种情况,还需要在yml文件下加入如下配置:

datasource:
    primary:
        max-idle: 10
        max-wait: 10000
        min-idle: 5
        initial-size: 5
        validation-query: SELECT 1
        test-on-borrow: false
        test-while-idle: true
        time-between-eviction-runs-millis: 18800 #就是这句话
    secondary:
              max-idle: 10
              max-wait: 10000
              min-idle: 5
              initial-size: 5
              validation-query: SELECT 1
              test-on-borrow: false
              test-while-idle: true
              time-between-eviction-runs-millis: 18800 #就是这句话

目前配置双数据源已经完成了。

当然,上面的配置是针对使用默认数据源的配置,但是还有很多童鞋使用了阿里的druid数据源,两种原理基本相同,只是稍作改动:

spring:
  datasource:
   druid: 
    primary:
      driver-class-name: com.mysql.jdbc.Driver
      url: url地址
      username: 用户名
      password: 密码
    secondary:
      driver-class-name: com.mysql.jdbc.Driver
      url: url地址
      username: 用户名
      password: 密码

在config中作如下修改:

将 DataSourceBuilder 改为 DuridDataSourceBuilder

原文地址:http://blog.51cto.com/11864647/2067708

时间: 2024-08-30 12:56:28

springboot配置多数据源的相关文章

SpringBoot配置Druid数据源

在我刚开始接触JDBC的时候,用的是DriveManager驱动来连接数据库的.而现在大多是用DataSource. 这里先简单说一下区别: 1.datasource是与连接池获取连接,而DriverManager是获取与数据库的连接!DriverManager类的主要作用是管理注册到DriverManager中的JDBC驱动程序,并根据需要使用JDBC驱动程序建立与数据服务器的网络连接.但是建立与数据库的连接是一项较耗资源的工作,频繁的进行数据库连接建立操作会产生较大的系统开销,为了解决上述问

springboot配置多数据源(JdbcTemplate方式)

在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host.需要使用多种数据库(MySql.Oracle.SqlServer...) 如果使用springboot开发,可做如下配置: Config: import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; imp

springboot 配置多数据源

第一步:编写application.yml配置文件 spring: datasource: system: jdbc-url: jdbc:oracle:thin:@localhost:1521/orcl driver-class-name: oracle.jdbc.OracleDriver username: system password: 1234 initial-size: 5 min-idle: 5 max-active: 20 min-evictable-idle-time-milli

SpringBoot配置 druid 数据源配置 慢SQL记录

spring: datasource: url: jdbc:mysql://127.0.0.12:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull username: root password: root druid: initialSize: 5 application: name: message-center secur

springboot配置多数据源mongodb

参考大佬的文章 https://juejin.im/entry/5ab304dd51882555825241b3 原文地址:https://www.cnblogs.com/zhoujl-5071/p/10427174.html

SpringBoot Mybatis双数据源的配置

近期因为公司项目的需要,用上了maven和Springboot,对于java开发这块,早闻maven是个好东西,但一直没有去用,感觉用maven帮我们自己做了太多的事情,一个项目跑起来都不知道背后做了些什么,现在想想,可能那个时候脑子进水了吧. Springboot作为Spring的简约版(我是这么叫的,没有任何依据),将原来Spring需要做的配置文件,改为了注解,提供了大量的***start组件来帮我们做了很多初始化的处理. 但是今天我想要分享的不是maven如何搭建一个helloword的

SpringBoot+Druid+Mybatis配置多数据源

我们在开发一个项目的时候,可能会遇到需要对多个数据库进行读写的需求,这时候就得在项目中配置多个数据源了.在Java项目的开发中,目前最常用的数据操作框架是 Mybatis,开发框架也都基本用上了SpringBoot.而Druid号称最好的数据库连接池,自然也是被广泛使用. 所以本文将演示一下,SpringBoot+Druid+Mybatis如何去配置多数据源.首先在IDEA中创建一个SpringBoot工程: 选择一些基本的包: 完成创建: pom.xml配置的依赖如下: <dependenci

springboot之多数据源配置JdbcTemplate

springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.

springboot+mybatis+Druid配置多数据源(mysql+postgre)

springboot+mybatis+Druid配置多数据源(mysql+postgre)引入pom依赖设置application多数据源config配置db1config配置(主数据库配置)db2config配置(其他数据库)事务处理mapper层 springboot+mybatis+Druid配置多数据源(mysql+postgre) 参考资料: 第八章 springboot + mybatis + 多数据源 springboot + mybatis + druid + 多数据源 spri