Spring boot 如何";“允许远程数据库创建”;什么时候使用h2数据库?

Spring boot 如何";“允许远程数据库创建”;什么时候使用h2数据库?,spring-boot,h2,Spring Boot,H2,我正在尝试使用H2数据库创建一个Spring引导项目,其他程序也可以访问该项目 应用程序属性 spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dia

我正在尝试使用H2数据库创建一个Spring引导项目,其他程序也可以访问该项目

应用程序属性

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
SpringBootMyApplication.java

例外情况是:

Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/onz03589/db/tech" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200]

如何实际“允许远程数据库创建”

您需要将
“-ifNotExists”
参数添加到
Server.createTcpServer()
。但是,同样,您不应该将它与
“-tcpallowthers”
一起使用,除非您的端口受到某种程度的保护。

您需要添加spring boot starter jpa依赖项以启用
h2
数据库设置的自动配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

org.springframework.boot
spring引导启动器数据jpa

我在pom.xml中添加了这两个依赖项

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>runtime</scope>
    </dependency>
这对我很有效

如果在创建数据库时要插入任何表或数据。您可以通过将.sql文件放在resources目录下来实现

现在,您可以在上打开数据库
http://localhost:port/h2-console

当您使用
-tcpAllowOthers
时,千万不要这样做,否则会在系统中造成远程安全漏洞。你真的有理由启用对服务器的远程访问吗?@EvgenijRyazanov这是一个没有任何internet连接的内部服务器。我正在创建一个基本应用程序,但没有添加此依赖项。添加此依赖项后,数据库将自动创建
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.200</version>
        <scope>runtime</scope>
    </dependency>
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect