java - Hibernate/Spring框架: path of the database in a configuration file

标签 java spring hibernate sqlite spring-boot

我正在尝试编写一个简单的 Hibernate 应用程序,并且希望根据应用程序配置文件更改 SQLite 数据库位置。

因此,我想做的是从项目中的文本文件中检索数据库的路径并将其放入 dbLocation 中,然后运行以下代码:

Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", "jdbc:sqlite:" + dbLocation);

是否有更好、更“标准”的方法来做到这一点?我正在为我的应用程序使用 Spring Boot,我刚刚意识到有一个名为 application.properties 的文件。我可以用这个吗?我对 Hibernate 和 Spring 框架都很陌生。

最佳答案

在 Spring Boot 应用程序属性中,您可以外部化应用程序属性,以便在应用程序源代码之外配置/管理它们。

在 application.properties 中定义属性后,您可以使用 SpringBoot 内置功能来访问值

@Configuration 
public class ApplicationProperty {

@Value("${prop}")
private String prop;

关于java - Hibernate/Spring框架: path of the database in a configuration file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719178/

相关文章:

Spring Cloud zuul "path": "/actuator/routes" 404 not found

spring - 我们可以使用 HTML5 从手机的相机上传照片吗

hibernate - 如何使用hibernate.load修复 "is in unnamed module of loader '应用';“

java - 将 JSON 转换为映射并使用变量传递 JSOn 数据

java - 带注解的 Hibernate 拦截器

java - Java如何决定三元条件运算符表达式的类型?

java - 类型PreparedStatement中的错误: The method setString(int, String)不适用于参数(int,Object)

java.lang.IllegalAccessError : class javax. xml.parsers.SecuritySupport12 无法访问其父类(super class) javax.xml.parsers.SecuritySupport

java - Hibernate 事务内自定义 id 生成

java - Hibernate 无效配置映射