Spring-data-jpa支持
对于数据源的配置可以沿用上例中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
|
@Configuration @EnableTransactionManagement @EnableJpaRepositories ( entityManagerFactoryRef= "entityManagerFactoryPrimary" , transactionManagerRef= "transactionManagerPrimary" , basePackages= { "com.didispace.domain.p" }) //设置Repository所在位置 public class PrimaryConfig { @Autowired @Qualifier ( "primaryDataSource" ) private DataSource primaryDataSource; @Primary @Bean (name = "entityManagerPrimary" ) public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); } @Primary @Bean (name = "entityManagerFactoryPrimary" ) public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) { return builder .dataSource(primaryDataSource) .properties(getVendorProperties(primaryDataSource)) .packages( "com.didispace.domain.p" ) //设置实体类所在位置 .persistenceUnit( "primaryPersistenceUnit" ) .build(); } @Autowired private JpaProperties jpaProperties; private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Primary @Bean (name = "transactionManagerPrimary" ) 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
|
@Configuration @EnableTransactionManagement @EnableJpaRepositories ( entityManagerFactoryRef= "entityManagerFactorySecondary" , transactionManagerRef= "transactionManagerSecondary" , basePackages= { "com.didispace.domain.s" }) //设置Repository所在位置 public class SecondaryConfig { @Autowired @Qualifier ( "secondaryDataSource" ) private DataSource secondaryDataSource; @Bean (name = "entityManagerSecondary" ) public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactorySecondary(builder).getObject().createEntityManager(); } @Bean (name = "entityManagerFactorySecondary" ) public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) { return builder .dataSource(secondaryDataSource) .properties(getVendorProperties(secondaryDataSource)) .packages( "com.didispace.domain.s" ) //设置实体类所在位置 .persistenceUnit( "secondaryPersistenceUnit" ) .build(); } @Autowired private JpaProperties jpaProperties; private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean (name = "transactionManagerSecondary" ) PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject()); } } |
完成了以上配置之后,主数据源的实体和数据访问对象位于:com.didispace.domain.p
,次数据源的实体和数据访问接口位于:com.didispace.domain.s
。
分别在这两个package下创建各自的实体和数据访问接口
- 主数据源下,创建User实体和对应的Repository接口
1234567891011121314151617181920212223242526
@Entity
public
class
User {
@Id
@GeneratedValue
private
Long id;
@Column
(nullable =
false
)
private
String name;
@Column
(nullable =
false
)
private
Integer age;
public
User(){}
public
User(String name, Integer age) {
this
.name = name;
this
.age = age;
}
// 省略getter、setter
}
public
interface
UserRepository
extends
JpaRepository<User, Long> {
}
- 从数据源下,创建Message实体和对应的Repository接口
1234567891011121314151617181920212223242526
@Entity
public
class
Message {
@Id
@GeneratedValue
private
Long id;
@Column
(nullable =
false
)
private
String name;
@Column
(nullable =
false
)
private
String content;
public
Message(){}
public
Message(String name, String content) {
this
.name = name;
this
.content = content;
}
// 省略getter、setter
}
public
interface
MessageRepository
extends
JpaRepository<Message, Long> {
}
接下来通过测试用例来验证使用这两个针对不同数据源的配置进行数据操作。
1234567891011121314151617181920212223242526272829@RunWith
(SpringJUnit4ClassRunner.
class
)
@SpringApplicationConfiguration
(Application.
class
)
public
class
ApplicationTests {
@Autowired
private
UserRepository userRepository;
@Autowired
private
MessageRepository messageRepository;
@Test
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());
}
}