字典树

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<DictTreePair> 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<DictTreePair> getChildren() {
        return children;
    }

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

2.实现类

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

    /**
     * List转树结构
     * @param list
     * @return
     */
    private List<DictTreePair> convertListToTree(List<DictTreePair> list) {
        List<DictTreePair> treeList = Lists.newArrayList();
        // list转map
        final Map<String, DictTreePair> 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方法

<select id="selectDictTreeList" resultType="DictTreePair">
        select dict_type, dict_code, parent_code, dict_text, level, sort,is_leaf
        from sys_tree_data dict
        where dict_type in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and dict.deleted = 0 and dict.status = 1
        order by dict.dict_type, dict.sort
    </select>

  

posted @ 2022-04-16 15:55  南山下的采药人  阅读(30)  评论(0编辑  收藏  举报