Java Spring:H2数据库持久化

Java Spring:H2数据库持久化,java,spring,persistence,h2,Java,Spring,Persistence,H2,My application.properties: spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:h2:./src/main/resources/asnDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.user=sa spring.datasource.password= spring.h2.console.enabl

My application.properties:

spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:./src/main/resources/asnDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.user=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create
我有一个data.sql,在启动spring项目时加载

如何更改application.properties以使数据库持久化

现在,它总是制造一个新的。如果我将
ddl.auto=create
更改为
ddl.auto=update
,它也不起作用。我知道
ddl.auto=create
会覆盖我的数据库,但我不知道如何使其持久化


在data.sql中有3条Insert语句,当我运行项目时,我的数据库中已经有3条Insert语句。然后我通过我的UI插入一个新的并退出项目。当我重新运行项目时,只有最初的3个插入。但是应该有4个插入。

您错过了自动重新连接功能

url=jdbc:h2:file:~/test2;DB_关闭_上的_退出=错误;自动重新连接=真

因此,例如:

spring.datasource.url=jdbc:h2:file:~/test2;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
spring.datasource.username=admin
spring.datasource.password=password
spring.datasource.driver-class-name=org.h2.Driver
#spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

持久性来自属性spring.jpa.hibernate.ddl auto更新而不是创建

,如果退出项目时数据库只包含3个原始插入,那么请检查通过UI进行插入后,行是否已保存且未回滚。因此,我不必这样做更改我的代码中的任何内容?当你停止应用程序而不重新运行它时,有多少行?我没有访问h2数据库的权限,我通过
mvn spring boot:run
启动项目,因此当我停止此操作时,“服务器”关闭。你有答案。问题是您的数据库在内存中。一旦应用程序停止,所有应用程序都将被销毁。一旦应用程序启动,作为启动过程的一部分,将添加3行。当spring.jpa.hibernate.ddl auto=update时,将数据库持久化到文件systemwhen确保没有单独的schema.sql文件来创建表结构。它将删除包含数据的旧表,并在每次启动应用程序时创建一个空表。你们都是非常有知识的人!!表达敬意!