ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2019-07-22 15:02:57  阅读:637  来源: 互联网

标签: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)");
    }
}

我可以看到这个登录启动时,我不认为有更多关于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文件出现的内容?

解决方法:

确保你的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嵌入式使用stapler.h2.db,您可以在此处学习如何操作:Why is my embedded h2 program writing to a .mv.db file

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

标签:java,spring-boot,spring-data,h2
来源: https://codeday.me/bug/20190722/1504057.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有