在实际开发中,我们会遇到一个应用需要访问多个数据源的情况。因此,我们需要配置多个数据源。这里我们来看看使用 Jpa 实现多数据源配置。
添加 JPA 依赖
1 | <dependency> |
2 | <groupId>org.springframework.boot</groupId> |
3 | <artifactId>spring-boot-starter-data-jpa</artifactId> |
4 | </dependency> |
配置数据源信息
- application.properties
1 | # 数据源一 |
2 | jdbc:mysql://localhost:3306/product?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC = |
3 | com.mysql.jdbc.Driver = |
4 | root = |
5 | root = |
6 | |
7 | # 数据源二 |
8 | jdbc:mysql://localhost:3306/game?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC = |
9 | com.mysql.jdbc.Driver = |
10 | root = |
11 | root = |
12 | |
13 | # JPA 配置 |
14 | mysql = |
15 | true = |
16 | true = |
17 | false = |
18 | update = |
19 | org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy = |
20 | org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy = |
21 | org.hibernate.dialect.MySQL5Dialect = |
配置多数据源
集成 JPA 配置数据源步骤如下:
- 创建数据源
DataSource
- 创建实体管理器工厂对象
LocalContainerEntityManagerFactoryBean
- 创建实体管理器对象
EntityManager
- 创建事务管理器对象
PlatformTransactionManager
数据源一
1 |
|
2 |
|
3 | ( |
4 | entityManagerFactoryRef = "entityManagerFactoryPrimary", |
5 | transactionManagerRef = "transactionManagerPrimary", |
6 | basePackages = {"com.example.jpa.repository.primary"}) |
7 | public class PrimaryDataSourceConfig { |
8 | |
9 | |
10 | private JpaProperties jpaProperties; |
11 | |
12 | |
13 | private HibernateProperties hibernateProperties; |
14 | |
15 | "primaryDataSource") (name = |
16 | |
17 | "spring.datasource.primary") (prefix = |
18 | public DataSource dataSource() { |
19 | return DataSourceBuilder.create().build(); |
20 | } |
21 | |
22 | private Map<String, Object> getVendorProperties() { |
23 | return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings()); |
24 | } |
25 | |
26 | "entityManagerFactoryPrimary") (name = |
27 | |
28 | public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) { |
29 | return builder.dataSource(dataSource()) |
30 | .properties(getVendorProperties()) |
31 | // 设置映射的实体类位置 |
32 | .packages("com.example.jpa.entity.primary") |
33 | .persistenceUnit("primaryPersistenceUnit") |
34 | .build(); |
35 | } |
36 | |
37 | "entityManagerPrimary") (name = |
38 | |
39 | public EntityManager entityManager(EntityManagerFactoryBuilder builder){ |
40 | return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); |
41 | } |
42 | |
43 | "transactionManagerPrimary") (name = |
44 | |
45 | public PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { |
46 | return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); |
47 | } |
48 | } |
数据源二
1 |
|
2 |
|
3 | ( |
4 | entityManagerFactoryRef = "entityManagerFactorySecondary", |
5 | transactionManagerRef = "transactionManagerSecondary", |
6 | basePackages = {"com.example.jpa.repository.secondary"}) |
7 | public class SecondaryDataSourceConfig { |
8 | |
9 | |
10 | private JpaProperties jpaProperties; |
11 | |
12 | |
13 | private HibernateProperties hibernateProperties; |
14 | |
15 | "secondaryDatasource") (name = |
16 | "spring.datasource.secondary") (prefix = |
17 | public DataSource dataSource() { |
18 | return DataSourceBuilder.create().build(); |
19 | } |
20 | |
21 | private Map<String, Object> getVendorProperties() { |
22 | return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings()); |
23 | } |
24 | |
25 | "entityManagerFactorySecondary") (name = |
26 | public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) { |
27 | return builder.dataSource(dataSource()) |
28 | .properties(getVendorProperties()) |
29 | // 设置映射的实体类位置 |
30 | .packages("com.example.jpa.entity.secondary") |
31 | .persistenceUnit("secondaryPersistenceUnit") |
32 | .build(); |
33 | |
34 | } |
35 | |
36 | "entityManagerSecondary") (name = |
37 | public EntityManager entityManagerSecondary(EntityManagerFactoryBuilder builder) { |
38 | return entityManagerFactorySecondary(builder).getObject().createEntityManager(); |
39 | } |
40 | |
41 | "transactionManagerSecondary") (name = |
42 | public PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { |
43 | return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); |
44 | } |
45 | } |
以上完成之后,多数据源配置就已经完成了,接下来就可以操作数据啦!
最后, 详细代码可以查看本示例的 Demo。