有没有一种简单的方法可以在 Spring/JPA 中查看原始数据库内容?

时间:2021-02-05 12:14:04

标签: java spring database jpa

是否可以在 Spring 应用程序中查看实际的数据库表内容?这可以是打印到 System.out 的典型 SQL 输出:

enter image description here

或任何其他类型的交互式表格视图。

这将在使用内存数据库的 spring 单元测试的上下文中使用 - 因此没有可以使用的单独的 SQL 类型提示(除非有一种简单的方法将它连接到内存数据库).

最好不要为每个实体类/存储库编写不同的代码。主要目的是能够在测试应用程序时快速查看/调试添加到数据库表中的内容(接下来编写实际测试用例)。

2 个答案:

答案 0 :(得分:0)

另一种方法是使用 Hibernate Tools 控制台,可在 https://hibernate.org/tools/

答案 1 :(得分:0)

使用H2内存数据库时,可以使用H2控制台访问数据库。

您将需要以下属性:

  • import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootApplicationClass.class) //annotation when you need Spring Boot features (此属性的值需要在 H2 控制台登录屏幕中作为 URL 填写)
  • spring.datasource.url=jdbc:h2:mem:testdb

并且在进行单元测试时,需要将 spring.h2.console.enabled=true 添加到您的 webEnvironment=WebEnvironment.DEFINED_PORT 注释中。