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

在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host、需要使用多种数据库(MySql、Oracle、SqlServer...)

如果使用springboot开发,可做如下配置:

Config:

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

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

    @Bean(name = "formalDataSource")
    @Qualifier("formalDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.formal.mysql")
    public DataSource formalDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name="testJdbcTemplate")
    public JdbcTemplate testJdbcTemplate (
            @Qualifier("testDataSource")  DataSource testDataSource ) {
        return new JdbcTemplate(testDataSource);
    }

    @Bean(name = "formalJdbcTemplate")
    public JdbcTemplate formalJdbcTemplate(
            @Qualifier("formalDataSource") DataSource formalDataSource){
        return new JdbcTemplate(formalDataSource);
    }
}

配置文件 application.properties

spring.datasource.hikari.mysql.jdbc-url =jdbc:mysql://mysql2.cdqdops.org:3306/standard?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
spring.datasource.hikari.mysql.username = root
spring.datasource.hikari.mysql.password = 123456
spring.datasource.hikari.mysql.driver-class-Name = com.mysql.jdbc.Driver

spring.datasource.formal.mysql.jdbc-url =jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
spring.datasource.formal.mysql.username = root
spring.datasource.formal.mysql.password = 1314
spring.datasource.formal.mysql.driver-class-Name = com.mysql.jdbc.Driver

注意事项

使用多个数据源时,需要添加@Primary注解

@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者

当然,primary意味着"主要的",类似与SQL语句中的"primary key",有且只能有一个

开发时,别人将注释掉的代码恢复,出现了多个"@Primary",就导致了如下错误:

[root@app4 logs]# tail stdout.log
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [mysqlDataSource, formalDataSource, sqlServerDataSource]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.determinePrimaryCandidate(DefaultListableBeanFactory.java:1381)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.determineAutowireCandidate(DefaultListableBeanFactory.java:1341)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1110)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getIfUnique(DefaultListableBeanFactory.java:1728)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.getDataSourceInitializer(DataSourceInitializerInvoker.java:100)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:62)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
    ... 32 common frames omitted

原文地址:https://www.cnblogs.com/rosa-king/p/10482954.html

时间: 2024-08-27 18:27:47

springboot配置多数据源(JdbcTemplate方式)的相关文章

spring mvc配置 + dbcp数据源+jdbcTemplate

spring mvc配置 + dbcp数据源+jdbcTemplate 最近打算仔细研究一下spring,就从用了2年的spring mvc开始吧,初学者可以看看,大神就pass好了,呵呵.... 首先去spring官网下载完整的spring包,包含libs, docs和schema,spring的版本是3.2.4 我们来看一下spring的lib包都有那些内容: 上面图片中除红色框内的两个jar其它都是spring官方提供的jar包,红色框内的jar我们在配置事务的时候会用到,我们一会再说.我

springboot配置多数据源

在做项目的过程中难免会遇到这种情况:一个项目需要两个数据库中的数据,希望这篇文章能给遇到这些问题的小伙伴一点帮助 第一步:将两个数据源的mapper接口和xml文件分别放入不同的文件夹下: 第二步:在application.yml文件中加入双数据源,一定要指定主数据源,不然会报错 spring:   datasource:     primary:       driver-class-name: com.mysql.jdbc.Driver       url: url地址       user

SpringBoot配置Druid数据源

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

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上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式

1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture") @ResponseBody public JSONResultUtil uploadPicture(MultipartFile file, Integer viewId) { if (file == null) { return JSONResultUtil.error("文件没接到&q

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的

Spring动态配置多数据源

Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时的请求及系统状态来动态的决定将数据存储在哪个数据库实例中,以及从哪个数据库提取数据. 基本信息 1.Spring配置多数据源的方式和具体使用过程. 2.Spring对于多数据源,以数据库表为参照,大体上可以分成两大类情况: 一是,表级上的跨数据库.即,对于不同的数据库却有相同的表(表名和表结构完全相