Jdbctemplate多數(shù)據(jù)源配置方法詳解
1.數(shù)據(jù)源配置
spring: # jdbctemplate 連接多數(shù)據(jù)源配置 db1: datasource: jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main1?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource db2: datasource: jdbcurl: jdbc:mysql://127.0.0.1:3306/cloud-main2?useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource
2.啟動(dòng)類
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
3.config 配置datasource
package com.example.demo.jdbctemplate.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 javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Primary //(主數(shù)據(jù)源配置)
@Bean(name = "db1")
@Qualifier("db1")
@ConfigurationProperties(prefix = "spring.db1.datasource")
public DataSource mysqlDataSource(){
return DataSourceBuilder.create().build();
}
//
@Bean(name = "db2")
@Qualifier("db2")
@ConfigurationProperties(prefix = "spring.db2.datasource")
public DataSource sqlServerDataSource(){
return DataSourceBuilder.create().build();
}
}
構(gòu)造 db1JdbcTemplate、 db2JdbcTemplate
package com.example.demo.jdbctemplate.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
@Repository
public class DBLoader {
@Bean(name = "db1JdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("db1") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "db2JdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("db2") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
4.調(diào)用
@Service
public class DBTools {
@Autowired
@Qualifier( "db1JdbcTemplate")
private JdbcTemplate jdbcTemplate1;
@Autowired
@Qualifier("db2JdbcTemplate")
private JdbcTemplate jdbcTemplate2 ;
JdbcTemplate jdbcTemplate;
public JdbcTemplate getDB(String db ) {
if("db1".equals(db)){
return jdbcTemplate1;
}else if ("db2".equals(db)){
return jdbcTemplate2;
}else {
return null ;
}
}
/***
* 查詢
* @param sql
* @return 返回list
*/
public List<Map<String, Object>> queryForList(String db,String sql ) {
List<Map<String, Object>> queryForList = getDB(db).queryForList(sql );
return queryForList;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
- SpringBoot使用JdbcTemplate操作數(shù)據(jù)庫(kù)
- Spring boot 使用JdbcTemplate訪問(wèn)數(shù)據(jù)庫(kù)
- Spring Boot 與 Kotlin 使用JdbcTemplate連接MySQL數(shù)據(jù)庫(kù)的方法
- springboot使用JdbcTemplate完成對(duì)數(shù)據(jù)庫(kù)的增刪改查功能
- Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫(kù)實(shí)例
相關(guān)文章
java使用this調(diào)用構(gòu)造函數(shù)的實(shí)現(xiàn)方法示例
這篇文章主要介紹了java使用this調(diào)用構(gòu)造函數(shù)的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了java面向?qū)ο蟪绦蛟O(shè)計(jì)中函數(shù)調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
Mybatis如何動(dòng)態(tài)創(chuàng)建表
這篇文章主要介紹了Mybatis如何動(dòng)態(tài)創(chuàng)建表問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
深入分析:用1K內(nèi)存實(shí)現(xiàn)高效I/O的RandomAccessFile類的詳解
本篇文章是對(duì)用1K內(nèi)存實(shí)現(xiàn)高效I/O的RandomAccessFile類的詳細(xì)分析介紹,需要的朋友參考下2013-05-05
詳解SpringBoot之訪問(wèn)靜態(tài)資源(webapp...)
這篇文章主要介紹了詳解SpringBoot之訪問(wèn)靜態(tài)資源(webapp...),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
SpringBoot整合MinIO實(shí)現(xiàn)文件存儲(chǔ)系統(tǒng)的代碼示例
在現(xiàn)代的應(yīng)用程序中,文件存儲(chǔ)和管理是一個(gè)常見的需求,MinIO是一個(gè)開源的對(duì)象存儲(chǔ)系統(tǒng),與Spring?Boot框架結(jié)合使用,可以快速構(gòu)建高性能的文件存儲(chǔ)系統(tǒng),本文將介紹如何使用Spring?Boot和MinIO來(lái)實(shí)現(xiàn)文件存儲(chǔ)系統(tǒng)2023-06-06
IDEA卡在”正在解析Maven依賴項(xiàng)“的解決方法
在創(chuàng)建新的SpringBoot項(xiàng)目時(shí),始終卡在"正在解析Maven依賴項(xiàng)…",本文小編給大家介紹了幾種相關(guān)的解決方案,具有一定的參考價(jià)值,需要的朋友可以參考下2023-11-11
java子類調(diào)用父類的方法中包含子類重寫的實(shí)例方法
在本篇文章里小編給大家整理了關(guān)于java子類調(diào)用父類的方法中包含子類重寫的實(shí)例方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。2019-09-09

