内存数据库中的Springboot H2-无法连接

时间:2020-04-04 19:04:55

标签: spring-boot h2

我为springboot应用程序配置了H2,如下所示:

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

# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2

当我登录时,它说找不到数据库。我像这样明确输入jdbc:h2:mem:testdb

enter image description here

我还能做些什么来排除故障?我确实有一个data.sql文件,用于创建架构并放入示例数据。

2 个答案:

答案 0 :(得分:0)

我通读了其他一些教程,其中提到了maven / gradle中的jdbcjpa。当我添加其中任何一个时,H2似乎都可以正常工作。

答案 1 :(得分:0)

有同样的问题,我所做的工作是:

application.properties:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:uniqueNewName <<testdb did not work!
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
springspring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

pom.xml:

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

    <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10 -->
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc10</artifactId>
        <version>19.8.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.3.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

不要问我为什么,但我必须将 jbdc 和 spring-boot-starter-data-jpa 添加到我的依赖项中才能使其工作。