Spring Boot之JdbcTemplate多数据源配置与使用
多数据源配置
创建一个Spring配置类,定义两个DataSource用来读取application.properties
中的不同配置。如下例子中,主数据源配置为spring.datasource.primary
开头的配置,第二数据源配置为spring.datasource.secondary
开头的配置。
之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源。在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties
文件中配置连接参数即可。但是往往随着业务量发展,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源,下面基于之前的JdbcTemplate和Spring-data-jpa例子分别介绍两种多数据源的配置方式。
多数据源配置
创建一个Spring配置类,定义两个DataSource用来读取application.properties
中的不同配置。如下例子中,主数据源配置为spring.datasource.primary
开头的配置,第二数据源配置为spring.datasource.secondary
开头的配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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
配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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。
1
2
3
4
5
6
7
|
@Autowired @Qualifier ( "primaryJdbcTemplate" ) protected JdbcTemplate primaryJdbcTemplate; @Autowired @Qualifier ( "secondaryJdbcTemplate" ) protected JdbcTemplate secondaryJdbcTemplate; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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 )); } } |