Spring JPA 如何确保将数据写入持久存储 [英] Spring JPA how to make sure that data gets written into persistent storage

查看:22
本文介绍了Spring JPA 如何确保将数据写入持久存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展此示例:https://github.com/scratches/jpa-method-security-sample 通过在控制器中添加一个方法来注册",其中一个新用户被动态添加到存储库中.在 import.sql 脚本中添加了默认的现有用户集.但是,我看到我动态添加的用户在服务器重启时被清除了.你能指出我确保表格持续更新的方法吗.

I am trying to extend this example: https://github.com/scratches/jpa-method-security-sample by adding a method in the controller to "signup" where a new user is dynamically added to the repo. The default set of existing users is added in the import.sql script. However, I see that my dynamically added user gets wiped out on server restart. Could you point me out the way to make sure the table gets updated persistently.

这是我的注册方法代码:

Here is my code for the signup method:

@RequestMapping(value = "signup",method =  RequestMethod.GET)
    public String signup(Map<String, Object> model) {
        model.put("message","Sign up!");
        model.put("title", "Hello Signup");
        model.put("date", new Date());
        users.createUser();
        return "signup";
    }

 public void createUser() {
    User u = new User();
    u.setName("xxx");
    u.setPassword("spring");
    repo.save(u);
}

我还对 loadUserByUserName 进行了更改,以便为新用户返回正确的权限.这适用于一个服务器会话,我可以使用新用户登录.但是,在服务器重新启动时,需要重新添加用户.我想防止这种情况发生.

I have also made changes to loadUserByUserName to return the correct authorities for the new user. This works fine for one server session and I can log in with the new user. However, on server restart, the user needs to be re-added. I want to prevent this.

我有一种感觉,我需要做一些更复杂的事情,比如配置一个持久性数据源.如果是这种情况,能否请您指出一些使用 java 配置来执行此操作的资源.我刚从春天开始,这可能是一个基本问题,在我阅读的所有内容中,我不知道接下来该做什么.

I have a feeling that I need to do something more elaborate like configuring a persistent datasource. If that is the case, could you please point me to some resource that uses java config to do this. I am just starting on spring and this may be a basic question and of everything that I read, I am not sure what to follow.

推荐答案

由于您使用的是嵌入式内存 H2 数据库,因此数据不会在重新启动后持续存在.您应该远离嵌入式数据库,并且由于该应用程序基于 spring-boot,您可以轻松地重新配置它并使用例如mysql,通过向 pom 添加 mysql 依赖项.

Since you're using an embedded in-memory H2 database data does not persist across restarts. You should move away from the embedded DB, and since the app is based on spring-boot, you can easily reconfigure this and use e.g. mysql, by adding mysql dependency to the pom.

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

以及 application.properties 中的相应数据库属性,例如

and appropriate DB properties inside application.properties e.g.

spring.datasource.url=jdbc:mysql://localhost/[YOUR DB]
spring.datasource.username=[YOUR USERNAME]
spring.datasource.password=[YOUR PASSWORD]
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

要记住的另一件事是,某些默认值特定于嵌入式 DB,例如 H2 DB.例如,ddl-auto 属性设置为 create-drop.

Another thing to keep in mind, certain defaults are specific for embedded DBs such as the H2 DB. For example, the ddl-auto property is set to create-drop.

这意味着在创建 SessionFactory 时将创建 DB 模式,并在关闭时删除该模式.

This means that the DB schema will be created when the SessionFactory is created and dropped when it is closed.

此属性的值,例如默认情况下,MySql 设置为 none,这意味着您必须首先运行您的应用程序覆盖该属性(在 application.properties 中设置它)

The value of this property for e.g. MySql is set to none by default, this means that you'll have to initially run your application overriding the property (settting it inside application.properties)

spring.jpa.hibernate.ddl-auto: create

这将首先从 import.sql 创建数据.第一次启动后,将属性更改为update,您的数据将在重启后保持不变

this will initially create the data from the import.sql. After the first start, change the property to update, and you'll data will persist across restart

这篇关于Spring JPA 如何确保将数据写入持久存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