Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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 JPA配置动态多数据源_Java_Spring_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java 如何使用Spring boot JPA配置动态多数据源

Java 如何使用Spring boot JPA配置动态多数据源,java,spring,spring-boot,spring-data-jpa,Java,Spring,Spring Boot,Spring Data Jpa,我想在运行时选择不同的数据源,下面是我的示例: 在不同的数据库中有两个结构相同的MySQL表,比如testing.io:3306/db/person和production.io:3306/db/person。我想根据一些标准来选择数据库 以下是一些伪代码: 实体类 @Entity @Data public class person{ @Id @GeneratedValue private Long id; private String name; } 它的存储库

我想在运行时选择不同的数据源,下面是我的示例: 在不同的数据库中有两个结构相同的MySQL表,比如testing.io:3306/db/person和production.io:3306/db/person。我想根据一些标准来选择数据库

以下是一些代码:

实体类

@Entity
@Data
public class person{
    @Id
    @GeneratedValue
    private Long id;
    private String name;
}
它的存储库:

   @RepositoryRestResource(collectionResourceRel = "Person", path = "Person")
public interface PersonRepository extends CrudRepository<Person, Long>{

}
我省略了服务接口,因为其中只有一个方法

控制员:

   @RestController
public class PersonrController {
    @Autowired
    PersonService personService ;


    @RequestMapping(value = "/select-person", method= RequestMethod.POST)
    public String selectPerson(@RequestBody parameter) {
        /**
          class Parameter only has one field: env
        /
        return personService.select(Parameter parameter);
    }


}
该服务将实现:

@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    @Use("testing") // It means this repo uses the 'testing' config in the application.yml
    PersonRepository testingPersonRepo;

    @Autowired
    @Use("production") // It means this repo uses the 'production' config in the application.yml
    PersonRepository productionPersonRepo;


    @Override
    public String select(Parameter parameter){
        // dynamically use different DB with the same entity
        if (parameter.getEnv().equals("production")){
            return productionPersonRepo.findAll();
        }else{
            return testingPersonRepo.findAll();
        }

    }
}

如何用Spring Boot JPA优雅地配置它?

您应该使用Spring配置文件。 在应用程序开始时,您告诉环境变量
“spring.profiles.active”
以确定它是测试还是生产,
spring.profiles.active=testing
spring.profiles.active=production

然后在
应用程序中.yml

spring:
  profiles:
    active: testing

spring:
  datasource:
      url: jdbc:mysql://testing.io:3306/db?useSSL=false
      username: root
      password: 1111
 driver-class-name: com.mysql.jdbc.Driver

---
spring:
  profiles:
    active: production

spring:
  datasource:
      url: jdbc:mysql://production.io:3306/db?useSSL=false
      username: root
      password: 2222

这将根据配置文件是一个配置文件还是另一个配置文件为数据源分配值。

首先,感谢您的回答。但我认为你不明白我的意思。如果我使用配置文件,我可以在运行时同时使用这两个配置文件的配置吗?请阅读我的伪代码。如果这是您的要求,您不能在运行时使用两个配置。
spring:
  profiles:
    active: testing

spring:
  datasource:
      url: jdbc:mysql://testing.io:3306/db?useSSL=false
      username: root
      password: 1111
 driver-class-name: com.mysql.jdbc.Driver

---
spring:
  profiles:
    active: production

spring:
  datasource:
      url: jdbc:mysql://production.io:3306/db?useSSL=false
      username: root
      password: 2222