springboot jdbctemplate如何實(shí)現(xiàn)多數(shù)據(jù)源
1.簡介
所謂多數(shù)據(jù)源,其實(shí)就是在一個(gè)項(xiàng)目中使用多個(gè)數(shù)據(jù)庫實(shí)例中的數(shù)據(jù)庫或者同一個(gè)數(shù)據(jù)庫實(shí)例中多個(gè)不同的庫。
在大部分情況下會(huì)使用更加強(qiáng)大的持久化框架來訪問數(shù)據(jù)庫,比如MyBatis、Hibernate或者Spring Data JPA等ORM框架。
使用JDBC是開發(fā)者必備的基礎(chǔ)技能,只有熟悉了基礎(chǔ)的JDBC,才能更加深入地學(xué)習(xí)其他的ORM框架。
2.舉例
2.1配置多數(shù)據(jù)源連接信息
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/jdbctest spring.datasource.primary.username=root spring.datasource.primary.password=Yjb123456 spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/jdbctest2 spring.datasource.secondary.username=root spring.datasource.secondary.password=Yjb123456 spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
2.2配置JDBC初始化
創(chuàng)建DataSourceConfig類,在項(xiàng)目啟動(dòng)時(shí)讀取配置文件中的數(shù)據(jù)庫信息,并對(duì)JDBC初始化,具體代碼如下:
在上面的示例中,DataSourceConfig類的作用是在項(xiàng)目啟動(dòng)時(shí)根據(jù)特定的前綴加載不同的數(shù)據(jù)源,再根據(jù)構(gòu)建好的數(shù)據(jù)源創(chuàng)建不同的JdbcTemplate。
由于Spring容器中存在兩個(gè)數(shù)據(jù)源,使用默認(rèn)的類型查找時(shí)會(huì)報(bào)錯(cuò),因此加上@Qualifier注解,表示按照名稱查找。
這里創(chuàng)建了兩個(gè)JdbcTemplate實(shí)例,分別對(duì)應(yīng)了兩個(gè)數(shù)據(jù)源。
需要注意的是,使用多個(gè)數(shù)據(jù)源時(shí)需要添加@Primary注解,表示自動(dòng)裝配出現(xiàn)多個(gè)Bean候選者時(shí),被注解為@Primary的Bean將作為首選者。
Primary表示“主要的”,類似于SQL語句中的“Primary Key”(主鍵),只能有唯一一個(gè),否則會(huì)報(bào)錯(cuò)。
package com.yangjunbo.helloword.properties;
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 {
@Primary
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name="primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate (
@Qualifier("primaryDataSource") DataSource dataSource ) {
return new JdbcTemplate(dataSource);
}
@Bean(name="secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
2.3測試調(diào)用多數(shù)據(jù)源
package com.yangjunbo.helloword;
import com.yangjunbo.helloword.pojo.Student;
import com.yangjunbo.helloword.rowMapper.StudentRowMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class RowMapper {
@Autowired
private JdbcTemplate primaryJdbcTemplate;
@Autowired
private JdbcTemplate secondaryJdbcTemplate;
@Test
public void dataSourceTest(){
Student student = new Student("weiz多數(shù)據(jù)源",0,30);
primaryJdbcTemplate.update("INSERT INTO Student(name, sex, age) values(?, ?, ?)",
student.getName(), student.getSex(), student.getAge());
secondaryJdbcTemplate.update("INSERT INTO Student(name, sex, age) values(?, ?, ?)",
student.getName(), student.getSex(), student.getAge());
}
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java SSM實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng)
本項(xiàng)目基于Java SSM框架實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng),主要實(shí)現(xiàn)系統(tǒng)的在線點(diǎn)餐功能。文中的示例代碼講解詳細(xì),感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-02-02
Java程序設(shè)計(jì)之12個(gè)經(jīng)典樣例
這篇文章主要給大家分享關(guān)于Java程序設(shè)計(jì)11個(gè)經(jīng)典樣例,主要以舉例的形式詳細(xì)的講解了Java程序設(shè)計(jì)的各種方法,需要的朋友可以參考一下文章具體的內(nèi)容2021-10-10
springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程
這篇文章主要介紹了springboot集成springsecurity 使用OAUTH2做權(quán)限管理的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之網(wǎng)上商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Java+jsp+servlet+mysql+ajax實(shí)現(xiàn)一個(gè)網(wǎng)上商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
java使用WatchService監(jiān)控文件夾示例
本篇文章主要介紹了java使用WatchService監(jiān)控文件夾示例的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02
nacos注冊中心調(diào)用服務(wù)失敗的解決方案
nacos注冊中心出現(xiàn)調(diào)用服務(wù)失敗的情況,可能是因?yàn)樽粤藘蓚€(gè)IP地址,一個(gè)是開發(fā)/測試環(huán)境,一個(gè)是本地啟動(dòng)的IP,解決方法是將本地的IP從注冊中心下線,以避免干擾其他環(huán)境,確定哪個(gè)IP是開發(fā)環(huán)境/測試環(huán)境的IP,可以使用ifconfig命令查看服務(wù)器的IP地址2026-02-02
SpringBoot對(duì)數(shù)據(jù)庫用戶名和密碼進(jìn)行加密的兩種方案
在當(dāng)今互聯(lián)網(wǎng)時(shí)代,?數(shù)據(jù)庫安全的重要性不言而喻,作為Java開發(fā)的主流框架,Spring Boot項(xiàng)目通常需要連接數(shù)據(jù)庫,而數(shù)據(jù)庫的?用戶名和密碼作為最敏感的信息之一,所以本文給大家詳細(xì)介紹兩種主流的數(shù)據(jù)源配置加密方案,需要的朋友可以參考下2025-12-12
JAVA對(duì)字符串進(jìn)行32位MD5加密的實(shí)踐
本文主要介紹了JAVA對(duì)字符串進(jìn)行32位MD5加密的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

