赞
踩
<!--hutool工具--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.8</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> <scope>compile</scope> </dependency>
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TreeDemo {
private String id;
private String columnName;
private String parentId;
private Double sortNum;
}
package com.xxxx.studyseckill.tree; import cn.hutool.core.lang.tree.Tree; import cn.hutool.core.lang.tree.TreeNode; import cn.hutool.core.lang.tree.TreeNodeConfig; import cn.hutool.core.lang.tree.TreeUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.toolkit.CollectionUtils; import com.xxxx.studyseckill.mapper.TreeDemoMapper; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections.ComparatorUtils; import org.apache.commons.collections.comparators.ComparableComparator; import org.thymeleaf.util.StringUtils; import javax.annotation.Resource; import java.util.*; import java.util.stream.Collectors; public class TestTree { @Resource private TreeDemoMapper treeDemoMapper; public List<Tree<String>> getTree(String parentId){ List<Tree<String>> resultTree = new ArrayList<>(); if(StringUtils.isEmpty(parentId)){ parentId = "root"; } List<TreeDemo> demoTreeList = this.getDemoByParentId(parentId,new ArrayList<>()); //转换成树结点 List<TreeNode<String>> treeNodeList = demoTreeList.stream().map(treeDemo -> { Map<String,Object> map = new HashMap<>(); map.put("columnName",treeDemo.getColumnName()); map.put("id",treeDemo.getId()); map.put("sortNum",treeDemo.getSortNum()); TreeNode<String> treeNode = new TreeNode<String>().setId(treeDemo.getId()).setParentId(treeDemo.getParentId()).setExtra(map); return treeNode; }).collect(Collectors.toList()); //转换树形列表中字段名称 TreeNodeConfig config = new TreeNodeConfig().setChildrenKey("children").setIdKey("id").setParentIdKey("parentId"); //转换树结点到树形列表 String rootTreeNode = "root"; if(CollectionUtils.isNotEmpty(demoTreeList)){ rootTreeNode = demoTreeList.get(0).getParentId(); } List<Tree<String>> treeList = TreeUtil.build(treeNodeList,rootTreeNode,config,(node,tree)->{ tree.setId(node.getId()).setParentId(node.getParentId()); tree.putExtra("columnName",node.getExtra().get("columnName")); tree.putExtra("id",node.getExtra().get("id")); tree.putExtra("sortNum",node.getExtra().get("sortNum")); }); //排序 treeListSort(treeList); return treeList; } /** * 获取当前结点和子节点的值 * @param parentId * @return */ private List<TreeDemo> getDemoByParentId(String parentId,List<TreeDemo> treeDemoList) { //获取当前结点信息 TreeDemo currentTreeDemo = treeDemoMapper.selectOne(new LambdaQueryWrapper<TreeDemo>().eq(TreeDemo::getId, parentId).last("limit 1")); if(Objects.nonNull(currentTreeDemo)){ treeDemoList.add(currentTreeDemo); } //获取子节点信息 List<TreeDemo> demoTreeList = this.treeDemoMapper.getTreeDemoByParentId(parentId); treeDemoList.addAll(demoTreeList); if(CollectionUtils.isNotEmpty(demoTreeList)){ for (TreeDemo treeDemo : demoTreeList) { String id = treeDemo.getId(); getDemoByParentId(id,treeDemoList); } } else { return demoTreeList; } //去除重复值 LinkedHashSet<TreeDemo> hashSet = new LinkedHashSet<>(demoTreeList); ArrayList<TreeDemo> listWithoutDuplicates = new ArrayList<>(hashSet); return listWithoutDuplicates; } /** * 对树集合升序排列 * @param treeList */ private void treeListSort(List<Tree<String>> treeList) { Comparator<?> instance = ComparableComparator.getInstance(); instance = ComparatorUtils.nullLowComparator(instance); treeList.sort(new BeanComparator<>("sortNum",instance)); treeList.forEach(e -> { List<Tree<String>> childrenList = e.getChildren(); if(CollectionUtils.isNotEmpty(childrenList)){ treeListSort(childrenList); } }); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。