之前在介绍使用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
|
public
class DataSourceConfig {
"primaryDataSource")
(name =
"primaryDataSource")
(
"spring.datasource.primary")
(prefix=
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
"secondaryDataSource")
(name =
"secondaryDataSource")
(
"spring.datasource.secondary")
(prefix=
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
|
对应的application.properties
配置如下:
1
2
3
4
5
6
7
8
9
|
spring.datasource.primary.url=jdbc:mysql://localhost:3306/
test1
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/
test2
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
|
对JdbcTemplate的支持比较简单,只需要为其注入对应的datasource即可,如下例子,在创建JdbcTemplate的时候分别注入名为primaryDataSource
和secondaryDataSource
的数据源来区分不同的JdbcTemplate。
1
2
3
4
5
6
7
8
9
10
11
12
|
"primaryJdbcTemplate")
(name =
public JdbcTemplate primaryJdbcTemplate(
@Qualifier(
"primaryDataSource") DataSource dataSource) {
return
new JdbcTemplate(dataSource);
}
"secondaryJdbcTemplate")
(name =
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier(
"secondaryDataSource") DataSource dataSource) {
return
new JdbcTemplate(dataSource);
}
|
接下来通过测试用例来演示如何使用这两个针对不同数据源的JdbcTemplate。
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
|
public
class ApplicationTests {
"primaryJdbcTemplate")
(
protected JdbcTemplate jdbcTemplate1;
"secondaryJdbcTemplate")
(
protected JdbcTemplate jdbcTemplate2;
public void setUp() {
jdbcTemplate1.update(
"DELETE FROM USER ");
jdbcTemplate2.update(
"DELETE FROM USER ");
}
public void test() throws Exception {
// 往第一个数据源中插入两条数据
jdbcTemplate1.update(
"insert into user(id,name,age) values(?, ?, ?)",
1,
"aaa",
20);
jdbcTemplate1.update(
"insert into user(id,name,age) values(?, ?, ?)",
2,
"bbb",
30);
// 往第二个数据源中插入一条数据,若插入的是第一个数据源,则会主键冲突报错
jdbcTemplate2.update(
"insert into user(id,name,age) values(?, ?, ?)",
1,
"aaa",
20);
// 查一下第一个数据源中是否有两条数据,验证插入是否成功
Assert.assertEquals(
"2", jdbcTemplate1.queryForObject(
"select count(1) from user", String.class));
// 查一下第一个数据源中是否有两条数据,验证插入是否成功
Assert.assertEquals(
"1", jdbcTemplate2.queryForObject(
"select count(1) from user", String.class));
}
}
|
完整示例:Chapter3-2-3
对于数据源的配置可以沿用上例中DataSourceConfig
的实现。
新增对第一数据源的JPA配置,注意两处注释的地方,用于指定数据源对应的Entity
实体和Repository
定义位置,用@Primary
区分主数据源。
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
|
entityManagerFactoryRef=
"entityManagerFactoryPrimary",
transactionManagerRef=
"transactionManagerPrimary",
basePackages= {
"com.didispace.domain.p" })
//设置Repository所在位置
public
class PrimaryConfig {
"primaryDataSource")
(
private DataSource primaryDataSource;
"entityManagerPrimary")
(name =
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
}
"entityManagerFactoryPrimary")
(name =
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(primaryDataSource)
.properties(getVendorProperties(primaryDataSource))
.packages(
"com.didispace.domain.p")
//设置实体类所在位置
.persistenceUnit(
"primaryPersistenceUnit")
.build();
}
private JpaProperties jpaProperties;
private Map
return jpaProperties.getHibernateProperties(dataSource);
}
"transactionManagerPrimary")
(name =
public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
return
new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
}
}
|
新增对第二数据源的JPA配置,内容与第一数据源类似,具体如下:
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
|
entityManagerFactoryRef=
"entityManagerFactorySecondary",
transactionManagerRef=
"transactionManagerSecondary",
basePackages= {
"com.didispace.domain.s" })
//设置Repository所在位置
public
class SecondaryConfig {
"secondaryDataSource")
(
private DataSource secondaryDataSource;
"entityManagerSecondary")
(name =
public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
return entityManagerFactorySecondary(builder).getObject().createEntityManager();
}
"entityManagerFactorySecondary")
(name =
public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
return builder
.dataSource(secondaryDataSource)
.properties(getVendorProperties(secondaryDataSource))
.packages(
"com.didispace.domain.s")
//设置实体类所在位置
.persistenceUnit(
"secondaryPersistenceUnit")
.build();
}
private JpaProperties jpaProperties;
private Map
return jpaProperties.getHibernateProperties(dataSource);
}
"transactionManagerSecondary")
(name =
PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
return
new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
}
}
|
完成了以上配置之后,主数据源的实体和数据访问对象位于:com.didispace.domain.p
,次数据源的实体和数据访问接口位于:com.didispace.domain.s
。
分别在这两个package下创建各自的实体和数据访问接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
class User {
private Long id;
false)
(nullable =
private String name;
false)
(nullable =
private Integer age;
public User(){}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
// 省略getter、setter
}
|
1
2
3
|
public
interface UserRepository extends JpaRepository<User, Long> {
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
class Message {
private Long id;
false)
(nullable =
private String name;
false)
(nullable =
private String content;
public Message(){}
public Message(String name, String content) {
this.name = name;
this.content = content;
}
// 省略getter、setter
}
|
1
2
3
|
public
interface MessageRepository extends JpaRepository<Message, Long> {
}
|
接下来通过测试用例来验证使用这两个针对不同数据源的配置进行数据操作。
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
|
public
class ApplicationTests {
private UserRepository userRepository;
private MessageRepository messageRepository;
public void test() throws Exception {
userRepository.save(
new User(
"aaa",
10));
userRepository.save(
new User(
"bbb",
20));
userRepository.save(
new User(
"ccc",
30));
userRepository.save(
new User(
"ddd",
40));
userRepository.save(
new User(
"eee",
50));
Assert.assertEquals(
5, userRepository.findAll().size());
messageRepository.save(
new Message(
"o1",
"aaaaaaaaaa"));
messageRepository.save(
new Message(
"o2",
"bbbbbbbbbb"));
messageRepository.save(
new Message(
"o3",
"cccccccccc"));
Assert.assertEquals(
3, messageRepository.findAll().size());
}
}
|
完整示例:Chapter3-2-4