Spring / H2控制台已连接到数据库,但未显示任何数据

时间:2020-08-18 15:07:17

标签: java spring database h2

我想将数据库从spring连接到h2控制台,但是当我这样做时它不显示任何表,我尝试了stackoverflow和google的所有解决方案,但似乎没有帮助 这是我的application.properties文件:

spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.url=jdbc:h2:mem:testdb

我的数据库名称(如果是Data.sql,并且存储在资源中)

pom.xml的内容:

4.0.0 org.springframework.boot 弹簧启动启动器父母 2.3.3发行 无名 项目 0.0.1-快照 费用 Spring Boot的演示项目

<properties>
    <java.version>11</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <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>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
[![测试连接] [1]] [1]

after connection

和db:

insert into user values (1,'Siamak', 'Codeengine11@gmail.com')
insert into user values (2,'John', 'John@john.com')
insert into user values (3,'Adam', 'adam@adam.com')


insert into category values (1,'Travel')
insert into category values (2,'Auto Loan')
insert into category  values (3,'Travel')


insert into expense values (100,'New York Business Trip','2019-06-16T17:00:00.000Z','New York',1,1)
insert into expense values (101,'Ford Mustang Payment','2019-06-15T15:00:00.000Z','Los Angeles',2,2)
insert into expense values(102,'Grand Canyon Trip With Family','2019-06-15T15:00:00.000Z','Arizona',3,1)
 

型号:

@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table(name="user")
public class User {
    
    @Id
    private Long id;
     
    private String name;
    
    private String email;
    
}

当我删除时:

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

h2控制台给我以下错误:

Database "mem:testdb" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149

1 个答案:

答案 0 :(得分:0)

运行h2的应用程序中的我的application.properties看起来像这样:

spring.datasource.url=jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver

spring.datasource.platform=h2
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto=create

# show sql commands
spring.jpa.show-sql=true