根据当前的类别查询父级的类别
由于业务的需要我们有时点开某个分类下的详情时,需要显示它的完整路径这个时候,递归就应用起来啦
- 具体代码如下
@RequestMapping("/info/{attrGroupId}")
//@RequiresPermissions("product:attrgroup:info")
public R info(@PathVariable("attrGroupId") Long attrGroupId){
AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
//找到当前的id
Long catelogId = attrGroup.getCatelogId();
//查找当前id的父级的完整路径
Long[] path=categoryService.findCatelogPath(catelogId);
attrGroup.setCatelogPath(path);
return R.ok().put("attrGroup", attrGroup);
}
/** * 返回层级关系 * [父/子/孙] */ @Override public Long[] findCatelogPath(Long catelogId) { ArrayList<Long> paths = new ArrayList<>(); List<Long> list = findPaths(catelogId, paths); //调整返回的顺序 Collections.reverse(list); return list.toArray(new Long[list.size()]); } /** *返回父级的完整路径[孙/子/父] */ private List<Long> findPaths(Long catelogId, List<Long> paths){ //先保存当前的节点id paths.add(catelogId); //查询当前目录的详情 CategoryEntity categoryEntity = baseMapper.selectById(catelogId); //如果当前id还有父级 if (categoryEntity.getParentCid()!=0){ //继续递归执行 findPaths(categoryEntity.getParentCid(), paths); } return paths; }
- 返回结果


浙公网安备 33010602011771号