根据某一节点,查询该节点下所有子节点的数据,组成json形式的字符串。

根据某一节点,查询该节点下所有子节点的数据,组成json形式的字符串。
通过递归查询的方式实现,结合具体需求我们来看看如何实现。
实体类:
zTreeEntity (对应数据库)

public class ZTreeEntity {
    @JsonIgnore
    private String id = null;
    private String name = null;
    private String identification = null;
    private String fathernode = null;

TbpzEntity

public class TbpzEntity {
    private ZTreeEntity value = new ZTreeEntity();
    private Map child = new LinkedHashMap();

    public TbpzEntity(){

    }
    public TbpzEntity(ZTreeEntity value) {
        this.value = value;
    }

    public ZTreeEntity getValue() {
        return value;
    }

    public void setValue(ZTreeEntity value) {
        this.value = value;
    }
    public Map getChild() {
        return child;
    }

    public void setChild(Map child) {
        this.child = child;
    }
    //获取子节点包含的数据
    public ZTreeEntity getChildValue(String bs){
        if (getChildPzx(bs)!=null){
            return getChildPzx(bs).getValue();
        }
        return null;
    }
    //获取对应的子节点下的全部数据
    public TbpzEntity getChildPzx(String bs){
        return (TbpzEntity) child.get(bs);
    }
}

工具类
PbpzUtils

 public static TbpzEntity getDgTbpz(List<ZTreeEntity> list, TbpzEntity father){
        ZTreeEntity f_zTreeEntity  = father.getValue();
        for (ZTreeEntity zTreeEntity:list){
            if (!zTreeEntity.isEmptyFathernode() && f_zTreeEntity.getId().equals(zTreeEntity.getFathernode())){
                TbpzEntity tbpzEntity = new TbpzEntity();
                tbpzEntity.setValue(zTreeEntity);
                father.getChild().put(zTreeEntity.getIdentification(),tbpzEntity);
                getDgTbpz(list,tbpzEntity);
            }
        }
        return father;
    }

测试

PbpzUtils.getDgTbpz(list,new TbpzEntity(ztree));
//list为查询到List<ZTreeEntity>数据,这其中包含了父节点和子节点数据
//ztree为父节点数据

效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值