在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个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);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
配置文件 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
1
2
3
4
5
6
7
8
9
注意事项
使用多个数据源时,需要添加**@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
1
2
3
4
5
6
7
8
9
10
11

---------------------

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

  1. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  2. SpringBoot配置多数据源时遇到的问题

    SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...

  3. springboot 配置多数据源

    1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...

  4. springboot 配置多数据源 good

    1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...

  5. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

  6. springboot配置多数据源mybatis配置失效问题

    mybatis配置 #开启驼峰映射 mybatis.configuration.map-underscore-to-camel-case=true #开启打印sql mybatis.configura ...

  7. Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)

    1.配置 orcale jdbc 对于一个Maven项目,使用Mysql时,可直接添加如下依赖: <dependency> <groupId>mysql</groupId ...

  8. springboot 配置多数据源实例代码(分包方式)

    目录 1.数据库 2.pom与yml 2.1.pom中的依赖部分 2.2.yml数据库配置部分 3.数据源配置类 DataSourceConfig 3.1.DataSourceConfig1.java ...

  9. SpringBoot配置多数据源

    原文:https://www.jianshu.com/p/033e0ebeb617 项目中用到了两个数据库,分别是Oracle和Mysql,涉及到了多数据源问题,这里做下记录 官方讲解:https:/ ...

随机推荐

  1. [Vue @Component] Pass Props Between Components with Vue Slot Scope & renderless component

    Components with slots can expose their data by passing it into the slot and exposing the data using  ...

  2. .net core2.0添加json文件并转化成类注入控制器使用 让js调试更简单—console

    .net core2.0添加json文件并转化成类注入控制器使用 上一篇,我们介绍了如何读取自定义的json文件,数据是读取出来了,只是处理的时候太麻烦,需要一遍一遍写,很枯燥.那么有没有很好的办法呢 ...

  3. 高效开发之SASS篇 灵异留白事件——图片下方无故留白 你会用::before、::after吗 link 与 @import之对比 学习前端前必知的——HTTP协议详解 深入了解——CSS3新增属性 菜鸟进阶——grunt $(#form :input)与$(#form input)的区别

    高效开发之SASS篇   作为通往前端大神之路的普通的一只学鸟,最近接触了一样稍微高逼格一点的神器,特与大家分享~ 他是谁? 作为前端开发人员,你肯定对css很熟悉,但是你知道css可以自定义吗?大家 ...

  4. phpstorm安装和调试

    首先: phpstorm是用JAVA开发的,所以在安装之前须要先安装jdk sudo apt-get install default-jdk 从官网上下载phpstorm 的linux版本号 http ...

  5. 《转》OpenStack对象存储——Swift

    OpenStack Object Storage(Swift)是OpenStack开源云计算项目的子项目之中的一个.被称为对象存储.提供了强大的扩展性.冗余和持久性.本文将从架构.原理和实践等几方面讲 ...

  6. js对象实例化的常见三种方式

    三种常见模式:工厂模式,构造函数模式,原型模式 <span style="font-size:18px;"><!doctype html> <html ...

  7. coffeescript遍历json对象

    直接给代码: headers = a:"this is a" ,b:"this is b" ,c:"this is c" exheaders ...

  8. Longest Increasing Subsequence HDU - 6284

    /* 首先预处理好f g数组 fi :以a[i]为结尾的 最长上升子序列的长度 gi :以a[i]为开始的 最长上升子序列的长度 mxx : 最长上升子序列的长度 线段树优化 nlogn (不包含a[ ...

  9. Cadence——每次启动软件弹出找不到license文件的提示窗口

    1. 摘要 按照Cadence16.60,每次启动该软件,总弹出提示窗口,内如大致为:Orcad Capture license was not found.... 2. 解决方法 参考此链接:htt ...

  10. .NET下WebBrowser的一个BUG以及其替代品——geckofx

    今天研究一个小问题,在C#的WebBrowser下打开奇艺网的视频,经常整个FLASH就偏了,进度条控制条什么的都没有. 要全屏一下然后还原才能解决这个问题. 如下,图1为webbrowser打开,图 ...