zoukankan      html  css  js  c++  java
  • 树结构Tree查询

    1.前端数据结构

     return {
            data: [{
              label: '一级 1',
              children: [{
                label: '二级 1-1',
                children: [{
                  label: '三级 1-1-1'
                }]
              }]
            }, {
    View Code

    2.我们需要封装前端数据

    在Mybatis-Plus中需要在字段上打上标签

        @TableField(exist = false)
        private List<CourseType> children = new ArrayList<CourseType>();
    View Code

    3.方法一:Mapper中添加

    List<CourseType> selectTreeData(@Param("pid") Long i);

    在xml中添加方法(这里我们只需要迭代就可以了),先查询出最上层的目录我们传入参数为pid=0,然后在通过把查到的对象的ID找到自己儿子

     <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="cn.jiedada.hrm.domain.CourseType">
            <id column="id" property="id" />
            <result column="createTime" property="createTime" />
            <result column="updateTime" property="updateTime" />
            <result column="name" property="name" />
            <result column="pid" property="pid" />
            <result column="logo" property="logo" />
            <result column="description" property="description" />
            <result column="sortIndex" property="sortIndex" />
            <result column="path" property="path" />
            <result column="totalCount" property="totalCount" />
            <collection property="children"
                        column="id"
                        ofType="cn.jiedada.hrm.domain.CourseType"
                        select="selectTreeData">
            </collection>
        </resultMap>
    
        <select id="selectTreeData" resultMap="BaseResultMap">
            SELECT
            id,
            createTime,
            updateTime,
            name,
            pid,
            logo,
            description,
            sortIndex,
            path,
            totalCount
            FROM t_course_type WHERE pid=#{pid}
        </select>
    View Code

    4方法二:通过把所以数据查询出来自己封装,这里如果和上面的思考一样的话迭代次数太多提高查询效率我们思考二次遍历就能够查询

    (想法:也是先把最上层节点找出来,然后在遍历一次所以节点找到非最上层节点的子节点,然后追加到后面)

    封装为一个方法

     //获得TreeDate
        private List<CourseType> getCourseTree(List<CourseType> courseTypes) {
            //实现的方式是先找到所有最上层节点,然后在遍历所有节点,这些节点在保存到自己的父节点
            //获得所有数据
            //保存最上层节点
            List<CourseType> fristCourseTypes = new ArrayList<>();
            for (CourseType courseType : courseTypes) {
                if (courseType.getPid() == 0){
                    //找到所有的最上层节点
                    fristCourseTypes.add(courseType);
                }else {
                    //不是最上层节点
                    //遍历所有节点找到该节点的父节点
                    for (CourseType corruntType : courseTypes) {
                        if (corruntType.getId().equals(courseType.getPid())){
                            //保存到
                            corruntType.getChildren().add(courseType);
                            break;
                        }
                    }
                }
            }
            //因为fristCourseTypes可以list只看地址所以我们保存的数据可以直接拿到
            return fristCourseTypes;
        }
    View Code
  • 相关阅读:
    【算法】273-每周一练 之 数据结构与算法(Tree)
    【算法】272-每周一练 之 数据结构与算法(Dictionary 和 HashTable)
    【CSS】271- RGB、HSL、Hex网页色彩,看完这篇全懂了
    【Redis】270- 你需要知道的那些 redis 数据结构
    Hybird App 应用开发中5个必备知识点复习
    【Vuejs】269- 提升90%加载速度——vuecli下的首屏性能优化
    【富文本】268- 富文本原理了解一下?
    HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)
    HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (一)
    HTML5 CSS3 专题 : 拖放 (Drag and Drop)
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/13651308.html
Copyright © 2011-2022 走看看