这是我的控制器,它将表单数据保存在数据库中:
select t.tag_id,t.name ,
(case when (select tag_id from tags where tag_id=tti.item_id)
THEN
tti.item_id
ELSE
NULL
END) as item_id
from tags as t
left join tag_to_item as tti ON tti.tag_id = t.tag_id;
调用saveUser后,如何在该控制器中获取该用户的生成ID,以便我可以用它来调用另一个函数? 谢谢
编辑:
UserService实施:
@RequestMapping(value = "/user/newUser", method = RequestMethod.POST)
public ModelAndView createNewSurvey(@Valid User user, BindingResult bindingResult) {
ModelAndView modelAndView = new ModelAndView();
userService.saveUser(user);
modelAndView.addObject("successMessage", "Added user!");
modelAndView.addObject("user", new User());
modelAndView.setViewName("user/newUser");
return modelAndView;
}
答案 0 :(得分:1)
尝试以下内容,让我知道它是否适合您
@Override
public User saveUser(User user) {
return userRepository.save(user);
}
还更改接口方法以在UserService接口中返回用户
public User saveUser(User user);
然后在控制器中调用返回的UserObject
中的getId()User newUser = userService.saveUser(user);
newUser.getId();