Spring Boot 整合JdbcTemplate 多數據源

多數據源配置也算是一個常見的開發需求,Spring 和 SpringBoot 中,對此都有相應的解決方案,不過通常來講,若是有多數據源的需求,我仍是建議首選分佈式數據庫中間件 MyCat 去解決相關問題,以前有小夥伴在個人知識星球上提問,他的數據根據條件的不一樣,可能保存在四十多個不一樣的數據庫中,怎麼辦?這種場景下使用多數據源其實就有些費事了,我給的建議是使用 MyCat,而後分表策略使用 sharding-by-intfile 。java

固然若是一些簡單的需求,仍是可使用多數據源的,Spring Boot 中,JdbcTemplate、MyBatis 以及 Jpa 均可以配置多數據源,本文就先和大夥聊一聊 JdbcTemplate 中多數據源的配置(關於JdbcTemplate的用法,若是還有小夥伴不瞭解,能夠參考個人 Spring Boot2 系列教程(十九)Spring Boot 整合 JdbcTemplate)。mysql

建立工程

首先是建立工程,和前文同樣,建立工程時,也是選擇 Web、Jdbc 以及 MySQL 驅動,以下圖:git

圖片

建立成功以後,必定接下來手動添加 Druid 依賴,因爲這裏一會須要開發者本身配置 DataSoruce,因此這裏必需要使用 druid-spring-boot-starter 依賴,而不是傳統的那個 druid 依賴,由於 druid-spring-boot-starter 依賴提供了 DruidDataSourceBuilder 類,這個能夠用來構建一個 DataSource 實例,而傳統的 Druid 則沒有該類。完整的依賴以下:github

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.28</version>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid-spring-boot-starter</artifactId>
   <version>1.1.10</version>
</dependency>

配置數據源

接下來,在 application.properties 中配置數據源,不一樣於上文,這裏的數據源須要配置兩個,以下:web

spring.datasource.one.url=jdbc:mysql:///test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.one.username=root
spring.datasource.one.password=root
spring.datasource.one.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.two.url=jdbc:mysql:///test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.two.username=root
spring.datasource.two.password=root
spring.datasource.two.type=com.alibaba.druid.pool.DruidDataSource

這裏經過 one 和 two 對數據源進行了區分,可是加了 one 和 two 以後,這裏的配置就無法被 SpringBoot 自動加載了(由於前面的 key 變了),須要咱們本身去加載 DataSource 了,此時,須要本身配置一個 DataSourceConfig,用來提供兩個 DataSource Bean,以下:spring

@Configuration
public class DataSourceConfig {
   @Bean
   @ConfigurationProperties(prefix = "spring.datasource.one")
   DataSource dsOne() {
       return DruidDataSourceBuilder.create().build();
   }
   @Bean
   @ConfigurationProperties(prefix = "spring.datasource.two")
   DataSource dsTwo() {
       return DruidDataSourceBuilder.create().build();
   }
}

這裏提供了兩個 Bean,其中 @ConfigurationProperties 是 Spring Boot 提供的類型安全的屬性綁定,以第一個Bean爲例, @ConfigurationProperties(prefix = "spring.datasource.one") 表示使用 spring.datasource.one 前綴的數據庫配置去建立一個 DataSource,這樣配置以後,咱們就有了兩個不一樣的 DataSource,接下來再用這兩個不一樣的 DataSource 去建立兩個不一樣的 JdbcTemplate。sql

配置 JdbcTemplate 實例

建立 JdbcTemplateConfig 類,用來提供兩個不一樣的 JdbcTemplate 實例,以下:數據庫

@Configuration
public class JdbcTemplateConfig {
   @Bean
   JdbcTemplate jdbcTemplateOne(@Qualifier("dsOne") DataSource dsOne) {
       return new JdbcTemplate(dsOne);
   }
   @Bean
   JdbcTemplate jdbcTemplateTwo(@Qualifier("dsTwo") DataSource dsTwo) {
       return new JdbcTemplate(dsTwo);
   }
}

每個 JdbcTemplate 的建立都須要一個 DataSource,因爲 Spring 容器中如今存在兩個 DataSource,默認使用類型查找,會報錯,所以加上 @Qualifier 註解,表示按照名稱查找。這裏建立了兩個 JdbcTemplate 實例,分別對應了兩個 DataSource。安全

接下來直接去使用這個 JdbcTemplate 就能夠了。app

測試使用

關於 JdbcTemplate 的詳細用法大夥能夠參考個人上篇文章,這裏我主要演示數據源的差別,在 Controller 中注入兩個不一樣的 JdbcTemplate ,這兩個 JdbcTemplate 分別對應了不一樣的數據源,以下:

@RestController
public class HelloController {
   @Autowired
   @Qualifier("jdbcTemplateOne")
   JdbcTemplate jdbcTemplateOne;
   @Resource(name = "jdbcTemplateTwo")
   JdbcTemplate jdbcTemplateTwo;

   @GetMapping("/user")
   public List<User> getAllUser() {
       List<User> list = jdbcTemplateOne.query("select * from t_user", new BeanPropertyRowMapper<>(User.class));
       return list;
   }
   @GetMapping("/user2")
   public List<User> getAllUser2() {
       List<User> list = jdbcTemplateTwo.query("select * from t_user", new BeanPropertyRowMapper<>(User.class));
       return list;
   }
}

和 DataSource 同樣,Spring 容器中的 JdbcTemplate 也是有兩個,所以不能經過 byType 的方式注入進來,這裏給大夥提供了兩種注入思路,一種是使用 @Resource 註解,直接經過 byName 的方式注入進來,另一種就是 @Autowired 註解加上 @Qualifier 註解,二者聯合起來,實際上也是 byName。將 JdbcTemplate 注入進來以後,jdbcTemplateOne 和 jdbcTemplateTwo 此時就表明操做不一樣的數據源,使用不一樣的 JdbcTemplate 操做不一樣的數據源,實現了多數據源配置。

好了,這個問題就先說到這裏,感興趣的小夥伴也能夠參考相關案例:https://github.com/lenve/javaboy-code-samples

轉自:https://mp.weixin.qq.com/s/9Q2xk3L9OErPga6rAN25Fg

相關文章
相關標籤/搜索