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

查看:118
本文介绍了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 config进行此操作的资源.我只是从春天开始,这可能是一个基本问题,对于我所阅读的所有内容,我不确定该怎么做.

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,方法是将mysql依赖项添加到pom中.

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).例如,

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时创建数据库模式,并在关闭它时将其删除.

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天全站免登陆