gpt4 book ai didi

java - Spring Boot JPA 获取对 EntityManagerFactory 的引用

转载 作者:行者123 更新时间:2023-11-30 02:21:33 25 4
gpt4 key购买 nike

我有一个 Spring-Boot Web 应用程序,并且有一个具有数据访问层的第 3 方 jar。为了使用它,我必须向它发送对 EntityManagerFactory 的引用。我可以这样做:

    Map<String, String> stg = new HashMap<String, String>();
stg.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydb");
stg.put("hibernate.connection.username", "name");
stg.put("hibernate.connection.password", "password");

EntityManagerFactory fact = MultipleEntityManagerFactoryHolder.getEmf("stg", stg);

但是当我将其添加到 application.properties 时,如何获得 Spring(我假设)已经为我构建的属性:

spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false
spring.datasource.username=user
spring.datasource.password=password

我无法访问该 jar,因此我需要工厂,而不是数据源。有人知道如何获得吗?

最佳答案

您可以简单地在 bean 中 Autowiring 它:

@Autowired
private EntityManagerFactory emFactory;

关于java - Spring Boot JPA 获取对 EntityManagerFactory 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46680088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com