controller
/
*** 设置陌生人问题
** @return
*/
@PostMapping("questions")
public ResponseEntity<Void> saveQuestions(@RequestBody Map<String,保存 String> param) {
try {
String content = param.get("content");
this.myCenterService.saveQuestions(content);
return ResponseEntity.ok(null);
} catch (Exception e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
=========================================================
service 获取controller传递请求线程 获取其中用户ID
public void saveQuestions(String content) {
User user = UserThreadLocal.get();
this.questionService.save(user.getId(), content);
}
==================================================
service 核心逻辑处理 问题service
public void save(Long userId, String content) {
Question question = this.queryQuestion(userId);
if(null != question){
question.setTxt(content);
this.questionMapper.updateById(question);
}else {
question = new Question();
question.setUserId(userId);
question.setTxt(content);
question.setCreated(new Date());
question.setUpdated(question.getCreated());
this.questionMapper.insert(question);
}
}
(作者:新闻中心)