springboot jdbctemplate如何實現(xiàn)多數(shù)據(jù)源
1.簡介
所謂多數(shù)據(jù)源,其實就是在一個項目中使用多個數(shù)據(jù)庫實例中的數(shù)據(jù)庫或者同一個數(shù)據(jù)庫實例中多個不同的庫。
在大部分情況下會使用更加強大的持久化框架來訪問數(shù)據(jù)庫,比如MyBatis、Hibernate或者Spring Data JPA等ORM框架。
使用JDBC是開發(fā)者必備的基礎技能,只有熟悉了基礎的JDBC,才能更加深入地學習其他的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類,在項目啟動時讀取配置文件中的數(shù)據(jù)庫信息,并對JDBC初始化,具體代碼如下:
在上面的示例中,DataSourceConfig類的作用是在項目啟動時根據(jù)特定的前綴加載不同的數(shù)據(jù)源,再根據(jù)構建好的數(shù)據(jù)源創(chuàng)建不同的JdbcTemplate。
由于Spring容器中存在兩個數(shù)據(jù)源,使用默認的類型查找時會報錯,因此加上@Qualifier注解,表示按照名稱查找。
這里創(chuàng)建了兩個JdbcTemplate實例,分別對應了兩個數(shù)據(jù)源。
需要注意的是,使用多個數(shù)據(jù)源時需要添加@Primary注解,表示自動裝配出現(xiàn)多個Bean候選者時,被注解為@Primary的Bean將作為首選者。
Primary表示“主要的”,類似于SQL語句中的“Primary Key”(主鍵),只能有唯一一個,否則會報錯。
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測試調用多數(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());
}
}
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題
這篇文章主要介紹了SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
快速解決?IDEA?報錯:?“java?找不到符號“(“cannot?find?symbol“)
文章詳細講解了在IntelliJIDEA中解決“找不到符號”錯誤的方法,包括檢查導入語句、拼寫錯誤、類路徑設置、文件編譯狀態(tài)、JDK配置以及IDE配置問題,通過具體示例代碼,展示了如何從錯誤代碼到解決步驟,感興趣的朋友一起看看吧2025-03-03

