JdbcTemplate 配置鏈接多個數(shù)據(jù)源的方法
JdbcTemplate 是 Spring 框架提供的 JDBC 模板類,旨在簡化原生 JDBC 繁瑣的數(shù)據(jù)庫操作,支持 增刪改查及事務(wù)管理,廣泛應(yīng)用于 Spring 及 Spring Boot 項目 。
核心功能與優(yōu)勢
- 簡化數(shù)據(jù)庫操作:JdbcTemplate 封裝了 JDBC 連接獲取、SQL 執(zhí)行及結(jié)果集處理等底層細(xì)節(jié),顯著減少了樣板代碼 。
- 連接管理:自動處理數(shù)據(jù)庫連接的獲取與釋放,防止資源泄漏 。
- 異常處理:將 JDBC 的受檢異常轉(zhuǎn)換為 Spring 的統(tǒng)一數(shù)據(jù)訪問異常體系,簡化錯誤處理邏輯 。
- 靈活的 SQL 控制:與 JPA 等全自動 ORM 框架不同,JdbcTemplate 允許開發(fā)者 手寫原生 SQL,在執(zhí)行復(fù)雜查詢或存儲過程時更具靈活性 。
- 適用場景:適合小型項目、快速原型開發(fā)或需要精細(xì)控制 SQL 性能的場景 。
- 對比分析:相比 MyBatis,其配置更簡單但自動化映射能力較弱;相比原生 JDBC,開發(fā)效率大幅提升 。
- Spring 生態(tài)集成:作為 Spring 家族成員,它能無縫集成 Spring 事務(wù)管理、依賴注入等核心功能,便于構(gòu)建分層架構(gòu) 。
引入所mysql-connector-java和依賴spring-boot-starter-jdbc依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>zhang</groupId>
<artifactId>DataConvertion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DataConvertion</name>
<description>DataConvertion</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>配置數(shù)據(jù)庫連接信息(注意單數(shù)據(jù)源url,多數(shù)據(jù)源需要改成jdbc-url)
spring.application.name=DataConvertion spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/xqr?useUnicode=true&characterEncoding=utf8 spring.datasource.db1.username=root spring.datasource.db1.password=root spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/mcms?useUnicode=true&characterEncoding=utf8 spring.datasource.db2.username=root spring.datasource.db2.password=root
3.配置類配置數(shù)據(jù)源
package zhang.dataconvertion.config;
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//表名此類是是配置工具類,讓springboot框架將此類當(dāng)成配置文件加載
public class DatasourceConfig {
//創(chuàng)建第一個數(shù)據(jù)源(數(shù)據(jù)庫)
@Bean//bean注解用來創(chuàng)建數(shù)據(jù)源,Bean注解幾乎是方法專用。
@Primary//表示該數(shù)據(jù)源為首選數(shù)據(jù)源.主要用于分清主次數(shù)據(jù)源
@ConfigurationProperties(prefix ="spring.datasource.db1" )//ConfigurationProperties用來連接創(chuàng)建誰的數(shù)據(jù)源對象.讀取配置文件的信息。
public DataSource dbOne(){
return DataSourceBuilder.create().build();
}
//創(chuàng)建第二個數(shù)據(jù)源(數(shù)據(jù)庫)
@Bean //方法名就是對象名
@ConfigurationProperties(prefix ="spring.datasource.db2")
public DataSource dbTwo(){
return DataSourceBuilder.create().build();
}
}配置類配置JdbcTemplate對象
package zhang.dataconvertion.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
//數(shù)據(jù)源具體怎么使用就依賴于這個類
//該類作用:給jdbcTemplate注入不同的數(shù)據(jù)源
@Configuration
public class JdbcTemplateConfig {
@Bean
public JdbcTemplate oneTemplate(@Qualifier("dbOne") DataSource dataSource){//@Qualifier("dbOne")指定第一個數(shù)據(jù)源。dbOne為數(shù)據(jù)源對應(yīng)的方法名。不指定數(shù)據(jù)源,由于有兩個,因此會報錯
return new JdbcTemplate(dataSource);
}
@Bean
public JdbcTemplate twoTemplate(@Qualifier("dbTwo") DataSource dataSource){//@Qualifier("dbTwo")指定第二個數(shù)據(jù)源。dbTwo為數(shù)據(jù)源對應(yīng)的方法名
return new JdbcTemplate(dataSource);
}
}5.注入JdbcTemplate使用 @Autowired 和 @Qualifier(“twoTemplate”)或者 @Resource(name = “oneTemplate”)
package zhang.dataconvertion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@SpringBootApplication
public class DataConvertionApplication implements CommandLineRunner {
@Autowired
@Qualifier("oneTemplate")
private JdbcTemplate oneTemplate;
@Autowired
@Qualifier("twoTemplate")
private JdbcTemplate twoTemplate;
public static void main(String[] args){
SpringApplication.run(DataConvertionApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
List<Map<String, Object>> maps1 = oneTemplate.queryForList("select * from story");
for (Map<String, Object> map : maps1) {
System.out.println(map);
}
List<Map<String, Object>> maps2 = twoTemplate.queryForList("select * from mdiy_dict");
for (Map<String, Object> map : maps2) {
System.out.println(map);
}
}
}到此這篇關(guān)于JdbcTemplate 配置鏈接多個數(shù)據(jù)源的方法的文章就介紹到這了,更多相關(guān)JdbcTemplate 配置數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot jdbctemplate如何實現(xiàn)多數(shù)據(jù)源
- Spring?Boot整合持久層之JdbcTemplate多數(shù)據(jù)源
- SpringBoot2使用JTA組件實現(xiàn)基于JdbcTemplate多數(shù)據(jù)源事務(wù)管理(親測好用)
- 詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- Jdbctemplate多數(shù)據(jù)源配置方法詳解
- 詳解springboot采用多數(shù)據(jù)源對JdbcTemplate配置的方法
相關(guān)文章
詳解Spring Cloud Hystrix斷路器實現(xiàn)容錯和降級
本篇文章主要介紹了詳解Spring Cloud Hystrix斷路器實現(xiàn)容錯和降級,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
解決springboot項目找不到resources目錄下的資源問題
這篇文章主要介紹了解決springboot項目找不到resources目錄下的資源問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
SpringBoot開發(fā)技巧啟動時配置校驗實現(xiàn)示例
這篇文章主要為大家介紹了SpringBoot開發(fā)在啟動時自動配置校驗的實現(xiàn)示例及原理解析,有需要的朋友可以借鑒參考下希望能夠有所幫助2021-10-10
將java中的 string 類型轉(zhuǎn)成 數(shù)組案例
這篇文章主要介紹了將java中的 string 類型轉(zhuǎn)成 數(shù)組案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

