根据某一节点,查询该节点下所有子节点的数据,组成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为父节点数据
效果: