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

标签 java spring spring-boot spring-data-jpa

我想在运行时选择不同的数据源,这是我的示例: 不同的数据库中有2个相同结构的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>{

}

application.yml

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

我省略了服务接口(interface),因为它只有一个方法。

Controller :

   @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=testingspring.profiles.active=product

然后在您的application.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

这将根据配置文件是其中之一来为您的数据源分配值。

关于java - 如何使用 Spring boot JPA 配置动态多数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57090896/

相关文章:

java - 将 Rest API 异常处理为内部服务器错误 (500)

java - 仅将一列作为外键存储在另一个实体中

java - 带有自定义 AngularJS 登录页面的 Spring Boot 和安全性

java - 将 Java ArrayList 与自定义对象一起使用

java - 高度图相关问题

Java随机数异常行为

angularjs - 将文件上传到 Jhipster 中的目录

java - Spring Security 3.2.7 HttpServletRequest.isUserInRole(String) 不会自动添加 "ROLE_"前缀

java - 将 Spring-ws webservicetemplate 请求以 XML 格式保存到 DB

java - 一些 Controller 的 Spring mvc 过滤器