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

标签 java spring-boot spring-data h2

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

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

我知道这个配置是正确的,因为在应用程序启动日志中有有效的端口号 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)");
    }
}

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

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

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

最佳答案

确保您的 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>

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

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

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

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

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

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

创建的文件可能会被命名为 stapler.mv.db。要告诉 H2 embedded 使用 stapler.h2.db,您可以在此处了解如何操作:Why is my embedded h2 program writing to a .mv.db file

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

关于java - 如何使用 Spring Boot 连接到 H2 作为远程数据库而不是嵌入式模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32875524/

相关文章:

spring-boot - Docker-compose在运行数小时后崩溃

具有管道聚合的 Spring Data MongoDB 查找

java - 如何强制 Maven 3.1 在 Mac OS 8.10 上使用正确版本的 Java?

java - 将毫秒转换为年数、月数和天数的最佳方法

java - 模拟策略

java - 如何从redis缓存取回Long(数据类型)值

java - 自定义 Java XMLBuilder 与基于标准类的对比

java - Spring 启动 Web 应用程序 : compression is not applying

spring - 向所有存储库添加自定义行为,无需 xml 配置

java - 将存储库 impl 放在非基础包中