如何使用Spring Boot将H2作为远程数据库而不是嵌入式模式连接到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)");
    }
}

我可以看到这是在启动时记录的,我认为没有更多关于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

由于上述原因,我想在主目录中看到一个“ stapler.h2.db”文件,情况并非如此。要在此处更改什么以使DB文件出现?

肯尼·巴斯塔尼(Kenny Bastani)

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

如果要使用JDBC将H2用作远程数据库,则需要确保已在连接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 Embeddedstapler.h2.db代替使用,您可以在此处学习如何做:为什么我的嵌入式H2程序写入.mv.db文件?

(非常感谢StéphaneNic​​oll帮助我回答了这一问题)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring H2嵌入式数据库

嵌入式H2数据库的Spring Boot版本

如何使用Spring Boot连接到h2数据库?

如何使用H2嵌入式数据库获取序列中的nextvalue?

Spring Boot中嵌入式H2数据库的默认名称是什么?

如何检查是否创建了 h2 嵌入式数据库?

如何从Java和H2 DB连接到H2数据库

Spring和H2数据库-无法连接到H2数据库

如何在嵌入式H2数据库引擎运行时对其进行备份?

如何在 Mockito 单元测试期间生成 H2 嵌入式数据库?

如何允许多个用户同时连接到我的H2数据库?

使用Datagrip客户端连接到H2数据库

无法使用来自Jhipster的给定路由连接到H2数据库

连接到H2数据库时,DriverManager尝试使用mariadb驱动程序

使用 RobotFramework 连接到 H2 数据库文件

如何配置spring-boot以使用基于文件的H2数据库

带有嵌入式H2的Tomcat WebApp:数据库可能已在使用中:“已被另一个进程锁定”

Spring的嵌入式H2数据源和DB_CLOSE_ON_EXIT

Play框架:在生产模式下使用h2数据库进行开发和postgresql,以及如何通过conf文件连接到postgresql

使用 Spring Boot JPA 在 H2 数据库中插入数据

推荐使用Spring Boot开发时使用H2数据库吗?

如何使用Jhipster再生H2数据库

如何在H2数据库中存储文件-Spring Boot

如何从H2数据库中的用户预加载Spring Boot集成测试?

使用Spring Boot和H2读取数据库对象

使用JPA进行Spring Boot-无法从h2数据库返回对象

使用数据库H2在Spring Boot中进行测试

如何将H2数据库嵌入交付给客户端的jar文件中?

我如何使用表格将数据插入我的H2数据库