查找当前分类id的所有父分类id的集合

1、实体类加字段

	/**
	 * 自定义的字段,此注解代表表里不存在
	 */
	@TableField(exist = false)
	private Long[] catagoryPath;

2、controller层

@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;
    
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/info/{attrGroupId}")
    
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		//获取所属分类id
        Long catelogId = attrGroup.getCatelogId();
        Long path[] = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatagoryPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }
}

3、service接口类

public interface CategoryService extends IService<CategoryEntity> {

    /**
     * 根据所属分类id查找出所有它的长辈们
     * 例如当前分类id是255,父亲分类id是 50,爷爷分类id是 20,重爷爷分类id是5,老祖宗是0
     * 父 子 孙 ,返回一个数组列表集合 []
     * @param catelogId
     * @return
     */
    Long[] findCatelogPath(Long catelogId);
}

4、service实现类

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


	@Override
    public Long[] findCatelogPath(Long catelogId) {
        //创建列表用来装当前分类id及所有父分类的id
        ArrayList<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        //将数据子父类的分类id 逆序排列
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }
	
    private List<Long> findParentPath(Long catelogId, ArrayList<Long> paths){
        //收集当前节点分类id
        paths.add(catelogId);
        //根据当前分类id找到具体实体类信息
        CategoryEntity entity = this.getById(catelogId);
        //判断当前分类实体的父类是否是一级分类0
        if (entity.getParentCid() != 0){
            //如果当前分类实体的父亲不是一级分类0,就获取父类的id
           findParentPath(entity.getParentCid(),paths);
        }
        //如果父分类找到是0后,直接返回所有当前分类实体的父分类集合
        return paths;
    }
}

5、测试

	@Autowired
    CategoryService categoryService;

    @Test
    public void test(){
        Long[] paths = categoryService.findCatelogPath(183l);
        for (Long path : paths) {
            System.out.println(path);
        }
    }

返回结果为

2021-06-09 15:02:18.232  INFO 15884 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-06-09 15:02:19.586  INFO 15884 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-06-09 15:02:19.597 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : ==>  Preparing: SELECT cat_id,parent_cid,icon,name,show_status,product_unit,sort,product_count,cat_level FROM pms_category WHERE cat_id=? AND show_status=1 
2021-06-09 15:02:19.629 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : ==> Parameters: 183(Long)
2021-06-09 15:02:19.660 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : <==      Total: 1
2021-06-09 15:02:19.663 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : ==>  Preparing: SELECT cat_id,parent_cid,icon,name,show_status,product_unit,sort,product_count,cat_level FROM pms_category WHERE cat_id=? AND show_status=1 
2021-06-09 15:02:19.663 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : ==> Parameters: 26(Long)
2021-06-09 15:02:19.665 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : <==      Total: 1
2021-06-09 15:02:19.666 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : ==>  Preparing: SELECT cat_id,parent_cid,icon,name,show_status,product_unit,sort,product_count,cat_level FROM pms_category WHERE cat_id=? AND show_status=1 
2021-06-09 15:02:19.667 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : ==> Parameters: 1(Long)
2021-06-09 15:02:19.670 DEBUG 15884 --- [           main] c.l.g.p.dao.CategoryDao.selectById       : <==      Total: 1
1
26
183
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值