Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot-配置了两个数据源-如何使用第二个数据源?_Java_Mysql_Jpa_Spring Boot_Spring Data - Fatal编程技术网

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