INIT=RUNSCRIPT 和相对路径的问题
我对源路径 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用以下网址:
"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.
我建议初学者尝试使用绝对路径,只是为了检查一切是否正常。然后,检查您的类路径。例如,
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
, assumingbin
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.对我来说,如果我将脚本保存在
src/test/resources
下,它就可以工作:无论如何,在 JUnit 上下文中,如果您使用传统的文件路径
schema.sql
(对于DDL) 和src/test/resources
下的data.sql
(对于 DDL),您不需要在连接字符串中使用 INIT 变量指定脚本。您只需使用:
,JUnit 将在内存中创建 H2 实例时自动运行脚本。
For me it works if I keep the scripts under
src/test/resources
:Anyway, in a JUnit context, if you use the conventional file paths
schema.sql
(for the DDL) anddata.sql
(for the DDL) undersrc/test/resources
, you don't need the specify the scripts with the INIT variable in the connection string.You can simply use:
and JUnit will automatically run the scripts when creating the H2 instance in memory.
您的问题是您正在指定
'src/
您需要改为
./src
。我遇到了同样的问题,此更改修复了它:
如果您使用 Docker 和 Kubernetes,
请将此行添加到 Dockerfile
Your issue is you are specifying
'src/
You need to do
./src
instead.I had the same issue and this change fixed it:
If you are using Docker and Kubernetes,
add this line to Dockerfile