edit/update java spring

edit/update java spring

所以我可以创建一个新用户,但是我 stuck.What 如果我想更新某个用户的电子邮件,我应该怎么做?我正在使用 save.Is 创建用户,有办法更新它吗?

public User update(@RequestBody User user)

`

@ResponseBody
@PostMapping(value = "/api/users/create")
@ResponseStatus(HttpStatus.CREATED)
public User create(@RequestBody User user) {

    User newUser = new User();
    newUser.setUsername(user.getUsername());
    newUser.setEmail(user.getEmail());
    newUser.setPassword(bCryptEncoder.encode(user.getPassword()));

    user_dao.save(newUser);

    return newUser;
}

在您的服务中放置(覆盖)这样的更新用户的方法:

public void updateUser(User user) {
    User entity = userRepository.findById(user.getId()).orElse(null);
    //Check if the intended record exists. If exists, update it..
    if (entity != null) {
        System.out.println("\nUpdating User- " + user.getUsername() + "\n");
        entity.setUsername(user.getUsername());
        entity.setUseremail(user.getUseremail());
        entity.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
    }
    // And finally save it...
    userRepository.save(entity);
}