我可以让 H2 在内存数据库中自动创建模式吗?
(我已经看到了 内存中的 H2 数据库 - 初始化模式通过 Spring/Hibernate 问题;它在这里不适用。)
我想知道 H2 中是否有一个设置允许我在连接到它时自动创建一个模式。如果有帮助,我只对内存中的情况感兴趣。
H2 支持在 URL 末尾使用各种分号分隔的修饰符,但我没有找到用于自动创建模式的修饰符。有这样的功能吗?
(I've already seen the H2 database In memory - Init schema via Spring/Hibernate question; it is not applicable here.)
I'd like to know if there's a setting in H2 that will allow me to auto-create a schema upon connecting to it. If it helps, I'm only interested in the in-memory case.
H2 supports various semicolon-separated modifiers at the end of the URL, but I didn't find one for automatically creating a schema. Is there such a feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,H2 支持连接时执行 SQL 语句。您可以运行一个脚本,或者只是一个或两个语句:
请注意,仅在 Java 中才需要双反斜杠 (
\\
)。INIT
中;
之前的反斜杠是必需的。Yes, H2 supports executing SQL statements when connecting. You could run a script, or just a statement or two:
Please note the double backslash (
\\
) is only required within Java. The backslash(es) before;
within theINIT
is required.如果您将 spring 与
application.yml
结合使用,以下内容将适合您:If you are using spring with
application.yml
, the following will work for you:托马斯所写的是正确的,除此之外,如果您想初始化多个模式,您可以使用以下内容。请注意,有一个
\\;
分隔两个 create 语句。参考:http://www.h2database.com/html/features.html#execute_sql_on_connection
What Thomas has written is correct, in addition to that, if you want to initialize multiple schemas you can use the following. Note there is a
\\;
separating the two create statements.ref : http://www.h2database.com/html/features.html#execute_sql_on_connection
“默认情况下,当应用程序调用
DriverManager.getConnection(url, ...)
并且 URL 中指定的数据库尚不存在时,将创建一个新的(空)数据库。”—H2 数据库。附录:@Thomas Mueller 展示了如何在连接上执行 SQL,但有时我只是在代码中创建和填充,如下所示。
"By default, when an application calls
DriverManager.getConnection(url, ...)
and the database specified in the URL does not yet exist, a new (empty) database is created."—H2 Database.Addendum: @Thomas Mueller shows how to Execute SQL on Connection, but I sometimes just create and populate in the code, as suggested below.
如果您将 Spring Framework 与
application.yml
一起使用,并且无法让测试在INIT
属性上找到 SQL 文件,则可以使用classpath:< /code> 符号。
例如,如果您在
src/test/resources
上有一个init.sql
SQL 文件,只需使用:If you are using Spring Framework with
application.yml
and having trouble to make the test find the SQL file on theINIT
property, you can use theclasspath:
notation.For example, if you have a
init.sql
SQL file on thesrc/test/resources
, just use:如果您使用 spring 和 xml 配置,下面是如何完成此操作的示例:
If you are using spring and xml configuration, here is an example how this should be done:
我遇到了一个导致消息混乱的问题!我已被添加
到处理关键字名称的配置中。但它一直说类别
“menu”
不存在。我尝试通过将其篡改为错误来验证 INIT 脚本是否正在运行,并且 h2 导致错误(这是真实的行为)。
所以我确信将会生成
menu
架构。最后我发现 h2 数据库说
"menu"
模式不存在,不是menu
模式!然后我删除了global_quoted_identifiers,一切顺利!
I had a problem that causes confusing message! I had been added
to the configuration for handling keyword names. but it keeps saying category
"menu"
does not exists.I tried to verify if INIT script is running by tampring it to something wrong, and h2 causes an error (which is a true behaviour).
So I was sure that
menu
schema will be generated.Finally I found that h2 database says
"menu"
schema does not exists notmenu
schema!Then I removed globally_quoted_identifiers and everything goes fine!