In previous tutorials in this series , We have shown you how to use the three most commonly used data access methods :
- JdbcTemplate
- Spring Data JPA
- MyBatis
Next, we will be divided into three parts to introduce the three ways of data access , When we need multiple data sources , Configuration instructions for how to use .
Add configuration for multiple data sources
First in Spring Boot Configuration file for application.properties
Set up two database configurations that you want to link , Such as this :
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1
spring.datasource.primary.username=root
spring.datasource.primary.password=123456
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=123456
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
Explanation and attention :
- Multi source data configuration , The difference with a single data source is that
spring.datasource
After that, set a data source nameprimary
andsecondary
To distinguish between different data source configurations , This prefix will be used in subsequent initialization of the data source . - Data source connection configuration 2.x and 1.x There is a difference between the configuration items of :2.x Use
spring.datasource.secondary.jdbc-url
, and 1.x Version USESspring.datasource.secondary.url
. If this error occurs during configurationjava.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
, So it’s the configuration item .
Related reading :Spring Boot 1.x Basic course : Multi data source configuration
Initialize the data source with JdbcTemplate
After completing the configuration information of multiple data sources , Let’s create a configuration class to load the configuration information , Initialize the data source , And initialize each data source with JdbcTemplate. You just need to Spring Boot Add the following configuration class under the application to complete !
@Configuration
public class DataSourceConfiguration {
@Primary
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
return new JdbcTemplate(primaryDataSource);
}
@Bean
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
return new JdbcTemplate(secondaryDataSource);
}
}
Explanation and attention :
- The first two Bean It’s the creation of data sources , adopt
@ConfigurationProperties
You can know that the two data sources have loadedspring.datasource.primary.*
andspring.datasource.secondary.*
Configuration of . @Primary
The annotation specifies the master data source , When we don’t specifically specify which data source , You’ll use this Bean- The latter two Bean It’s for each data source
JdbcTemplate
. You can see these twoJdbcTemplate
At the time of creation , They injectedprimaryDataSource
Data sources andsecondaryDataSource
data source
Test it
After finishing the above , We can write a test class to test whether the above multi data source configuration is correct , Like the following :
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter37ApplicationTests {
@Autowired
protected JdbcTemplate primaryJdbcTemplate;
@Autowired
protected JdbcTemplate secondaryJdbcTemplate;
@Before
public void setUp() {
primaryJdbcTemplate.update("DELETE FROM USER ");
secondaryJdbcTemplate.update("DELETE FROM USER ");
}
@Test
public void test() throws Exception {
// Insert... Into the first data source 2 Data
primaryJdbcTemplate.update("insert into user(name,age) values(?, ?)", "aaa", 20);
primaryJdbcTemplate.update("insert into user(name,age) values(?, ?)", "bbb", 30);
// Insert... Into the second data source 1 Data , If you insert the first data source , The primary key conflict will be reported
secondaryJdbcTemplate.update("insert into user(name,age) values(?, ?)", "ccc", 20);
// Check to see if there is any in the first data source 2 Data , Verify that the insertion was successful
Assert.assertEquals("2", primaryJdbcTemplate.queryForObject("select count(1) from user", String.class));
// Check to see if there is any in the first data source 1 Data , Verify that the insertion was successful
Assert.assertEquals("1", secondaryJdbcTemplate.queryForObject("select count(1) from user", String.class));
}
}
Explanation and attention :
- Maybe here you ask , There are two JdbcTemplate, Why not
@Qualifier
Appoint ? Here’s a little bit of knowledge , When we don’t specify , The name of the parameter is used to find Bean, If there is, inject . - these two items. JdbcTemplate At the time of creation , We didn’t name it either , How they match ? Here is also a little bit of knowledge , When we create Bean When , By default, the method name is used as Bean The name of , So here’s the corresponding . Readers might as well look back to see if the two names are identical ?
Code example
For an example of this article, see the following in the warehouse chapter3-7
Catalog :
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
If you think this article is good , welcome Star Support , Your concern is the driving force of my persistence !
This article was first published in :Spring Boot 2.x Basic course :JdbcTemplate Multi data source configuration for , Reprint please indicate the source . This series of topics can be Click to learn for free !