【发布时间】:2021-01-06 08:18:28
【问题描述】:
我正在使用带有 Spring Boot(版本 2.3.3.RELEASE)的 H2 数据库以及 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 数据库(由 springboot 配置并启动所有默认配置)时启动应用程序后,
我说错了
找不到数据库“mem:testDB”,请预先创建或允许远程 创建数据库
如何使用所有默认凭据连接到由 Spring Boot 配置和启动的 H2 数据库。
我不想覆盖application.properties 文件中的任何配置,spring.h2.console.enabled=true 除外。
【问题讨论】:
-
试试小写:
jdbc:h2:mem:testdb -
你提供的 spring.datasource.url 值是什么,基于它将配置内存数据库
-
我没有提供任何像“spring.datasource.url”这样的属性。我想让 spring 使用默认配置配置数据库。 @Selindek:我也尝试使用小写的“jdbc:h2:mem:testdb”,但仍然出现相同的错误
-
尝试 jdbc:h2:~/test 因为 h2 会自动在您的用户目录下创建一个测试数据库。查看快速入门:h2database.com/html/quickstart.html
标签: java spring-boot h2 in-memory-database