ruijiege

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

 

posted on 2020-09-11 13:48  哦哟这个怎么搞  阅读(604)  评论(0)    收藏  举报