美文网首页
Java 树形结构构造器

Java 树形结构构造器

作者: 阿杰_96c5 | 来源:发表于2020-10-22 09:38 被阅读0次

    Java 树形结构构造器

    1. 创建树型结构节点基类

    /**
     * Tree基类
     */
    public class TreeEntity extends BaseEntity
    {
        private static final long serialVersionUID = 1L;
    
        /** 当前节点ID */
        private int id;
    
        /** 父节点ID */
        private int parentId;
    
        /** 显示顺序 */
        private int orderNum;
    
         /** 子节点列表 */
        private List<TreeEntity> children = new ArrayList<>();
        
    }
    

    2. 树型结构构造器

    先生成父节点

    然后调用buildChildren 递归生成子节点

     public static  List<? extends TreeEntity> buildTree(List<? extends TreeEntity> dataList, int level) {
            List<TreeEntity> parentList = new ArrayList<>();
            x = 0;
            // 生成0级节点
            TreeEntity treeEntity = new TreeEntity();
            treeEntity.setLabel("全部");
            treeEntity.setId(0);
            treeEntity.setOrderNum(1);
            parentList.add(treeEntity);
    
            buildChildren(parentList, dataList,level);
            return parentList;
        }
    

    3. 子节点递归生成器

     private static void buildChildren(List<? extends TreeEntity> parentList, List<? extends TreeEntity> dataList,int level) {
            if(x<level) {
                x++;
                for (TreeEntity parent : parentList) {
                    List<? extends TreeEntity> children = dataList.stream().filter(item -> ((TreeEntity) item).getParentId() == parent.getId())
                            .collect(Collectors.toList());
    
                    parent.setChildren((List<TreeEntity>) children);
    
                    buildChildren(children, dataList, level);
                }
            }
    
        }
    

    4. 构造器的使用

    4.1实体类
    @Data
    public class DormGroup extends TreeEntity {
    
        private int id;
    
        /**
         * 分组名称
         */
        @NotBlank
        private String menuName;
        /**
         * 父分组ID
         */
        @NotNull
        private int parentId;
        /**
         * 父分组名称
         */
        private String parentName;
    
        /**祖级列表*/
        private String  ancestors;
        /**
         * 显示顺序
         */
        private int orderNum;
        /**
         * 分组状态(0正常 1删除)
         */
        private char status
    
    }
    
    4.2 mapper
    <resultMap id="dormGroupResult" type="com.foen.dorm.domain.DormGroup">
            <id property="id" column="id" />
            <result property="menuName" column="menu_name"/>
            <result property="label" column="menu_name"/>
            <result property="parentId" column="parent_id"/>
            <result property="ancestors" column="ancestors"/>
            <result property="parentName" column="parent_name"/>
            <result property="orderNum" column="order_num" />
            <result property="status"   column="status"/>
            <result property="createBy"      column="create_by"      />
            <result property="createTime"    column="create_time"    />
            <result property="updateBy"      column="update_by"      />
            <result property="updateTime"    column="update_time"    />
    
        </resultMap>
    
        <sql id="selectDormGroup">
    
            select
                a.id,
                a.menu_name,
                a.parent_id,
                a.ancestors,
                b.menu_name as parent_name,
                a.order_num,
                a.status,
                a.create_by,
                a.create_time,
                a.update_by,
                a.update_time
            from
                `foen-vue`.dorm_group a
            left join  `foen-vue`.dorm_group  b on  a.parent_id  = b.id
    
        </sql>
    
    
    4.3 service中使用
    public List<DormGroup> selectList() {
        List<DormGroup> dormGroups = dormGroupMapper.selectDormGroup();
        return (List<DormGroup>) buildTree(dormGroups);
    
    }
    

    查询子分类(含自己)

    id menu_name parent_id ancestors order_num status
    1 洪鼎城 0 0 1 0
    2 忠信22 0 0 2 0
    3 一号楼 1 0,1 1 0
    4 二号楼 1 0,1 2 0
    5 三号楼 1 0,1 3 0
    6 4号楼 1 0,1 4 0
    7 333 0 0 3 0
    8 4444 0 0 4 1
    9 40001 8 0,8 1 1
    10 4000102 9 0,8,9 1 1
    11 33301 7 0,7 1 0
    12 33302 7 0,7 2 0
    13 33303 7 0,7 3 0
    14 4441 0 0 4 1
    19 33304 7 0,7 4 0
    select id from `foen-vue`.dorm_group   where FIND_IN_SET(#{groupId},ancestors) 
    union all select #{groupId} as id 
    

    查询父分类id(含自己)

    将表中的ancestors字段按逗号分割成行

    SELECT
    SUBSTRING_INDEX(SUBSTRING_INDEX(a.ancestors,',',b.help_topic_id+1),',',-1) AS id
    FROM
    mysql.help_topic b
    inner join `foen-vue`.dorm_group a
    WHERE
    b.help_topic_id <![CDATA[< ]]>  LENGTH(a.ancestors)-LENGTH(REPLACE(a.ancestors,',',''))+1
    and a.id = #{id}
    union all select #{id} AS id
    

    相关文章

      网友评论

          本文标题:Java 树形结构构造器

          本文链接:https://www.haomeiwen.com/subject/vklumktx.html