字典树


1.实体类

public class DictTreePair implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 字典类型
     */
    @ApiModelProperty("字典类型")
    private String dictType;

    /**
     * 类型名称
     */
    @ApiModelProperty("类型名称")
    private String typeName;

    /**
     * 字典键值
     */
    @ApiModelProperty("字典键值")
    private String dictCode;
    /**
     * 字典内容
     */
    @ApiModelProperty("字典内容")
    private String dictText;

    /**
     * 字典父级键值
     */
    @ApiModelProperty("字典父级键值")
    private String parentCode;

    /**
     * 是否是叶子节点
     */
    @ApiModelProperty("是否是叶子节点")
    private Boolean isLeaf;

    /**
     * 字典排序
     */
    @ApiModelProperty("字典排序")
    private Integer sort;

    /**
     * 子节点
     */
    @ApiModelProperty("子节点")
    private List children = new ArrayList<>();


    public String getDictType() {
        return dictType;
    }

    public void setDictType(String dictType) {
        this.dictType = dictType;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getDictCode() {
        return dictCode;
    }

    public void setDictCode(String dictCode) {
        this.dictCode = dictCode;
    }

    public String getParentCode() {
        return parentCode;
    }

    public void setParentCode(String parentCode) {
        this.parentCode = parentCode;
    }

    public Boolean getLeaf() {
        return isLeaf;
    }

    public void setLeaf(Boolean leaf) {
        isLeaf = leaf;
    }

    public String getDictText() {
        return dictText;
    }

    public void setDictText(String dictText) {
        this.dictText = dictText;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public List getChildren() {
        return children;
    }

    public void setChildren(List children) {
        this.children = children;
    }
}

2.实现类

public ResponseResult>> selectMoreMapByDictType(String dictTypes) {
        List dictTypeList = Arrays.asList(dictTypes.split(","));
        List list = sysTreeDataMapper.selectDictTreeList(dictTypeList);
        Map> dictMap = list.stream().collect(Collectors.groupingBy(DictTreePair::getDictType));
        // 既然是字典树,那么返回tree结构数据
        Map> dictTreeMap = new HashMap<>();
        dictMap.forEach((k, v) -> {
            dictTreeMap.put(k, convertListToTree(v));
        });
        return ResponseResult.success(dictTreeMap);
    }

    /**
     * List转树结构
     * @param list
     * @return
     */
    private List convertListToTree(List list) {
        List treeList = Lists.newArrayList();
        // list转map
        final Map map = new LinkedHashMap<>(list.size(), 1);
        for (DictTreePair t : list) {
            map.put(t.getDictCode(), t);
        }
        for (DictTreePair t : list) {
            // 判断是否是同源节点(父子节点的code是一样的)
            if(t.getParentCode().equals(t.getDictCode())) {
                treeList.add(t);
            } else {
                // 父节点在map中不存在,则说明当前是跟
                DictTreePair parent = map.get(t.getParentCode());
                if (parent == null) {
                    treeList.add(t);
                } else {
                    parent.getChildren().add(t);
                }
            }
        }
        return treeList;
    }

 3.mapper方法