h2 localhost URL与testdb不同 [英] h2 localhost url different from testdb
问题描述
我正在使用下面的spring boot config:
I am using below spring boot config:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
当我的春季应用程序启动时,我看到以下内容:
When my spring app comes up, I see the following:
H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:304a69fe-27f6-4271-a5c3-015f06910885'
但是,如果我在属性文件中设置以下内容,我确实会看到testdb已连接:
However if i set the below in property file, i do see testdb being connected:
spring.datasource.url=jdbc:h2:mem:testdb
有人可以让我知道为什么我需要在属性文件中显式设置URL吗?我最近用完全相同的配置创建了另一个spring boot应用程序,但配置为spring boot版本2.2.4.RELEASE,其中h2默认连接到testdb ,而没有在属性文件中设置.
Can someone please let me know why do i need to explicitly set the url in property file? I had created another spring boot app recently with the exact same config but with spring boot version 2.2.4.RELEASE where h2 connected by default to testdb without setting it in property file.
谢谢!
推荐答案
发现,在最新版本的Spring Boot(2.3+)中,每次重新启动服务器时都会随机生成H2数据库名称.类似的帖子: springboot 2.3.0,同时连接到h2数据库
Found out that with the latest versions of Spring Boot (2.3+), the H2 database name is randomly generated each time you restart the server. Similar post: springboot 2.3.0 while connecting to h2 database
这篇关于h2 localhost URL与testdb不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!