之前写过一篇用stream流实现查询树型结构的文章,现在以hutool中的TreeUtil再来实现一次,之前的帖子Java Stream流实现递归查询树型结构
查询出所有数据,用父节点递归查询出所有子节点数据
/**
* 封装备注分类集合
*
* @param remarkTypeList 备注分类集合
* @return 递归好的集合
*/
@Override
public List<Tree<String>> queryTreeList(List<RemarkType> remarkTypeList) {
//配置
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
treeNodeConfig.setIdKey("remarkTypeId");
treeNodeConfig.setNameKey("typeName");
treeNodeConfig.setParentIdKey("parentId");
// 最大递归深度
treeNodeConfig.setDeep(3);
//转换器
return TreeUtil.build(remarkTypeList, "0", treeNodeConfig,
(treeNode, tree) -> {
tree.setId(treeNode.getRemarkTypeId().toString());
tree.setParentId(treeNode.getParentId().toString());
tree.setName(treeNode.getTypeName());
tree.putExtra("displayStatus",treeNode.getDisplayStatus());
tree.putExtra("sort",treeNode.getSort());
tree.putExtra("contactWay",treeNode.getContactWay());
});
}
查询结果:
{
"msg": "操作成功",
"code": 200,
"data": [
{
"remarkTypeId": "1",
"parentId": "0",
"typeName": "未接通",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "4",
"parentId": "1",
"typeName": "关机",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "5",
"parentId": "1",
"typeName": "空号",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "6",
"parentId": "1",
"typeName": "停机",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "7",
"parentId": "1",
"typeName": "通话中",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "8",
"parentId": "1",
"typeName": "无法接通",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "9",
"parentId": "1",
"typeName": "无人接听",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
}
]
},
{
"remarkTypeId": "2",
"parentId": "0",
"typeName": "已接通",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "10",
"parentId": "2",
"typeName": "开场白挂机",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "22",
"parentId": "10",
"typeName": "用户不说话",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "21",
"parentId": "10",
"typeName": "开场白挂断",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "11",
"parentId": "2",
"typeName": "客户忙",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "23",
"parentId": "11",
"typeName": "在忙没空\r\n",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "12",
"parentId": "2",
"typeName": "需要屏蔽",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "24",
"parentId": "12",
"typeName": "说不要再打了\r\n",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "13",
"parentId": "2",
"typeName": "已充值",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "25",
"parentId": "13",
"typeName": "套餐转移",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "26",
"parentId": "13",
"typeName": "正在使用",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "14",
"parentId": "2",
"typeName": "已退货",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "15",
"parentId": "2",
"typeName": "有充值意向",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "27",
"parentId": "15",
"typeName": "用户承诺近期充值",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "28",
"parentId": "15",
"typeName": "已引导",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "16",
"parentId": "2",
"typeName": "物流正在路上",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "29",
"parentId": "16",
"typeName": "还没拿到已引导\r\n",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "17",
"parentId": "2",
"typeName": "用户不需要",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "30",
"parentId": "17",
"typeName": "骂人",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "31",
"parentId": "17",
"typeName": "准备退货",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "32",
"parentId": "17",
"typeName": "试用不满意",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "33",
"parentId": "17",
"typeName": "价格高",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "34",
"parentId": "17",
"typeName": "说我们骗人",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "35",
"parentId": "17",
"typeName": "在用其他家的",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "36",
"parentId": "17",
"typeName": "设备不在身边",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "37",
"parentId": "17",
"typeName": "不用了不需要",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "18",
"parentId": "2",
"typeName": "用户不知情购买",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "38",
"parentId": "18",
"typeName": "非本人购买,已让转告",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "39",
"parentId": "18",
"typeName": "用户说没购买",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
},
{
"remarkTypeId": "19",
"parentId": "2",
"typeName": "用户犹豫\r\n\r\n",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "40",
"parentId": "19",
"typeName": "赠送免费流量",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "41",
"parentId": "19",
"typeName": "答应减免充值金额",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
}
]
},
{
"remarkTypeId": "3",
"parentId": "0",
"typeName": "其他",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "44",
"parentId": "3",
"typeName": "电商备注",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话"
},
{
"remarkTypeId": "20",
"parentId": "3",
"typeName": "一退",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": [
{
"remarkTypeId": "42",
"parentId": "20",
"typeName": "物流拦截",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
},
{
"remarkTypeId": "43",
"parentId": "20",
"typeName": "中途退货",
"displayStatus": "0",
"sort": 0,
"contactWay": "电话",
"children": null
}
]
}
]
}
]
}
标签:sort,hutool,displayStatus,typeName,contactWay,TreeUtil,parentId,树型,remarkTypeId From: https://www.cnblogs.com/Linzj5950/p/17853324.html根据子节点查询所有父节点数据