【发布时间】:2015-08-06 17:16:24
【问题描述】:
我从这篇文章中了解到https://spring.io/guides/gs/accessing-data-jpa/。而且我有一个问题,因为每次关闭应用程序并再次打开它,数据库中的数据都会丢失,必须重新输入。数据如何永久保存?
【问题讨论】:
标签: spring
我从这篇文章中了解到https://spring.io/guides/gs/accessing-data-jpa/。而且我有一个问题,因为每次关闭应用程序并再次打开它,数据库中的数据都会丢失,必须重新输入。数据如何永久保存?
【问题讨论】:
标签: spring
数据丢失,因为本教程使用内存数据库。对于要持久化的数据,您需要使用 MySQL 或 Postgres 等数据库。以下步骤将更改教程以使用 MySQL:
用 MySQL 替换对 H2 的依赖。即,如果您使用的是 maven,请在 pom.xml 中替换
<dependency>
<groupId>com.m2database</groupId>
<artifactId>h2</artifactId>
</dependency>
与
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
将 MySQL 连接信息放入src/main/resources/application.properties,如下所示:
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: update
spring.datasource.url: jdbc:mysql://localhost:3306/db-name
spring.datasource.username: your db-username e.g. root
spring.datasource.password: db-password
如果您还没有安装 MySQL,然后创建数据库。
【讨论】: