博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot配置多数据源(JdbcTemplate方式)
阅读量:6909 次
发布时间:2019-06-27

本文共 3822 字,大约阅读时间需要 12 分钟。

在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个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;@Configurationpublic 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=truespring.datasource.hikari.mysql.username = rootspring.datasource.hikari.mysql.password = 123456spring.datasource.hikari.mysql.driver-class-Name = com.mysql.jdbc.Driverspring.datasource.formal.mysql.jdbc-url =jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=truespring.datasource.formal.mysql.username = rootspring.datasource.formal.mysql.password = 1314spring.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

你可能感兴趣的文章
Contoso 大学 - 2 – 实现基本的增删改查
查看>>
Asp.Net Web API 2第五课——Web API路由
查看>>
Oracle GoldenGate 12c中的协同交付(Coordinated Delivery)
查看>>
使用GDB 修改MySQL参数不重启
查看>>
更改chrome底色为护目色
查看>>
EF扩展库(批量操作)
查看>>
论坛表格设计
查看>>
九度 1533:最长上升子序列
查看>>
demo1 spark streaming 接收 kafka 数据java代码WordCount示例
查看>>
九度 1482:玛雅人的密码(BFS)
查看>>
Windows 8 应用开发 - 挂起与恢复
查看>>
在InstallShield中发布单一的Setup.exe文件
查看>>
LINQ to Objects系列(1)相关技术准备
查看>>
[leetcode]Validate Binary Search Tree @ Python
查看>>
设计模式---简单工厂模式(学习笔记)
查看>>
第21周五
查看>>
c++ 使用json的库。cJSON
查看>>
struts2不兼容servlet、COS
查看>>
Java 打印堆栈的几种方法
查看>>
解读Gartner《2015年度新兴技术成熟度曲线报告》
查看>>