Spring Boot设置的H2内存数据库的默认配置。

huangapple go评论89阅读模式
英文:

Default configuration of H2 in-memory database set up by Springboot

问题

我正在使用带有 Spring Boot 的 H2 数据库(版本为 2.3.3.RELEASE),使用了 H2 数据库的所有默认设置。

以下是我应用程序的所有文件。

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.3.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

application.properties

spring.h2.console.enabled=true

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.sql.SQLException;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

在启动应用程序后,当我尝试使用以下凭据连接 H2 数据库(由 Spring Boot 配置和启动,使用所有默认配置)时,

Spring Boot设置的H2内存数据库的默认配置。

我收到错误消息

数据库 "mem:testDB" 未找到,请预先创建它或允许远程数据库创建。

如何使用所有默认凭据连接由 Spring Boot 配置和启动的 H2 数据库。

我不想覆盖 application.properties 文件中的任何配置,除了 spring.h2.console.enabled=true

英文:

I am using H2 database with Spring Boot (version 2.3.3.RELEASE) with all default settings for H2 database.

Here are the all files of my application.

pom.xml

&lt;parent&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    &lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
    &lt;relativePath/&gt;
&lt;/parent&gt;

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.h2database&lt;/groupId&gt;
        &lt;artifactId&gt;h2&lt;/artifactId&gt;
        &lt;scope&gt;runtime&lt;/scope&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;

application.properties

spring.h2.console.enabled=true

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.sql.SQLException;

@SpringBootApplication
public class App {
public static void main(String[] args) {
    SpringApplication.run(App.class, args);
 }

}

After starting the application when i am trying to connect the H2 database (configured and started by springboot with all default configuration) using below credentials,

Spring Boot设置的H2内存数据库的默认配置。

I am getting error saying

> Database "mem:testDB" not found, either pre-create it or allow remote
> database creation

How can I connect to H2 database configured and started by Spring Boot with all the default credentials.

I don't want to override any configuration in application.properties files except spring.h2.console.enabled=true.

答案1

得分: 2

在较新版本的Spring Boot(2.2+)中,在您的控制台上查找以下日志消息:在/h2-console页面上使用JDBC URL连接:

Spring Boot 2.2+:

INFO H2ConsoleAutoConfiguration : H2控制台位于'/h2-console'。数据库位于'jdbc:h2:mem:testdb'

Spring Boto 2.3+:

INFO H2ConsoleAutoConfiguration : H2控制台位于'/h2-console'。数据库位于'jdbc:h2:mem:621dd224-01db-4137-807f-b9c3046de64d'

英文:

In newer version of Spring Boot (2.2+), look for the following log message on your console: Use the JDBC URL to connect on /h2-console page:

Spring Boot 2.2+:

INFO H2ConsoleAutoConfiguration : H2 console available at &#39;/h2-console&#39;. Database available at &#39;jdbc:h2:mem:testdb&#39;

Spring Boto 2.3+:

INFO H2ConsoleAutoConfiguration : H2 console available at &#39;/h2-console&#39;. Database available at &#39;jdbc:h2:mem:621dd224-01db-4137-807f-b9c3046de64d&#39;

答案2

得分: -1

仅仅启用控制台是不够的,你还需要说明你想连接哪个数据库。在你的情况下,如果你想连接内存数据库,在下面也添加以下属性,然后尝试使用相同的凭据连接你的内存数据库。

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
英文:

Only enabling console wouldn't be sufficient, you also need to mention which db you want connect. In your case if you want connect in memory db add below properties as well, then try to connect your in memory db using same creds

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

huangapple
  • 本文由 发表于 2020年9月20日 02:10:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定