在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个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. 鸟哥的Linux私房菜-----10、学习Bash

  2. LeetCode 939. Minimum Area Rectangle (最小面积矩形)

    题目标签:HashMap 题目给了我们一组 xy 上的点坐标,让我们找出 能组成矩形里最小面积的那个. 首先遍历所有的点,把x 坐标当作key 存入map, 把重复的y坐标 组成set,当作value ...

  3. 如何利用UDP协议封装一个数据包

    在如何封装一个数据包上,是一个非常细致的问题,而利用UDP协议来封装的话,是比较简单,让我们一步步来分析典型的TCP/IP协议.一般来说一个典型的一个数据包,包括以太网MAC头+网络层IP数据头+传输 ...

  4. shell curl 实现rest 并发测试

    for i in {1..50}; do curl http://10.43.95.26:5812/rdk/service/app/example/server/my_service & do ...

  5. Android 自带Base64加密解密

    Android项目引用不到以下两个java类 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; Android有自己的base ...

  6. [Codeforces Round495B] Sonya and Exhibition

    [题目链接] https://codeforces.com/contest/1004/problem/B [算法] 不难发现 , 最优解一定是01010101.... 时间复杂度 : O(N) [代码 ...

  7. 64. Extjs中grid 的ColumnModel 属性配置

    转自:https://blog.csdn.net/u011530389/article/details/45821945 本文导读:Ext.grid.ColumnModel 该类用于定义表格的列模型, ...

  8. E20171028-hm

    stick   n. 棍棒,棍枝; 枝条; 操纵杆; 球棍; stopwatch  n. (赛跑等记时用的) 秒表,跑表; agency   n. 代理; 机构; 力量; various   adj. ...

  9. Java 集合 ArrayList 需要知道的几个问题

    问:Arraylist 的动态扩容机制是如何自动增加的?简单说说你理解的流程? 答:当在 ArrayList 中增加一个对象时 Java 会去检查 Arraylist 以确保已存在的数组中有足够的容量 ...

  10. jeecg中列表查询数据关联其他表的显示

    1.A表字段:id,name;B表字段:id,name,fid(A表外键),现查询A表和B表的所有数据并且查询条件A,B都有,在前台页面list显示 2.后台方法: @RequestMapping(p ...