SpringBoot 配置多個JdbcTemplate
前言
開發中使用多數據源配置是一個非常常見的需求。Spring和SpringBoot中,對此都有相應的解決方案。
多數據源的首選分佈式數據庫中間件MyCat或者Sharing-Jdbc去解決相關問題。使用MyCat,然後分表策略使用sharding-by-intfile。
本文我們只討論如何在SpringBoot中簡單配置多個JdbcTemplate。
一、創建一個SpringBoot 項目,並引入如下依賴
<!--web應用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--單元測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
二、在application.properties中添加數據源配置
spring.datasource.one.url=jdbc:mysql://localhost:3306/oy1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.one.jdbcUrl=jdbc:mysql://localhost:3306/oy1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.one.username=***
spring.datasource.one.password=***
spring.datasource.one.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.two.url=jdbc:mysql://localhost:3306/oy2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.two.jdbcUrl=jdbc:mysql://localhost:3306/oy2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.two.username=***
spring.datasource.two.password=***
spring.datasource.two.driver-class-name=com.mysql.cj.jdbc.Driver
三、新增DadaSourceConfig.java配置多個數據源以及JdbcTemplate,代碼如下:
package com.bestoyc.jdbctemplatedemo;
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.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* @author oyc
* @Title: DataSourceConfig
* @ProjectName jdbctemplatedemo
* @Description: TODO
* @date 2019/9/22 0:47
*/
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.one")
@Qualifier("oneDataSource")
DataSource dsOne() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.two")
@Qualifier("twoDataSource")
DataSource dsTwo() {
return DataSourceBuilder.create().build();
}
@Bean(name = "oneJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("oneDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "twoJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("twoDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
四、多個JdbcTemplate使用
@Autowired
@Qualifier("oneJdbcTemplate")
private JdbcTemplate oneJdbcTemplate;
@Autowired
@Qualifier("twoJdbcTemplate")
private JdbcTemplate twoJdbcTemplate;
@RequestMapping("/createUser1")
public String createUser1() {
oneJdbcTemplate.update("INSERT INTO `user`(`id`, `name`, `age`) VALUES (?,?,?);", null,"ouyang", 12);
return "success";
}
@RequestMapping("/createUser2")
public String createUser2() {
twoJdbcTemplate.update("INSERT INTO `user`(`id`, `name`, `age`) VALUES (?,?,?);", null,"ouyang", 12);
return "success";
}
這裏只是簡單使用,讀者可以根據自己的業務需要添加相應的AOP用戶數據源的切換。