递归节点,指定数据格式思路

本文介绍了一种将思维导图数据转换为特定格式的方法。通过递归查询数据库获取思维导图的所有子节点,并将这些信息封装成指定的数据结构。文章详细展示了如何使用Java实现这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述这是我想要的数据格式

 public Node initializService (String minderId){
        Node node = new Node();
        RootMind rootMind=new RootMind();
            //根据思维导图根节点id查出根节点下所有属性
            MindPo po = mindMapper.selectByMindId(minderId);
            //创建vo对象,用来接收根节点返回值
            if (po != null) {

                node.setTemplate(po.getTemplate());
                node.setTheme(po.getTheme());

                MindVO vo = new MindVO();
                vo.setId(po.getId());
                vo.setCreated(po.getCreated());
                vo.setExpandState(po.getExpandState());
                vo.setImage(po.getImage());
                vo.setNote(po.getNote());
                vo.setText(po.getText());
                vo .setPriority(po.getPriority());
                vo.setProgress(po.getProgress());
                vo.setHyperlink(po.getHyperlink());
                vo.setHyperlinkTitle(po.getHyperlinkTitle());
                vo.setTheme(po.getTheme());
                vo.setTemplate(po.getTemplate());
                ImgSize imgSize = new ImgSize();
                imgSize.setImgSizeWidth(po.getImgSizeWidth());
                imgSize.setImgSizeWidth(po.getImgSizeHeight());
                vo.setImgSize(imgSize);
                //创建rootmind,把根节点存放到data数据里
                rootMind.setData(vo);
                //根据根节点id查找根节点下的所有子节点,进行递归操作。
                List<RootMind> rootMindList =webService.selectChildrenByParent(po.getId());
                rootMind.setChildren(rootMindList);
                node.setRoot(rootMind);
            }
        node.setRoot(rootMind);
        return node;
    }
   //递归子节点
    public List<RootMind> selectChildrenByParent(String parentId) {
        //rootMindList用来存储要返回的数据格式
        List<RootMind> rootMindList =new ArrayList<>();
        //根据parentId查询所有子节点,不确定有多少个节点,故用list接收
        List<MindVO> mindVOList=this.mindMapper.selectChildrenByParent(parentId);
        if (mindVOList.size()==0){
            return  null;
        }
        for (MindVO mindVO:mindVOList){
            //判断子节点下的子节点是否有子节点
            List<RootMind> rootMindChildren = webService.selectChildrenByParent(mindVO.getId());
            //是空则证明下边没有子节点
            if(rootMindChildren == null){
                RootMind rootMind = new RootMind();
                rootMind.setData(mindVO);
                //因如果此节点下没有子节点,需插入一个空数组(rootMind2)来完善样式。
                List<RootMind>rootMindNull = new ArrayList<>();
                rootMind.setChildren(rootMindNull);
                rootMindList.add(rootMind);
            }else{
                RootMind rootMind = new RootMind();
                rootMind.setChildren(rootMindChildren);
                rootMind.setData(mindVO);
                rootMindList.add(rootMind);
            }
        }
      return rootMindList;

    }
@Data
public class RootMind {
    private MindVO data;
    private List<RootMind> children;
}

