根据当前的类别查询父级的类别
由于业务的需要我们有时点开某个分类下的详情时,需要显示它的完整路径这个时候,递归就应用起来啦
- 具体代码如下
@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; }
- 返回结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)