【问题标题】:Spring how to use non embbed databaseSpring如何使用非嵌入式数据库
【发布时间】:2015-08-06 17:16:24
【问题描述】:

我从这篇文章中了解到https://spring.io/guides/gs/accessing-data-jpa/。而且我有一个问题,因为每次关闭应用程序并再次打开它,数据库中的数据都会丢失,必须重新输入。数据如何永久保存?

【问题讨论】:

标签: spring


【解决方案1】:

数据丢失,因为本教程使用内存数据库。对于要持久化的数据,您需要使用 MySQL 或 Postgres 等数据库。以下步骤将更改教程以使用 MySQL:

  1. 用 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>
    
  2. 将 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
    
  3. 如果您还没有安装 MySQL,然后创建数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2013-02-03
    • 2011-03-07
    • 2012-10-22
    • 2018-06-13
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多