@RequestMapping("/delete") public ReturnCodeUtil delete(@RequestBody CommonUse commonUse) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); int res = commonUseService.delete(commonUse); if(res >0){ returnCodeUtil.setResult(true); }else { returnCodeUtil.setResult(false); } return returnCodeUtil; }
时间: 2023-06-27 08:08:29 浏览: 210
这是一个使用了Spring MVC框架的Java后端代码中的一个RequestMapping方法,用于处理HTTP POST请求,请求路径为"/delete"。该方法接收一个CommonUse类型的JSON格式数据作为请求体,并调用commonUseService中的delete方法进行删除操作,将删除结果通过ReturnCodeUtil类型的JSON格式数据返回给前端。如果删除成功,返回结果中的result属性为true,否则为false。
相关问题
@RestController @RequestMapping("/restful/commonUse") public class CommonUseController { @Autowired private UserService userService; @Autowired private CommonUseService commonUseService; //统一的数据查看 @RequestMapping("/view") public ReturnCodeUtil view(@RequestBody CommonUse commonUse) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); if(String.valueOf(commonUse.getUserId()).equals("0") || "".equals(commonUse.getUserFlag()) || commonUse.getUserFlag() == null ){ returnCodeUtil.setResult(false); return returnCodeUtil; } List<CommonUse> view = commonUseService.view(commonUse); returnCodeUtil.setResult(true); returnCodeUtil.setData(view); return returnCodeUtil; } //统一的数据更改 @RequestMapping("/edit") public ReturnCodeUtil edit(@RequestBody CommonUse commonUse) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); if(String.valueOf(commonUse.getUserId()).equals("0") || "".equals(commonUse.getUserFlag()) || commonUse.getUserFlag() == null ){ returnCodeUtil.setResult(false); return returnCodeUtil; } int res = commonUseService.edit(commonUse); if(res>0) { returnCodeUtil.setResult(true); }else returnCodeUtil.setResult(false); return returnCodeUtil; } //统一的数据插入 @RequestMapping("/add") public ReturnCodeUtil add(@RequestBody CommonUse commonUse) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); int add = commonUseService.add(commonUse); if(add >0){ returnCodeUtil.setResult(true); }else { returnCodeUtil.setResult(false); } return returnCodeUtil; } //统一数据删除 @RequestMapping("/delete") public ReturnCodeUtil delete(@RequestBody CommonUse commonUse) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); int res = commonUseService.delete(commonUse); if(res >0){ returnCodeUtil.setResult(true); }else { returnCodeUtil.setResult(false); } return returnCodeUtil; } }优化代码
可以将重复的部分提取出来,采用函数式编程的方式进行重构,提高代码的复用性和可读性,优化后的代码如下:
@RestController
@RequestMapping("/restful/commonUse")
public class CommonUseController {
@Autowired
private UserService userService;
@Autowired
private CommonUseService commonUseService;
// 统一的数据操作
private ReturnCodeUtil operateData(Function<CommonUse, Integer> function, CommonUse commonUse) {
ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil();
if (String.valueOf(commonUse.getUserId()).equals("0") || "".equals(commonUse.getUserFlag()) || commonUse.getUserFlag() == null) {
returnCodeUtil.setResult(false);
return returnCodeUtil;
}
int res = function.apply(commonUse);
returnCodeUtil.setResult(res > 0);
return returnCodeUtil;
}
// 统一的数据查看
@RequestMapping("/view")
public ReturnCodeUtil view(@RequestBody CommonUse commonUse) {
List<CommonUse> view = commonUseService.view(commonUse);
ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil();
returnCodeUtil.setResult(true);
returnCodeUtil.setData(view);
return returnCodeUtil;
}
// 统一的数据更改
@RequestMapping("/edit")
public ReturnCodeUtil edit(@RequestBody CommonUse commonUse) {
return operateData(commonUseService::edit, commonUse);
}
// 统一的数据插入
@RequestMapping("/add")
public ReturnCodeUtil add(@RequestBody CommonUse commonUse) {
return operateData(commonUseService::add, commonUse);
}
// 统一数据删除
@RequestMapping("/delete")
public ReturnCodeUtil delete(@RequestBody CommonUse commonUse) {
return operateData(commonUseService::delete, commonUse);
}
}
这里提取了 operateData 函数,用于统一处理数据操作,接收一个 Function 类型的参数,该参数用于指定具体的数据操作方法。同时,view 方法中的代码也进行了简化,使代码更加简洁。
优化代码 //统一数据删除 @RequestMapping("/delete") public ReturnCodeUtil delete(@RequestBody CommonUse commonUse) { ReturnCodeUtil returnCodeUtil = new ReturnCodeUtil(); int res = commonUseService.delete(commonUse); if(res >0){ returnCodeUtil.setResult(true); }else { returnCodeUtil.setResult(false); } return returnCodeUtil; } }
可以将代码进行简化和优化,如下所示:
@RequestMapping("/delete")
public ReturnCodeUtil delete(@RequestBody CommonUse commonUse) {
int res = commonUseService.delete(commonUse);
return new ReturnCodeUtil(res > 0);
}
这里使用了三元运算符来判断 res 是否大于 0,从而设置 ReturnCodeUtil 的结果。同时,去掉了无用的变量声明和赋值操作,使代码更简洁。
阅读全文