Skip to content

Commit 2ccecba

Browse files
committed
Spring Boot 2.x基础教程:JdbcTemplate的多数据源配置
1 parent f5f1192 commit 2ccecba

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

2.1.x/chapter3-7/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

2.1.x/chapter3-7/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.3.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<groupId>com.didispace</groupId>
14+
<artifactId>chapter3-7</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<description>使用JDBCTemplate的多数据源配置</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-jdbc</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>mysql</groupId>
35+
<artifactId>mysql-connector-java</artifactId>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace.chapter37;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Chapter37Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Chapter37Application.class, args);
11+
}
12+
13+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.didispace.chapter37;
2+
3+
import org.springframework.beans.factory.annotation.Qualifier;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.boot.jdbc.DataSourceBuilder;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Primary;
9+
import org.springframework.jdbc.core.JdbcTemplate;
10+
11+
import javax.sql.DataSource;
12+
13+
@Configuration
14+
public class DataSourceConfiguration {
15+
16+
@Primary
17+
@Bean
18+
@ConfigurationProperties(prefix = "spring.datasource.primary")
19+
public DataSource primaryDataSource() {
20+
return DataSourceBuilder.create().build();
21+
}
22+
23+
@Bean
24+
@ConfigurationProperties(prefix = "spring.datasource.secondary")
25+
public DataSource secondaryDataSource() {
26+
return DataSourceBuilder.create().build();
27+
}
28+
29+
@Bean
30+
public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
31+
return new JdbcTemplate(primaryDataSource);
32+
}
33+
34+
@Bean
35+
public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
36+
return new JdbcTemplate(secondaryDataSource);
37+
}
38+
39+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# pring boot 1.x的配置:spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
2+
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1
3+
spring.datasource.primary.username=root
4+
spring.datasource.primary.password=123456
5+
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
6+
7+
# spring boot 1.x的配置:spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
8+
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2
9+
spring.datasource.secondary.username=root
10+
spring.datasource.secondary.password=123456
11+
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace.chapter37;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.jdbc.core.JdbcTemplate;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
14+
@RunWith(SpringRunner.class)
15+
@SpringBootTest
16+
public class Chapter37ApplicationTests {
17+
18+
@Autowired
19+
@Qualifier("primaryJdbcTemplate")
20+
protected JdbcTemplate jdbcTemplate1;
21+
22+
@Autowired
23+
@Qualifier("secondaryJdbcTemplate")
24+
protected JdbcTemplate jdbcTemplate2;
25+
26+
@Before
27+
public void setUp() {
28+
jdbcTemplate1.update("DELETE FROM USER ");
29+
jdbcTemplate2.update("DELETE FROM USER ");
30+
}
31+
32+
@Test
33+
public void test() throws Exception {
34+
// 往第一个数据源中插入 2 条数据
35+
jdbcTemplate1.update("insert into user(name,age) values(?, ?)", "aaa", 20);
36+
jdbcTemplate1.update("insert into user(name,age) values(?, ?)", "bbb", 30);
37+
38+
// 往第二个数据源中插入 1 条数据,若插入的是第一个数据源,则会主键冲突报错
39+
jdbcTemplate2.update("insert into user(name,age) values(?, ?)", "ccc", 20);
40+
41+
// 查一下第一个数据源中是否有 2 条数据,验证插入是否成功
42+
Assert.assertEquals("2", jdbcTemplate1.queryForObject("select count(1) from user", String.class));
43+
44+
// 查一下第一个数据源中是否有 1 条数据,验证插入是否成功
45+
Assert.assertEquals("1", jdbcTemplate2.queryForObject("select count(1) from user", String.class));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)