java 如何使用 Spring Boot 作为远程数据库而不是嵌入模式连接到 H2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32875524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:54:26  来源:igfitidea点击:

How do you connect to H2 as a remote database instead of embedded mode using Spring Boot?

javaspring-bootspring-datah2

提问by kabeen

I have this configuration under src/main/resources for my little Spring Boot application:

我的小 Spring Boot 应用程序在 src/main/resources 下有这个配置:

server.port = 8090
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.url = jdbc:h2:file:~/stapler

I know this configuration is picked up properly, cause there is valid port number 8090 in application startup log. There is also a @PostConstruct initDb() method which creates and inserts data into 2 tables of that database:

我知道这个配置被正确选取,因为应用程序启动日志中有有效的端口号 8090。还有一个@PostConstruct initDb() 方法,它创建数据并将数据插入到该数据库的 2 个表中:

package com.avk.stapler.init;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class DbInitializer {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        SpringApplication.run(DbInitializer.class, args);
    }

    @PostConstruct
    private void initDb() {
        System.out.println("Creating table employees");
        jdbcTemplate.execute("drop table employees if exists");
        jdbcTemplate.execute("create table employees(id serial, name varchar(255), surname varchar(255))");
        jdbcTemplate.execute("insert into employees(name, surname) values('Jan', 'Kowalski')");
        jdbcTemplate.execute("insert into employees(name, surname) values('Stefan', 'Nowak')");


        System.out.println("Creating table allocations");
        jdbcTemplate.execute("drop table allocations if exists");
        jdbcTemplate.execute("create table allocations(id serial, week int, year int, shift int, employee_id bigint)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(29, 2015, 1, 1)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(28, 2015, 2, 1)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(29, 2015, 3, 2)");
        jdbcTemplate.execute("insert into allocations(week, year, shift, employee_id) values(28, 2015, 2, 2)");
    }
}

I can see this logged on startup, I don't think there are more logs regarding DB:

我可以看到这个登录启动,我认为没有更多关于数据库的日志:

2015-09-30 22:41:22.948  INFO 2832 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Creating embedded database 'testdb'
Creating table employees
Creating table allocations

And as a result of above, I'd like to see a "stapler.h2.db" file in my home directory, which is not the case. What should be changed here for the DB file to appear?

由于上述原因,我希望在我的主目录中看到一个“stapler.h2.db”文件,但事实并非如此。这里应该更改什么才能显示 DB 文件?

回答by Kenny Bastani

Make sure that your maven dependencies look like this:

确保您的 Maven 依赖项如下所示:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

If you want to use H2 as a remote database using JDBC, you need to make sure that you are already running an H2 database at the specified file path in your connection url.

如果要将 H2 用作使用 JDBC 的远程数据库,则需要确保已在连接 url 中指定的文件路径中运行 H2 数据库。

If you haven't already installed H2, you can get the instructions to run H2 in server mode here: http://www.h2database.com/html/tutorial.html#tutorial_starting_h2_console

如果您还没有安装 H2,您可以在此处获取在服务器模式下运行 H2 的说明:http: //www.h2database.com/html/tutorial.html#tutorial_starting_h2_console

Once you have it running, you can connect to it using the same JDBC connection URL you've provided. Just use the following application properties.

一旦它运行起来,您就可以使用您提供的相同 JDBC 连接 URL 连接到它。只需使用以下应用程序属性。

spring.datasource.url=jdbc:h2:tcp://localhost/~/stapler
spring.datasource.username=sa
spring.datasource.password=

If you'd rather that the embedded H2 database create your H2 file, that's also possible. Just use the configuration below.

如果您希望嵌入式 H2 数据库创建您的 H2 文件,那也是可能的。只需使用下面的配置。

spring.datasource.url=jdbc:h2:file:~/stapler;AUTO_SERVER=true
spring.datasource.username=
spring.datasource.password=

It's possible that the file that is created will be named stapler.mv.db. To tell H2 embedded to use stapler.h2.dbinstead, you can learn how to do that here: Why is my embedded h2 program writing to a .mv.db file

创建的文件可能会被命名为stapler.mv.db. 要告诉 H2 Embedded 改为使用stapler.h2.db,您可以在这里学习如何做到这一点:Why is my Embedded h2 program writing to a .mv.db file

(Big thanks to Stéphane Nicoll for helping me answer this one)

(非常感谢 Stéphane Nicoll 帮我回答这个问题)