之前在介紹使用JdbcTemplate和Spring-data-jpa時,都使用了單數據源。在單數據源的情況下,Spring Boot的配置非常簡單,只需要在application.properties文件中配置連接參數即可。但是往往隨着業務量發展,我們通常會進行數據庫拆分或是引入其他數據庫,從而我們需要配置多個數據源,下面基於之前的JdbcTemplate和Spring-data-jpa例子分別介紹兩種多數據源的配置方式。
多數據源配置
創建一個Spring配置類,定義兩個DataSource用來讀取application.properties中的不同配置。如下例子中,主數據源配置為spring.datasource.primary開頭的配置,第二數據源配置為spring.datasource.secondary開頭的配置。
package com.wls.diypro.util.datasource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
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 {
@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@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);
}
}
JdbcTemplate支持對應的application-dev.yml配置如下:
spring:
datasource:
primary:
driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://192.168.159.128:3306/mydb
url: jdbc:mysql://192.168.11.131:3306/mydb
username: wls
password: Wls141215!
secondary:
driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://192.168.159.128:3306/mydb
url: jdbc:mysql://192.168.11.131:3306/shopmall
username: wls
password: Wls141215!
對JdbcTemplate的支持比較簡單,只需要為其注入對應的datasource即可,如下例子,在創建JdbcTemplate的時候分別注入名為primaryDataSource和secondaryDataSource的數據源來區分不同的JdbcTemplate。
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate primaryJdbcTemplate;
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate secondaryJdbcTemplate;
package com.wls.diypro.test.jdbcTemplateTest;
import com.wls.diypro.model.OrderInfo;
import com.wls.diypro.service.IOrderInfoService;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class JdbcTemplateTest {
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate primaryJdbcTemplate;
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate secondaryJdbcTemplate;
@Autowired
private IOrderInfoService iOrderInfoService;
@Test
public void addOrder() throws Exception {
OrderInfo orderInfo = new OrderInfo();
orderInfo.setAddressDetail("廣平大街");
orderInfo.setArea("大興區");
orderInfo.setCity("北京市");
orderInfo.setOrderNumber("10000001");
orderInfo.setOrderStatus("2");
orderInfo.setOrderTime(new Date());
orderInfo.setProvince("北京");
orderInfo.setReceiver("王老師");
orderInfo.setStreet("ces");
iOrderInfoService.addOrder(orderInfo);
}
@Before
public void setUp() {
primaryJdbcTemplate.update("DELETE FROM order_info ");
secondaryJdbcTemplate.update("DELETE FROM order_info ");
}
@Test
public void test() throws Exception {
// 往第一個數據源中插入兩條數據
primaryJdbcTemplate.update("insert into order_info(order_flag,order_number,order_status,street) values(?, ?, ?, ?)", "test", "10001", "S01","廣平大街");
primaryJdbcTemplate.update("insert into order_info(order_flag,order_number,order_status,street) values(?, ?, ?, ?)", "test", "10001", "S01","廣平大街");
// 往第二個數據源中插入一條數據,若插入的是第一個數據源,則會主鍵沖突報錯
secondaryJdbcTemplate.update("insert into order_info(order_flag,order_number,order_status,street) values(?, ?, ?, ?)", "test", "10003", "S02","廣平大街");
// 查一下第一個數據源中是否有兩條數據,驗證插入是否成功
Assert.assertEquals("2", primaryJdbcTemplate.queryForObject("select count(1) from order_info", String.class));
// 查一下第一個數據源中是否有兩條數據,驗證插入是否成功
Assert.assertEquals("1", secondaryJdbcTemplate.queryForObject("select count(1) from order_info", String.class));
}
}
完整示例:guithub接下來通過測試用例來演示如何使用這兩個針對不同數據源的JdbcTemplate。
