如何使用静态sql文件加载h2数据库

问题描述 投票:0回答:2

如何使用静态 sql 文件从我的 Spring Boot 应用程序的 src/main/resources/data.sql 加载 h2 数据库。我无法以以下方式加载数据库,因为它无法获取 data.sql 文件。我将无法获得绝对路径,因为它在 docker 容器中不起作用

spring.datasource.url=jdbc:h2:file:src/main/resources/data.sql
spring-boot h2
2个回答
1
投票

默认情况下,Spring Boot 自动从

schema.sql
data.sql
文件加载 SQL。 您应该将这些文件放在
src/main/resources
文件夹中,仅此而已。 如果您的 h2 数据库配置正确,它应该可以开箱即用。


0
投票

在 Spring Boot 中使用静态 sql 文件加载 schema 的默认方法是在资源文件中使用

schema.sql
。这应该足够了。您不需要从属性文件中指向该文件。

您可以在这里阅读更多相关信息:https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization

© www.soinside.com 2019 - 2024. All rights reserved.