多表关联大平层转JSON树形结构
比如把这种平层数据转化为下面这种树形结构树[{id:2,parentId:null,name:有声书,type:category,children:[{id:1,parentId:2,name:有声书分类,type:attribute,children:[{id:1,name:男频小说,type:value},{id:2,name:女频小说,type:value}]}]}]方法1非递归建树首先创建一个实体类,作为返回给前端的json结构DataclassTreeNode{privateLongid;privateLongparentId;privateStringname;privateListTreeNodechildren;}核心方法/** * 核心三层平层数据 转 树形结构 * param flatList 平层数据分类属性属性值 * return 树形数据 */publicListTreeNodebuildTree(ListFlatDataflatList){// 1. 核心容器key节点IDvalue节点自动去重MapLong,TreeNodenodeMapnewHashMap();// 2. 遍历平层数据封装三层节点for(FlatDatadata:flatList){// 分类第一层buildNode(nodeMap,data.getCategoryId(),null,data.getCategoryName());// 属性第二层父分类IDbuildNode(nodeMap,data.getAttributeId(),data.getCategoryId(),data.getAttributeName());// 属性值第三层父属性IDbuildNode(nodeMap,data.getValueId(),data.getAttributeId(),data.getValueName());}// 3. 核心挂载子节点 → 父节点 childrenfor(TreeNodenode:nodeMap.values()){LongparentIdnode.getParentId();if(parentId!nullnodeMap.containsKey(parentId)){nodeMap.get(parentId).getChildren().add(node);}}// 4. 返回根节点parentIdnullreturnnodeMap.values().stream().filter(node-node.getParentId()null).toList();}// 通用节点构建去重privatevoidbuildNode(MapLong,TreeNodemap,Longid,LongparentId,Stringname){if(idnull||map.containsKey(id))return;TreeNodenodenewTreeNode();node.setId(id);node.setParentId(parentId);node.setName(name);node.setChildren(newArrayList());map.put(id,node);}2.分组嵌套很简单的一种写法就是层级需要固定// 1. 查询平层宽表数据ListFlatAttributeDTOflatListattributeMapper.selectFlatData(category1Id);ListJSONObjectresultnewArrayList();// 第一层按 一级分类ID 分组MapLong,ListFlatAttributeDTOgroupByCategoryflatList.stream().collect(Collectors.groupingBy(FlatAttributeDTO::getCategory1Id));// 组装一级分类节点groupByCategory.forEach((categoryId,categoryList)-{JSONObjectcategoryNodenewJSONObject();categoryNode.put(id,categoryId);categoryNode.put(name,categoryList.get(0).getCategory1Name());ListJSONObjectattributeListnewArrayList();// 第二层组内按 属性ID 分组MapLong,ListFlatAttributeDTOgroupByAttributecategoryList.stream().collect(Collectors.groupingBy(FlatAttributeDTO::getAttributeId));// 组装属性节点groupByAttribute.forEach((attributeId,attributeListData)-{JSONObjectattributeNodenewJSONObject();attributeNode.put(id,attributeId);attributeNode.put(name,attributeListData.get(0).getAttributeName());ListJSONObjectvalueListnewArrayList();// 第三层遍历属性值叶子节点for(FlatAttributeDTOdto:attributeListData){JSONObjectvalueNodenewJSONObject();valueNode.put(id,dto.getValueId());valueNode.put(name,dto.getValueName());valueList.add(valueNode);}attributeNode.put(children,valueList);attributeList.add(attributeNode);});categoryNode.put(children,attributeList);result.add(categoryNode);});3.使用MybatisPlus的ResultMap映射实体类这里的实体类异地昂要是这种里面有list这种// 一级分类根节点publicclassCategoryTree{privateLongid;// category1_idprivateStringname;// category1_nameprivateListCategoryLevel2children;// getter/setter 略}// 二级分类publicclassCategoryLevel2{privateLongid;// category2_idprivateStringname;// category2_nameprivateListCategoryLevel3children;// getter/setter 略}// 三级分类叶子节点publicclassCategoryLevel3{privateLongid;// category3_idprivateStringname;// category3_name// getter/setter 略}mapper文件mapper中最外层就是我们的最外的是体力里面的collection可以嵌套对应实体类里面的list说白了还是一一对应的关系resultMapidcategoryTreeMaptypecom.xxx.dto.CategoryTreeautoMappingfalse!-- 一级分类 --idcolumnc1_idpropertyid/resultcolumnc1_namepropertyname/!-- 二级分类集合 --collectionpropertychildrenofTypecom.xxx.dto.CategoryLevel2idcolumnc2_idpropertyid/resultcolumnc2_namepropertyname/!-- 三级分类集合 --collectionpropertychildrenofTypecom.xxx.dto.CategoryLevel3idcolumnc3_idpropertyid/resultcolumnc3_namepropertyname//collection/collection/resultMapselectidselectAttributeTreeresultMapattributeTreeMapSELECT bc.category1_id AS c1_id, bc.category1_name AS c1_name, ba.attribute_id AS a_id, ba.attribute_name AS a_name, bav.value_id AS v_id, bav.value_name AS v_name FROM base_category bc INNER JOIN base_attribute ba ON bc.category1_id ba.category1_id INNER JOIN base_attribute_value bav ON ba.attribute_id bav.attribute_id WHERE bc.category1_id #{category1Id}/select
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2564296.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!