簡體   English   中英

如何使用 Spring Boot 作為遠程數據庫而不是嵌入模式連接到 H2?

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

我的小 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 使用stapler.h2.db ,您可以在此處了解如何執行此操作: Why is my Embedded h2 program writing to a .mv.db file

(非常感謝 Stéphane Nicoll 幫我回答這個問題)

在您的 application.properties 中試試這個。 它對我有用:

  spring.datasource.url=jdbc:h2:~/test
  spring.datasource.driverClassName=org.h2.Driver
  spring.datasource.username=sa
  spring.datasource.password=
  spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM