我使用 H2 数据库作为测试应用程序,使用 Spring boot。每次,当我重新启动 Spring Boot 应用程序时,H2 中的数据都会被清除。我正在使用文件而不是内存。我spring.jpa.hibernate.ddl-auto=update也在 application.properties 中设置了。这是我的 application.properties 文件
spring.datasource.url=jdbc:h2:file:./data/demo
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么。它可以很好地保存数据。但是一旦我关闭应用程序,所有数据都会被清除。
我发现了我犯的错误。我在资源中有 data.sql 文件,每次 Spring boot 启动应用程序时,它都会运行此脚本。在该脚本中,我删除并重新创建了所有表。一旦我删除了这些 sql 语句,它就完美了。数据永久保存在文件中,服务器重启后不会被删除。
| 归档时间: |
|
| 查看次数: |
13805 次 |
| 最近记录: |