mysql无限极递归查询

sql递归查询出所有类目

select b.* from tbl_taoy_goods_cat b left join tbl_taoy_goods_cat a on b.father_id = a.num_id

java代码实现递归处理处理
参考:https://blog.csdn.net/u010820422/article/details/79239791

    /**
     * <p class="detail">
     * 功能:查询商品类目
     * </p>
     * @author Zerlinda
     * @date 2019年1月18日 
     * @param fatherId 父类ID,传入0时查询所有一级类目;传入非0时,查询二级类目;传入空时,查询所有类目
     * @return
     */
    @RequestMapping(value="/list.app", method={RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object getGoodsCatList(String fatherId){
        
        List<GoodsCat> list = goodsCatService.findByFatherId(fatherId);
        
        if(ExStringUtils.isBlank(fatherId)){
            list = goodsCatService.findAll();
            
            if(null != list && list.size() > 0){
                List<TblTaoyGoodsCat> allGc = new ArrayList<TblTaoyGoodsCat>();
                for(GoodsCat gc : list) { 
                    TblTaoyGoodsCat tgc = (TblTaoyGoodsCat)BeanCloneUtil.copyProperties(TblTaoyGoodsCat.class, gc);
                    allGc.add(tgc);
                }
                List<TblTaoyGoodsCat> tree = getAllGoodCatTree(allGc, "0");
                return ResponseObj.success(MessagesResource.OPERATION_SUCCESS, tree);
            }
            
        }
        
        return ResponseObj.success(MessagesResource.OPERATION_SUCCESS, list);
    }

递归查询下级

    private List<TblTaoyGoodsCat> getAllGoodCatTree(List<TblTaoyGoodsCat> allGc, String parentId){
        List<TblTaoyGoodsCat> parentGoodsCat = new ArrayList<TblTaoyGoodsCat>();
        List<TblTaoyGoodsCat> notParentGoodsCat = new ArrayList<TblTaoyGoodsCat>();
        // 找出所有根节点
        if(null != allGc && allGc.size() > 0){
            for(TblTaoyGoodsCat gc : allGc) { 
                if(parentId.equals(gc.getFatherId())){
                    parentGoodsCat.add(gc);
                }else {
                    notParentGoodsCat.add(gc);
                }
            }
        }
        
        // 递归获取所有子节点
        if(parentGoodsCat.size() > 0){
            for(TblTaoyGoodsCat gc : parentGoodsCat) { 
                gc.setSon(getAllGoodCatTree(notParentGoodsCat, gc.getNumId()+""));
            }
        }
        return parentGoodsCat;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zerlinda_Li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值