美文网首页
树形展示数据返回

树形展示数据返回

作者: c_gentle | 来源:发表于2020-09-17 13:50 被阅读0次

    前端页面展示数据树形数据时,需要我们返回一定的数据格式,这样的情况有两种解决办法,一种是进行字符串拼接,另一种是编写对应的实体类。下面是以实体类的方式进行解决。


    课程分类列表树形显示.png

    第一种实现方式

    一、编写实体类

    一级分类

    package com.caiweiwei.eduservice.entity.subject;
    import lombok.Data;
    import java.util.ArrayList;
    import java.util.List;
    
    @Data
    public class OneSubject {
        private String id;
        private String title;
        private List<TwoSubject> children=new ArrayList<>();
    }
    

    二级分类

    package com.caiweiwei.eduservice.entity.subject;
    import lombok.Data;
    
    @Data
    public class TwoSubject {
        private String id;
        private String title;
    }
    

    二、controller层

     /**
         * 功能描述:课程分类列表(树形)
         * @Author: caiweiwei
         * @Date: 2020/9/16 9:47
         */
        @GetMapping("/sublectList")
        public R getAllSubject(){
            //list的泛型是一级分类
            List<OneSubject>list= eduSubjectService.getAllOneTwoSubject();
            return R.ok().data("list",list);
        }
    

    三、service层

     /**
         * 功能描述:查询一级分类和二级分类并进行封装
         *
         * @Author: caiweiwei
         * @Date: 2020/9/16 10:12
         */
        @Override
        public List<OneSubject> getAllOneTwoSubject() {
            //查询一级分类
            QueryWrapper<EduSubject> oneSubjectQueryWrapper = new QueryWrapper<>();
            oneSubjectQueryWrapper.eq("parent_id", "0");
            List<EduSubject> oneSubjects = baseMapper.selectList(oneSubjectQueryWrapper);
            //查询二级分类
            QueryWrapper<EduSubject> twoSubjectQueryWrapper = new QueryWrapper<>();
            oneSubjectQueryWrapper.ne("parent_id", "0");
            List<EduSubject> twoSubjects = baseMapper.selectList(twoSubjectQueryWrapper);
            List<OneSubject> oneSubjectList = new ArrayList<>();
            //封装一级分类
            for (int i = 0; i < oneSubjects.size(); i++) {
                EduSubject eduSubject = oneSubjects.get(i);
                OneSubject oneSubject = new OneSubject();
                //将属性进行复制
                BeanUtils.copyProperties(eduSubject, oneSubject);
                //封装二级分类
                List<TwoSubject> twoSubjectList = new ArrayList<>();
                for (int j = 0; j < twoSubjects.size(); j++) {
                    if(twoSubjects.get(j).getParentId().equals(eduSubject.getId())){
    
                        TwoSubject twoSubject = new TwoSubject();
                        //将属性进行赋值
                        BeanUtils.copyProperties(twoSubjects.get(j), twoSubject);
                        twoSubjectList.add(twoSubject);
                    }
                }
                //将封装好的二级分类封装到一级分类的实体类中
                oneSubject.setChildren(twoSubjectList);
                oneSubjectList.add(oneSubject);
            }
            return oneSubjectList;
        }
    

    第二种实现方式

    一、创建树形结构实体类

    @Data
    public class TreeDto {
        private String id;
        private String name;
        private String pid;
        private String isParent;
        private List<TreeDto> childTreeDto;
    
    }
    

    二、创建树形工具类

    public class TreeToolUtils {
        private List<TreeDto> rootList; //根节点对象存放到这里
    
        private List<TreeDto> bodyList; //其他节点存放到这里,可以包含根节点
    
        public TreeToolUtils(List<TreeDto> rootList, List<TreeDto> bodyList) {
            this.rootList = rootList;
            this.bodyList = bodyList;
        }
    
        public List<TreeDto> getTree(){   //调用的方法入口
            if(bodyList != null && !bodyList.isEmpty()){
                //声明一个map,用来过滤已操作过的数据
                Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
                rootList.forEach(beanTree -> getChild(beanTree,map));
                return rootList;
            }
            return null;
        }
    
        public void getChild(TreeDto treeDto,Map<String,String> map){
            List<TreeDto> childList = Lists.newArrayList();
            bodyList.stream()
                    .filter(c -> !map.containsKey(c.getId()))
                    .filter(c ->c.getPid().equals(treeDto.getId()))
                    .forEach(c ->{
                        map.put(c.getId(),c.getPid());
                        getChild(c,map);
                        childList.add(c);
                    });
            treeDto.setChildTreeDto(childList);
    
        }
    }
    

    三、使用main方法进行测试

    TreeDto treeDto = new TreeDto("1", "总店", "null", "true",null);
        TreeDto treeDto1 = new TreeDto("2", "市分店", "1", "true",null);
        TreeDto treeDto2 = new TreeDto("3", "县分店", "2", "true",null);
        TreeDto treeDto3 = new TreeDto("710", "店长", "3", "true",null);
        TreeDto treeDto4= new TreeDto("713", "财务部", "3", "true",null);
        TreeDto treeDto5 = new TreeDto("20032", "后勤部", "3", "true",null);
        TreeDto treeDto6 = new TreeDto("1909", "小王", "710", "false",null);
        TreeDto treeDto7= new TreeDto("1974", "小张", "713", "false",null);
        TreeDto treeDto8 = new TreeDto("388187", "佳佳", "20032", "false",null);
        TreeDto treeDto9 = new TreeDto("1949", "阿达", "20032", "false",null);
        ArrayList<TreeDto> rootList = new ArrayList<>();//根节点
        ArrayList<TreeDto> bodyList = new ArrayList<>();//子节点
        rootList.add(treeDto);
        bodyList.add(treeDto1);
        bodyList.add(treeDto2);
        bodyList.add(treeDto3);
        bodyList.add(treeDto4);
        bodyList.add(treeDto5);
        bodyList.add(treeDto6);
        bodyList.add(treeDto7);
        bodyList.add(treeDto8);
        bodyList.add(treeDto9);
        TreeToolUtils utils =  new TreeToolUtils(rootList,bodyList);
        List<TreeDto> result =  utils.getTree();
        String jsonString = JSONObject.toJSONString(result.get(0));
        System.out.println(jsonString);
    }
    

    四、控制台输出样式

    {
        "childTreeDto": [{
            "childTreeDto": [{
                "childTreeDto": [{
                    "childTreeDto": [{
                        "childTreeDto": [],
                        "id": "1909",
                        "isParent": "false",
                        "name": "小王",
                        "pid": "710"
                    }],
                    "id": "710",
                    "isParent": "true",
                    "name": "店长",
                    "pid": "3"
                }, {
                    "childTreeDto": [{
                        "childTreeDto": [],
                        "id": "1974",
                        "isParent": "false",
                        "name": "小张",
                        "pid": "713"
                    }],
                    "id": "713",
                    "isParent": "true",
                    "name": "财务部",
                    "pid": "3"
                }, {
                    "childTreeDto": [{
                        "childTreeDto": [],
                        "id": "388187",
                        "isParent": "false",
                        "name": "佳佳",
                        "pid": "20032"
                    }, {
                        "childTreeDto": [],
                        "id": "1949",
                        "isParent": "false",
                        "name": "阿达",
                        "pid": "20032"
                    }],
                    "id": "20032",
                    "isParent": "true",
                    "name": "后勤部",
                    "pid": "3"
                }],
                "id": "3",
                "isParent": "true",
                "name": "县分店",
                "pid": "2"
            }],
            "id": "2",
            "isParent": "true",
            "name": "市分店",
            "pid": "1"
        }],
        "id": "1",
        "isParent": "true",
        "name": "总店",
        "pid": "null"
    }
    

    相关文章

      网友评论

          本文标题:树形展示数据返回

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