企業分散式微服務雲SpringCloud SpringBoot mybatis (十)Spring Boot多資料來源配置與使用Spring-data-jpa支援

allalongx發表於2018-02-23

Spring-data-jpa支援

對於資料來源的配置可以沿用上例中DataSourceConfig的實現。

新增對第一資料來源的JPA配置,注意兩處註釋的地方,用於指定資料來源對應的Entity實體和Repository定義位置,用@Primary區分主資料來源。

@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配置,內容與第一資料來源類似,具體如下:

@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介面 @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介面 @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> {

} 接下來通過測試用例來驗證使用這兩個針對不同資料來源的配置進行資料操作。

@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());

}
複製程式碼

}

企業分散式微服務雲SpringCloud SpringBoot mybatis (十)Spring Boot多資料來源配置與使用Spring-data-jpa支援
原始碼來源:http://minglisoft.cn/honghu/technology.html

相關文章