简体   繁体   English

如何仅更新已更改的属性 - Spring MVC

[英]How to update only attributes that have changed - Spring MVC

My controller 我的控制器

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public String updateUserById(@PathVariable("id") Long id, Model model) {
    User user = userRepository.findOne(id);
    model.addAttribute(user);
    return "admin/editUser";
}

@RequestMapping(value = "/user/{id}", method = RequestMethod.POST)
@ResponseBody
public String updateUserById(@PathVariable("id") Long id, @ModelAttribute User user) {
    userRepository.updateUser(id, user); // with a try catch
}

The dao

@Override
public void updateUser(Long id, User user) {
    User userDB = userRepository.findOne(id);
    userDB.setFirstName(user.getFirstName());
    userDB.setLastName(user.getLastName());
    userDB.setEmail(user.getEmail());
    userDB.setUsername(user.getUsername());
    userRepository.save(userDB);
}

This method works but it's pretty ugly for me. 这种方法有效但对我来说非常难看。 Let's say that the user have just changed the firstname field in the view, how can I adapt my code to only call the function to set the firstname ? 假设用户刚刚更改了视图中的firstname字段,如何调整我的代码只调用函数来设置firstname?

Something like the Observer pattern to notify field that have change ? 像Observer模式通知字段有变化?

if you are using hibernate then there is one attribute in hibernate dynamicUpdate = true which will update only updated fields in db 如果您正在使用hibernate,那么hibernate dynamicUpdate = true有一个属性,它只更新db中的更新字段

like below code 像下面的代码

@Entity
@Table(name = "User")
@org.hibernate.annotations.Entity(
        dynamicUpdate = true
)
public class User 

for hibenrate 4 + use these 对于hibenrate 4 +使用这些

@DynamicInsert(true)
@DynamicUpdate(true)

I suppose that your code works like this: 我想你的代码是这样的:

An user want to change some data of his user entity . 用户想要更改其user entity某些数据。 Means in frontend you already show him all his entries from the user table of the DB like firstname, lastname and so on. 在前端的方法你已经从数据库的用户表中显示了他的所有条目,如firstname,lastname等。

For example the user changed his firstname and hit the save button. 例如,用户更改了他的名字并点击了保存按钮。 Your controller receive the user entity inclusive the user.id, the new firstname and all the old set values . 您的控制器将接收包含user.id,新名字和所有旧设置值的用户实体。 If your code works like this you can easily save() the user entity. 如果您的代码像这样工作,您可以轻松save()用户实体。 There is no need to fetch the user from the DB first. 无需先从数据库中获取用户。

So just do it like this: 所以就这样做:

@Override
public void updateUser(User user) {
    userRepository.save(user);
}

The Repository knows the Id of the user entity and just update the complete entity. Repository知道用户实体的Id,只是更新完整的实体。

And for the reason you dont have the Id in your user entity use the Id from your controller like this: 由于您在用户实体中没有Id的原因,请使用控制器中的Id,如下所示:

@Override
public void updateUser(User user, Long Id) {
    user.setId(Id);
    userRepository.save(user);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM