spring-boot-route(十)多數據源切換
- 2020 年 10 月 9 日
- 筆記
- spring-boot-route
前面我們已經介紹了三種方式來操作數據庫,在實際開發中,往往會出現一個服務連接多個數據庫的需求,這時候就需要在項目中進行靈活切換數據源來完成多個數據庫操作。這一章中,我們使用jdbcTemplate來學習多數據源的配置。
一 準備工作
1.1 建庫、建表
我們新建兩個庫db1
和db2
,數據結構還是用前面演示的,分別在兩個庫中新建表student
。
CREATE TABLE `student` (
`student_id` int(30) NOT NULL AUTO_INCREMENT,
`age` int(1) DEFAULT NULL COMMENT '年齡',
`name` varchar(45) DEFAULT NULL COMMENT '姓名',
`sex` int(1) DEFAULT NULL COMMENT '性別:1:男,2:女,0:未知',
`create_time` datetime DEFAULT NULL COMMENT '創建時間',
`status` int(1) DEFAULT NULL COMMENT '狀態:1:正常,-1:刪除',
PRIMARY KEY (`student_id`)
) ENGINE=InnoDB CHARSET=utf8mb4 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='學生表'
1.2 引入mysql和jdbcTemplate依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
1.3 寫入兩個數據源配置
spring:
datasource:
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/db1
username: root
password: root
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/db2
username: root
password: root
二 多數據源配置
@Configuration
public class DataSourceConfig {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1DataSource(){
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2DataSource(){
return DataSourceBuilder.create().build();
}
}
- @Primary:表示主的,即出現多個bean的時候如果不指定具體的bean,則會採用這個
- @bean:標註為一個bean,如果不指定name屬性,則會使用創建bean方法的名字做為bean的名字
- @ConfigurationProperties:讀取配置文件
三 配置JdbcTemplate對象
@Configuration
public class DataSourceConfig {
@Bean
public JdbcTemplate db1JdbcTemplate(@Qualifier("db1DataSource") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Primary
@Bean
public JdbcTemplate db2JdbcTemplate(@Qualifier("db2DataSource") DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
- @Qualifier:bean類型相同後,指定使用的bean的name
四 測試類
4.1 測試@Primary屬性
不指定使用哪個JdbcTemplate對象時,會使用標註了@Primary屬性的對象
@SpringBootTest
class SpringBootDatasourceApplicationTests {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void testPrimary() {
jdbcTemplate.update("insert into student(name,age) values(?,?)",new Object[]{"Java旅途",18});
}
}
4.2 測試多數據源
@SpringBootTest
class SpringBootDatasourceApplicationTests {
@Autowired
private JdbcTemplate db1JdbcTemplate;
@Autowired
private JdbcTemplate db2JdbcTemplate;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
db1JdbcTemplate.update("insert into student(name,age) values(?,?)",new Object[]{"Java旅途",18});
db2JdbcTemplate.update("insert into student(name,age) values(?,?)",new Object[]{"Java旅途",18});
}
}
}
這裡分享一道面試題:@Autowired 與@Resource有什麼區別?
@Autowired是Spring提供的,@Resource是JDK提供的;
@Autowired是根據bean的類型匹配的,@Resource是根據bean的name匹配的;
如果@Autowird想要根據name匹配應該怎麼做呢?
- 配合@Qualifier註解指定bean的name
- 使用變量名稱作為bean的id,@Autowired如果匹配到多個符合條件的對象後,會自動根據變量名稱做為bean的id繼續匹配。我們在4.2中採用的就是這種方式。
此是spring-boot-route系列的第十篇文章,這個系列的文章都比較簡單,主要目的就是為了幫助初次接觸Spring Boot 的同學有一個系統的認識。本文已收錄至我的github,歡迎各位小夥伴star
!
github://github.com/binzh303/spring-boot-route
點關注、不迷路
如果覺得文章不錯,歡迎關注、點贊、收藏,你們的支持是我創作的動力,感謝大家。
如果文章寫的有問題,請不要吝嗇,歡迎留言指出,我會及時核查修改。
如果你還想更加深入的了解我,可以微信搜索「Java旅途」進行關注。回復「1024」即可獲得學習視頻及精美電子書。每天7:30準時推送技術文章,讓你的上班路不在孤獨,而且每月還有送書活動,助你提升硬實力!