@RequestMapping("/addUser") public ReturnCodeUtil addUser(@RequestBody User user) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); if(null == user.getUserFlag()){ returnCodeUtil.setResult(false); return returnCodeUtil; } int res = this.userService.addUser(user); if (res>0) { returnCodeUtil.setResult(true); } else { returnCodeUtil.setResult(false); } return returnCodeUtil; } @RequestMapping("/register") public ReturnCodeUtil register(@RequestBody User user) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); if(user == null) { returnCodeUtil.setResult(false); return returnCodeUtil; } int res = this.userService.addUser(user); if(res>0){ returnCodeUtil.setResult(true); }else returnCodeUtil.setResult(false); return returnCodeUtil; }优化这段代码
时间: 2024-03-11 16:49:27 浏览: 55
可以将这两个方法合并成一个,避免重复代码的出现,同时可以将判断是否添加用户成功的逻辑提取出来,避免重复出现,优化后的代码如下:
```java
@RequestMapping("/addUser")
public ReturnCodeUtil addUser(@RequestBody User user) {
ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil();
if (user == null || user.getUserFlag() == null) {
returnCodeUtil.setResult(false);
return returnCodeUtil;
}
int res = this.userService.addUser(user);
returnCodeUtil.setResult(res > 0);
return returnCodeUtil;
}
```
相关问题
@Autowired private UserService userService; @RequestMapping("/addUser") public ReturnCodeUtil addUser(@RequestBody User user) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); if(null == user.getUserFlag()){ returnCodeUtil.setResult(false); return returnCodeUtil; } int res = this.userService.addUser(user); if (res>0) { returnCodeUtil.setResult(true); } else { returnCodeUtil.setResult(false); } return returnCodeUtil; } @RequestMapping("/register") public ReturnCodeUtil register(@RequestBody User user) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); if(user == null) { returnCodeUtil.setResult(false); return returnCodeUtil; } int res = this.userService.addUser(user); if(res>0){ returnCodeUtil.setResult(true); }else returnCodeUtil.setResult(false); return returnCodeUtil; }
可以将这两个方法中的重复代码提取出来,避免重复的代码出现。重构后的代码如下所示:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/add")
public ResultVO addUser(@Validated @RequestBody User user) {
return addUserOrRegister(user);
}
@PostMapping("/register")
public ResultVO register(@Validated @RequestBody User user) {
return addUserOrRegister(user);
}
private ResultVO addUserOrRegister(User user) {
if (user == null || user.getUserFlag() == null) {
return ResultVO.fail("参数错误");
}
userService.addUser(user);
return ResultVO.success();
}
}
```
通过提取公共方法,代码变得更加简洁易读,避免了重复代码的出现,提高了代码的可维护性和可扩展性。同时,使用统一的ResultVO返回格式,避免了使用ReturnCodeUtil。
@ResponseBody @RequestMapping("/user/add") public Map<String, Object> addUser(@RequestBody User user) { ResponseBean responseBean = new ResponseBean(); User u = userService.selectOne(user); // 查询是否存在用户名 if (u == null) { userService.insertSelective(user); // 不存在则插入 } else { responseBean.putError("该用户名已存在"); } return responseBean.getResponseMap(); }
这段代码是一个Spring MVC的控制器方法,用于处理 "/user/add" 的POST请求,接收JSON格式的请求体,并将请求体反序列化为User对象。该方法首先通过调用userService的selectOne方法查询是否已存在相同用户名的用户,如果不存在,则通过调用userService的insertSelective方法将该用户插入到数据库中。如果存在相同用户名的用户,则返回一个包含错误信息的ResponseBean对象,最后将ResponseBean对象的响应Map返回给客户端。
阅读全文