俺又写了一种思路

 public List<RootMind> selectChildrenByParent(String parentId) {
        List<RootMind> rootMindList =new ArrayList<>();
        //根据parentId查询所有子节点,不确定有多少个节点,故用list接收
        List<MindVO> mindVOList=this.mindMapper.selectChildrenByParent(parentId);
        if (mindVOList.size()==0){
            return  null;
        }
        for (MindVO mindVO:mindVOList){
            //判断子节点下的子节点是否有子节点
            List<RootMind> rootMindChildren = this.selectChildrenByParent(mindVO.getId());
            //是空则证明下边没有子节点
            if(rootMindChildren == null){
                RootMind rootMind = new RootMind();
                Map<String,Object> mapChildren = new HashMap<>();
                mapChildren.put("id",mindVO.getId());
                mapChildren.put("created",mindVO.getCreated());
                mapChildren.put("note",mindVO.getNote());
                mapChildren.put("text",mindVO.getText());
                mapChildren.put("image",mindVO.getImage());
                mapChildren.put("imageTitle",mindVO.getImageTitle());
                ImgSize imgSize = new ImgSize();
                imgSize.setImgSizeWidth(mindVO.getImgSizeWidth());
                imgSize.setImgSizeWidth(mindVO.getImgSizeHeight());
                mapChildren.put("imageSize",imgSize);
                mapChildren.put("hyperlink",mindVO.getHyperlink());
                mapChildren.put("hyperlinkTitle",mindVO.getHyperlinkTitle());
                mapChildren.put("font-size",mindVO.getFontSize());
                mapChildren.put("font-family",mindVO.getFontFamily());
                mapChildren.put("font-style",mindVO.getFontStyle());
                mapChildren.put("font-weight",mindVO.getFontWeight());
                mapChildren.put("priority",mindVO.getPriority());
                mapChildren.put("progress",mindVO.getProgress());
                mapChildren.put("color",mindVO.getColor());
                rootMind.setData(mapChildren);
                //因如果此节点下没有子节点,需插入一个空数组(rootMind2)来完善样式。
                List<RootMind>rootMindNull = new ArrayList<>();
                rootMind.setChildren(rootMindNull);
                rootMindList.add(rootMind);
            }else{
                RootMind rootMind = new RootMind();
                rootMind.setChildren(rootMindChildren);
                Map<String,Object> mapChildren = new HashMap<>();
                mapChildren.put("id",mindVO.getId());
                mapChildren.put("created",mindVO.getCreated());
                mapChildren.put("text",mindVO.getText());
                mapChildren.put("image",mindVO.getImage());
                mapChildren.put("imageTitle",mindVO.getImageTitle());
                ImgSize imgSize = new ImgSize();
                imgSize.setImgSizeWidth(mindVO.getImgSizeWidth());
                imgSize.setImgSizeWidth(mindVO.getImgSizeHeight());
                mapChildren.put("imageSize",imgSize);
                mapChildren.put("hyperlink",mindVO.getHyperlink());
                mapChildren.put("hyperlinkTitle",mindVO.getHyperlinkTitle());
                mapChildren.put("font-size",mindVO.getFontSize());
                mapChildren.put("font-family",mindVO.getFontFamily());
                mapChildren.put("font-style",mindVO.getFontStyle());
                mapChildren.put("font-weight",mindVO.getFontWeight());
                mapChildren.put("priority",mindVO.getPriority());
                mapChildren.put("progress",mindVO.getProgress());
                mapChildren.put("color",mindVO.getColor());
                rootMind.setData(mapChildren);
                rootMindList.add(rootMind);
            }
        }
      return rootMindList;

    }
public Node initializService (String minderId){
        Node node = new Node();
        RootMind rootMind=new RootMind();
            //根据思维导图根节点id查出根节点下所有属性
            MindPo po = mindMapper.selectByMindId(minderId);
            //创建vo对象,用来接收根节点返回值
            if (po != null) {
                node.setTemplate(po.getTemplate());
                node.setTheme(po.getTheme());
                Map<String,Object> map = new HashMap<>();
                map.put("id",po.getId());
                map.put("created",po.getCreated());
                map.put("text",po.getText());
                map.put("image",po.getImage());
                ImgSize imgSize = new ImgSize();
                imgSize.setImgSizeWidth(po.getImgSizeWidth());
                imgSize.setImgSizeWidth(po.getImgSizeHeight());
                map.put("imageSize",imgSize);
                map.put("hyperlink",po.getHyperlink());
                map.put("hyperlinkTitle",po.getHyperlinkTitle());
                map.put("font-size",po.getFontSize());
                map.put("font-family",po.getFontFamily());
                map.put("font-style",po.getFontStyle());
                map.put("font-weight",po.getFontWeight());
                map.put("priority",po.getPriority());
                map.put("progress",po.getProgress());
                map.put("color",po.getColor());
                map.put("note",po.getNote());
                rootMind.setData(map);
                //根据根节点id查找根节点下的所有子节点,进行递归操作。
                List<RootMind> rootMindList =this.selectChildrenByParent(po.getId());
                rootMind.setChildren(rootMindList);
                node.setRoot(rootMind);
            }
//        node.setRoot(rootMind);
        return node;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值