Spring Boot 2

Spring Boot 2,第1张

  1. 分别创建两个数据源的JPA配置。

Primary数据源的JPA配置:

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(

entityManagerFactoryRef=“entityManagerFactoryPrimary”,

transactionManagerRef=“transactionManagerPrimary”,

basePackages= { “com.didispace.chapter38.p” }) //设置Repository所在位置

public class PrimaryConfig {

@Autowired

@Qualifier(“primaryDataSource”)

private DataSource primaryDataSource;

@Autowired

private JpaProperties jpaProperties;

@Autowired

private HibernateProperties hibernateProperties;

private Map getVendorProperties() {

return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());

}

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

.packages(“com.didispace.chapter38.p”) //设置实体类所在位置

.persistenceUnit(“primaryPersistenceUnit”)

.properties(getVendorProperties())

.build();

}

@Primary

@Bean(name = “transactionMa 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 nagerPrimary”)

public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());

}

}

Secondary数据源的JPA配置:

@Configuration

@EnableTransactionManagement

@EnableJpaRepositories(

entityManagerFactoryRef=“entityManagerFactorySecondary”,

transactionManagerRef=“transactionManagerSecondary”,

basePackages= { “com.didispace.chapter38.s” }) //设置Repository所在位置

public class SecondaryConfig {

@Autowired

@Qualifier(“secondaryDataSource”)

private DataSource secondaryDataSource;

@Autowired

private JpaProperties jpaProperties;

@Autowired

private HibernateProperties hibernateProperties;

private Map getVendorProperties() {

return hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());

}

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

.packages(“com.didispace.chapter38.s”) //设置实体类所在位置

.persistenceUnit(“secondaryPersistenceUnit”)

.properties(getVendorProperties())

.build();

}

@Bean(name = “transactionManagerSecondary”)

PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {

return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());

}

}

说明与注意:

  • 在使用JPA的时候,需要为不同的数据源创建不同的package来存放对应的Entity和Repository,以便于配置类的分区扫描

  • 类名上的注解@EnableJpaRepositories中指定Repository的所在位置

  • LocalContainerEntityManagerFactoryBean创建的时候,指定Entity所在的位置

  • 其他主要注意在互相注入时候,不同数据源不同配置的命名,基本就没有什么大问题了

[](()测试一下


完成了上面之后,我们就可以写个测试类来尝试一下上面的多数据源配置是否正确了,比如下面这样:

@Slf4j

@RunWith(SpringRunner.class)

@SpringBootTest

public class Chapter38ApplicationTests {

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/799734.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存