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è)不同的庫。
在大部分情況下會使用更加強(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)目啟動時(shí)讀取配置文件中的數(shù)據(jù)庫信息,并對JDBC初始化,具體代碼如下:
在上面的示例中,DataSourceConfig類的作用是在項(xiàng)目啟動時(shí)根據(jù)特定的前綴加載不同的數(shù)據(jù)源,再根據(jù)構(gòu)建好的數(shù)據(jù)源創(chuàng)建不同的JdbcTemplate。
由于Spring容器中存在兩個(gè)數(shù)據(jù)源,使用默認(rèn)的類型查找時(shí)會報(bào)錯(cuò),因此加上@Qualifier注解,表示按照名稱查找。
這里創(chuàng)建了兩個(gè)JdbcTemplate實(shí)例,分別對應(yīng)了兩個(gè)數(shù)據(jù)源。
需要注意的是,使用多個(gè)數(shù)據(jù)源時(shí)需要添加@Primary注解,表示自動裝配出現(xiàn)多個(gè)Bean候選者時(shí),被注解為@Primary的Bean將作為首選者。
Primary表示“主要的”,類似于SQL語句中的“Primary Key”(主鍵),只能有唯一一個(gè),否則會報(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)文章
springboot?項(xiàng)目啟動后無日志輸出直接結(jié)束的解決
這篇文章主要介紹了springboot?項(xiàng)目啟動后無日志輸出直接結(jié)束的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題
這篇文章主要介紹了SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03快速解決?IDEA?報(bào)錯(cuò):?“java?找不到符號“(“cannot?find?symbol“)
文章詳細(xì)講解了在IntelliJIDEA中解決“找不到符號”錯(cuò)誤的方法,包括檢查導(dǎo)入語句、拼寫錯(cuò)誤、類路徑設(shè)置、文件編譯狀態(tài)、JDK配置以及IDE配置問題,通過具體示例代碼,展示了如何從錯(cuò)誤代碼到解決步驟,感興趣的朋友一起看看吧2025-03-03