以树形结构获取所有商品分类
1、Controller
@ApiOperation("以树形结构获取所有商品分类")
@RequestMapping(value = "/categoryTreeList", method = RequestMethod.GET)
@ResponseBody
public CommonResult> categoryTreeList() {
List list = portalProductService.categoryTreeList();
return CommonResult.success(list);
}
2、Service
List categoryTreeList();
@Override
public List categoryTreeList() {
PmsProductCategoryExample example = new PmsProductCategoryExample();
List allList = productCategoryMapper.selectByExample(example);
List result = allList.stream()
.filter(item -> item.getParentId().equals(0L))
.map(item -> covert(item, allList)).collect(Collectors.toList());
return result;
}
private PmsProductCategoryNode covert(PmsProductCategory item, List allList) {
PmsProductCategoryNode node = new PmsProductCategoryNode();
BeanUtils.copyProperties(item, node);
List children = allList.stream()
.filter(subItem -> subItem.getParentId().equals(item.getId()))
.map(subItem -> covert(subItem, allList)).collect(Collectors.toList());
node.setChildren(children);
return node;
}
3、Mapper
List selectByExample(PmsProductCategoryExample example);