Java Spring Boot-配置了两个数据源-如何使用第二个数据源?
我在我的Java Spring Boot-配置了两个数据源-如何使用第二个数据源?,java,mysql,jpa,spring-boot,spring-data,Java,Mysql,Jpa,Spring Boot,Spring Data,我在我的springboot应用程序中从MySQL数据库配置了两个模式,并将一个数据源标记为主数据源 现在,我的问题是如何“使用”第二个数据源?我使用的是基于JpaRepository的方法,当我尝试将某些内容保存到第二个DB模式中的表时,我的Spring boot应用程序总是尝试在第一个DB模式中查找该表,并最终抛出“表不存在”错误 另外,我已经在我的实体类中正确地标记了第二个DB模式 下面是我的应用程序.properties文件(前缀已更改为省略机密名称): 在此博客上找到解决方案- 解决方
springboot
应用程序中从MySQL数据库配置了两个模式,并将一个数据源标记为主数据源
现在,我的问题是如何“使用”第二个数据源?我使用的是基于JpaRepository
的方法,当我尝试将某些内容保存到第二个DB模式中的表时,我的Spring boot应用程序总是尝试在第一个DB模式中查找该表,并最终抛出“表不存在”错误
另外,我已经在我的实体
类中正确地标记了第二个DB模式
下面是我的应用程序.properties
文件(前缀已更改为省略机密名称):
在此博客上找到解决方案- 解决方法是,必须将应用程序配置类拆分为两个,并使用如下所示的内容对每个类进行注释: 配置类1:
@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties({ MyServicefacadeProperties.class })
@EnableJpaRepositories(entityManagerFactoryRef = "myEntityManagerFactory", transactionManagerRef = "myTransactionManager", basePackages = {
"com.myorg.foo" })
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "masterEntityManagerFactory", transactionManagerRef = "masterTransactionManager", basePackages = {
"com.myorg.foo.master" })
配置类2:
@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties({ MyServicefacadeProperties.class })
@EnableJpaRepositories(entityManagerFactoryRef = "myEntityManagerFactory", transactionManagerRef = "myTransactionManager", basePackages = {
"com.myorg.foo" })
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "masterEntityManagerFactory", transactionManagerRef = "masterTransactionManager", basePackages = {
"com.myorg.foo.master" })
一个数据源应该驻留在第一个配置类中,第二个数据源驻留在另一个配置类中。此外,将实体类和JpaRepository接口移动到各自的包中,如上面的注释所示
干杯post
application.properties
@JohnJoe:post