写通用工具类场景1
utils\ResultVOUtil.java
public class ResultVOUtil { public static ResultVO success(Object object) { ResultVO resultVO = new ResultVO(); resultVO.setData(object); resultVO.setCode(0); resultVO.setMsg("成功"); return resultVO; } public static ResultVO success() { return success(null); } public static ResultVO error(Integer code, String msg) { ResultVO resultVO = new ResultVO(); resultVO.setCode(code); resultVO.setMsg(msg); return resultVO; } }
应用: controller\BuyerProductController.java
/** * 买点端商品 */ @RestController @RequestMapping("/buyer/product") public class BuyerProductController { @Autowired ProductService productService; @Autowired CategoryService categoryService; @GetMapping("/list") @Cacheable(cacheNames = "product" , key = "#sellerId" , condition = "#sellerId.length() > 3") public ResultVO<List<ProductVO>> list(@RequestParam("sellerId") String sellerId) { //获取所有上架商品 List<ProductInfo> productInfoList = productService.findUpAll(); //获取所有的类目 List<Integer> categoryTypes = productInfoList.stream() .map(e -> e.getCategoryType()) .collect(Collectors.toList()); List<ProductCategory> productCategoryList = categoryService.findByCategoryTypeIn(categoryTypes); //拼接VO List<ProductVO> productVOList = new ArrayList<>(); for(ProductCategory productCategory : productCategoryList) { ProductVO productVO = new ProductVO(); productVO.setCategoryName(productCategory.getCategoryName()); productVO.setCategoryType(productCategory.getCategoryType()); List<ProductInfoVO> productInfoVOList = new ArrayList<>(); for(ProductInfo productInfo : productInfoList) { if(productCategory.getCategoryType().equals(productInfo.getCategoryType())) { ProductInfoVO productInfoVO = new ProductInfoVO(); BeanUtils.copyProperties(productInfo , productInfoVO); productInfoVOList.add(productInfoVO); } } productVO.setProductInfos(productInfoVOList); productVOList.add(productVO); } return ResultUtils.success(productVOList); } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步