gpt4 book ai didi

java - 编辑/更新java spring

转载 作者:行者123 更新时间:2023-12-01 19:33:50 25 4
gpt4 key购买 nike

所以我可以创建一个新用户,但我卡住了。如果我想更新某个用户的电子邮件,我该怎么做?我使用保存创建用户。有没有办法像这样更新它?

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);
}

关于java - 编辑/更新java spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com