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);
}
}
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方式)的更多相关文章
- springboot配置Druid数据源
springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...
- SpringBoot配置多数据源时遇到的问题
SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...
- springboot 配置多数据源
1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...
- springboot 配置多数据源 good
1.首先在创建应用对象时引入autoConfig package com; import org.springframework.boot.SpringApplication; import org. ...
- springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...
- springboot配置多数据源mybatis配置失效问题
mybatis配置 #开启驼峰映射 mybatis.configuration.map-underscore-to-camel-case=true #开启打印sql mybatis.configura ...
- Springboot配置多数据源(Mysql和Orcale)--(Idea Maven JDBCTemplate支持下的)
1.配置 orcale jdbc 对于一个Maven项目,使用Mysql时,可直接添加如下依赖: <dependency> <groupId>mysql</groupId ...
- springboot 配置多数据源实例代码(分包方式)
目录 1.数据库 2.pom与yml 2.1.pom中的依赖部分 2.2.yml数据库配置部分 3.数据源配置类 DataSourceConfig 3.1.DataSourceConfig1.java ...
- SpringBoot配置多数据源
原文:https://www.jianshu.com/p/033e0ebeb617 项目中用到了两个数据库,分别是Oracle和Mysql,涉及到了多数据源问题,这里做下记录 官方讲解:https:/ ...
随机推荐
- MVC之查询demo
上篇已经说过怎样建立MVC项目.这次主要讲述样例的实现. 其基本的功能就是从数据库中查询一些基本信息. 前边我们已经将实体引入到了项目中,这时Model目录中已经出现了我们建立的newsSystem. ...
- 我所不知道的 javascript 函数
对字符串进行 Base64 加密: window.btoa(str) ---转码 window.atob(str) ---解码 这种加密方法不能加密中文,可以先进行 encodeURIComponen ...
- 双logo
from aip import AipSpeech bd_k_l = ['11059852', '5Kk01GtG2fjCwpzEkwdn0mjw', 'bp6Wyx377Elq7RsCQZzTBgG ...
- oc82--成员变量使用copy修饰
// // Person.h #import <Foundation/Foundation.h> typedef void (^myBlock)(); @interface Person ...
- BusyBox telnetd配置
配置telnetd遇到的一些坑,记录一下 BusyBox版本1.22.1 Networking Utilities -->[*] telnetd 错误1: Escape character is ...
- Bing Maps进阶系列二:使用GeocodeService进行地理位置检索
Bing Maps进阶系列二:使用GeocodeService进行地理位置检索 在<Bing Maps进阶系列一:初识Bing Maps地图服务>里已经对GeocodeService的功能 ...
- TensorFlow alexnet在华为Mate10上运行方法
我使用的caffe模型:https://github.com/BVLC/caffe/tree/ea455eb29393ebe6de9f14e88bfce9eae74edf6d/models/bvlc_ ...
- javaBean注意事项
1.重写tostring方法 2.属性第一位小写
- Codeforces--597A--Divisibility(数学)
DivisibilityCrawling in process... Crawling failed Time Limit:1000MS Memory Limit:262144KB ...
- bag of words in c++
#include <iostream> #include <vector> #include <cstddef> #include <string> # ...