本文主要讲hutool工具类打印树的用法。注意,项目需要引入hutool
工具类,pom
依赖:
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.4</version> </dependency>
step1. 首先准备数据(Test.json
):
[ {"orgId": 1,"parentOrgId": 0,"name": "xxx科技公司","desc": "xxx科技公司是一所只有007的company"}, {"orgId": 11,"parentOrgId": 1,"name": "研发部","desc": "研发只有996"}, {"orgId": 111,"parentOrgId": 11,"name": "Java开发部","desc": "描述....."}, {"orgId": 1111,"parentOrgId": 111,"name": "Java开发1组","desc": "描述....."}, {"orgId": 1112,"parentOrgId": 111,"name": "Java开发2组","desc": "描述....."}, {"orgId": 112,"parentOrgId": 11,"name": "Python开发部","desc": "描述....."}, {"orgId": 113,"parentOrgId": 11,"name": "C++开发部","desc": "描述....."}, {"orgId": 12,"parentOrgId": 1,"name": "财务部","desc": "描述....."}, {"orgId": 13,"parentOrgId": 1,"name": "商务部","desc": "描述....."}, {"orgId": 14,"parentOrgId": 1,"name": "销售部","desc": "描述....."}, {"orgId": 141,"parentOrgId": 14,"name": "华南区","desc": "描述....."}, {"orgId": 142,"parentOrgId": 14,"name": "华北区","desc": "描述....."}, {"orgId": 143,"parentOrgId": 14,"name": "华东区","desc": "描述....."} ]
step2. 单元测试代码:
public void testTree(){ // 读取json文件内容,转换为entity String resultJson = FileUtil.readString(new ClassPathResource("Test.json").getFile(), "UTF-8"); List<Org> orgs = JSONUtil.toList(resultJson, Org.class); List<TreeNode<Long>> treeNodeList = new ArrayList<>(); // 设置树节点 orgs.forEach(org -> { TreeNode<Long> node = new TreeNode<>(); node.setId(org.getOrgId()); node.setParentId(org.getParentOrgId()); node.setName(org.getName()); Map<String, Object> map = new HashMap<>(); map.put("desc", org.getDesc()); node.setExtra(map); treeNodeList.add(node); }); // 设置树节点字段别名 TreeNodeConfig treeNodeConfig = new TreeNodeConfig(); treeNodeConfig.setIdKey("basicId"); treeNodeConfig.setParentIdKey("parentBasicId"); treeNodeConfig.setNameKey("lable"); // 开始构建树 Tree<Long> tree = TreeUtil.buildSingle(treeNodeList, 0L, treeNodeConfig, new DefaultNodeParser<>()); tree.setName("机构树示例"); // 格式化打印树 System.out.println(tree); // 打印json树 String jsonStr = JSONUtil.toJsonStr(tree); JSONObject jsonObject = new JSONObject(jsonStr); Object children = jsonObject.get("children"); System.out.println(JSONUtil.toJsonPrettyStr(children.toString())); }
机构树打印运行效果:
返回json格式:
[ { "basicId": 1, "weight": 0, "parentBasicId": 0, "children": [ { "basicId": 11, "weight": 0, "parentBasicId": 1, "children": [ { "basicId": 111, "weight": 0, "parentBasicId": 11, "children": [ { "basicId": 1111, "weight": 0, "parentBasicId": 111, "lable": "Java开发1组", "desc": "描述....." }, { "basicId": 1112, "weight": 0, "parentBasicId": 111, "lable": "Java开发2组", "desc": "描述....." } ], "lable": "Java开发部", "desc": "描述....." }, { "basicId": 112, "weight": 0, "parentBasicId": 11, "lable": "Python开发部", "desc": "描述....." }, { "basicId": 113, "weight": 0, "parentBasicId": 11, "lable": "C++开发部", "desc": "描述....." } ], "lable": "研发部", "desc": "研发只有996" }, { "basicId": 12, "weight": 0, "parentBasicId": 1, "lable": "财务部", "desc": "描述....." }, { "basicId": 13, "weight": 0, "parentBasicId": 1, "lable": "商务部", "desc": "描述....." }, { "basicId": 14, "weight": 0, "parentBasicId": 1, "children": [ { "basicId": 141, "weight": 0, "parentBasicId": 14, "lable": "华南区", "desc": "描述....." }, { "basicId": 142, "weight": 0, "parentBasicId": 14, "lable": "华北区", "desc": "描述....." }, { "basicId": 143, "weight": 0, "parentBasicId": 14, "lable": "华东区", "desc": "描述....." } ], "lable": "销售部", "desc": "描述....." } ], "lable": "xxx科技公司", "desc": "xxx科技公司是一所只有007的company" } ]