INIT=RUNSCRIPT 和相对路径的问题

发布于 2024-10-08 07:28:48 字数 381 浏览 0 评论 0原文

我对源路径 (src/main src/test) 使用 Maven 约定,并且我的 sql 脚本位于 src/main/resources/scripts 中。

我想使用 H2 内存运行我的应用程序,并且我想使用 jdbc url 来初始化我的数据库:

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/main/resources/scripts/create.sql';

我的问题是这个相对路径(src/main/...)不起作用,并且 H2 也不起作用如果 init=runscript 命令没有针对任何目标,则会崩溃。

有人知道我应该使用什么路径来完成这项工作吗?

谢谢

I use maven conventions for source paths (src/main src/test) and i have my sql scripts in src/main/resources/scripts.

I want to run my app with H2 memory and i'd like to use the jdbc url to initialize my db :

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/main/resources/scripts/create.sql';

My problem is that this relative path (src/main/... ) does not work, and that H2 won't crash if the init=runscript command targets nothing.

Does someone know what is the path i should use to make this work ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宛菡 2024-10-15 07:28:49

您可以使用以下网址:

"jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'"

有了这个,就可以从类路径运行脚本。所以你可以把它放在 src/main/resources/scripts 或 src/test/resources/scripts 到你的 maven (或其他)项目中。

You can use the following url:

"jdbc:h2:mem:sample;INIT=RUNSCRIPT FROM 'classpath:scripts/create.sql'"

With that one it is possible to run script from classpath. So you can just put it src/main/resources/scripts or src/test/resources/scripts in your maven (or something else) project.

独留℉清风醉 2024-10-15 07:28:49

我建议初学者尝试使用绝对路径,只是为了检查一切是否正常。然后,检查您的类路径。例如,bin/main/resources/scripts/create.sql,假设 bin 是编译类的位置,并且位于类路径上。

由于源代码所在的 src 通常不在类路径上,因此这可能是问题的根源。

I'd suggest trying to use an absolute path for starters, just to check everything works. Afterwards, check your classpath. For example, bin/main/resources/scripts/create.sql, assuming bin is where your classes are compiled, and is on your classpath.

Since src, where your source lives, usually isn't on the classpath, this could be the source of your problem.

提笔书几行 2024-10-15 07:28:49

对我来说,如果我将脚本保存在 src/test/resources 下,它就可以工作:

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/test/resources/create.sql';

无论如何,在 JUnit 上下文中,如果您使用传统的文件路径 schema.sql (对于DDL) 和 src/test/resources 下的 data.sql(对于 DDL),您不需要在连接字符串中使用 INIT 变量指定脚本。
您只需使用:

database.url=jdbc:h2:mem:testdb

,JUnit 将在内存中创建 H2 实例时自动运行脚本。

For me it works if I keep the scripts under src/test/resources:

database.url=jdbc:h2:mem:;INIT=RUNSCRIPT FROM 'src/test/resources/create.sql';

Anyway, in a JUnit context, if you use the conventional file paths schema.sql (for the DDL) and data.sql (for the DDL) under src/test/resources, you don't need the specify the scripts with the INIT variable in the connection string.
You can simply use:

database.url=jdbc:h2:mem:testdb

and JUnit will automatically run the scripts when creating the H2 instance in memory.

戏舞 2024-10-15 07:28:49

您的问题是您正在指定 'src/
您需要改为 ./src

我遇到了同样的问题,此更改修复了它:

spring.datasource.url=jdbc:h2:./src/main/resources/data.sql;INIT=RUNSCRIPT FROM './src/main/resources/data.sql'

如果您使用 Docker 和 Kubernetes,

请将此行添加到 Dockerfile

COPY src/main/resources/data.sql /app/src/main/resources/

Your issue is you are specifying 'src/
You need to do ./src instead.

I had the same issue and this change fixed it:

spring.datasource.url=jdbc:h2:./src/main/resources/data.sql;INIT=RUNSCRIPT FROM './src/main/resources/data.sql'

If you are using Docker and Kubernetes,

add this line to Dockerfile

COPY src/main/resources/data.sql /app/src/main/resources/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文