【问题标题】:Spring/H2 console connected to database but not showing any data from itSpring/H2 控制台连接到数据库但不显示其中的任何数据
【发布时间】:2020-12-07 19:25:24
【问题描述】:

我想将我的数据库从 spring 连接到 h2 控制台,但是当我这样做时,它没有显示任何表格,我尝试了 stackoverflow 和谷歌的所有解决方案,但似乎没有任何帮助 这是我的 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 spring-boot-starter-parent 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]

和分贝:

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

【问题讨论】:

  • 分享你的实体和data.sql
  • 检查您的用户名
  • 在浏览器中用h2控制台查看数据库:localhost:8082
  • 可能数据库URL应该是这样的:jdbc:h2:~/test
  • 尝试更改为 jdbc:h2:~/test 但之后甚至无法连接

标签: java spring database h2


【解决方案1】:

使用 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

【讨论】:

  • 登录 h2-console 时,您必须设置 JDBC URL。在我的示例中:jdbc:h2:file:~/test;DB_CLOSE_ON_EXIT=FALSE
猜你喜欢
  • 1970-01-01
  • 2020-04-12
  • 2023-02-08
  • 2020-08-12
  • 2014-01-13
  • 2018-09-20
  • 2021-12-26
  • 2018-08-23
  • 2016-03-18
相关资源
最近更新 更多