Spring JPA存储库-在服务器重新启动时保留数据 [英] Spring JPA Repository - Keep data on server restart
问题描述
我目前正在尝试学习如何使用Spring Boot,但有一个不确定的问题,我不确定如何解决.
I'm currently trying to learn how to use Spring Boot and have a problem I'm not sure how to solve.
我已按照 http://spring.io/guides/上的指南进行操作gs/accessing-data-jpa/,一切正常.但是,如果我重新启动服务器,则所有保存的数据将完全丢失.有什么方法可以将数据保留在存储库/数据库中,以便如果我关闭应用程序并再次启动它,仍然可以访问所有以前保存的数据?
I've followed the guide at http://spring.io/guides/gs/accessing-data-jpa/ and everything works fine. However, if I restart the server, then all the data that was saved is completely lost. Is there any way to keep the data in the repository/database so that if I shut down the application and start it again, all the previously saved data is still accessible?
预先感谢您:)
推荐答案
所有示例都使用具有内存持久性的嵌入式数据库,这意味着仅在进程运行时才存储数据.只需切换到MySQL之类的常规数据库,或将H2与基于文件的存储url一起使用,该文件也将永久保存在您的磁盘上.对于后者,只需将以下属性添加到application.properties:
All examples use an embedded database with in memory persistence, which means, the data is only stored as long as the process is running. Just switch to a regular database like MySQL or use H2 with a file based storage url, which is also permanently saved on your disk. For the latter, just add the following property to your application.properties:
spring.datasource.url=jdbc:h2:tcp://localhost/${path/to/your/db/file}
,并将${path/to/your/db/file}
替换为您要存储数据库的路径(注意,如果不存在,则会在此处配置的文件夹被创建).
and replace ${path/to/your/db/file}
with the path where you want to store the database (note, the folder you configure here will be created, if it doesn't exist).
这篇关于Spring JPA存储库-在服务器重新启动时保留数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!