zoukankan      html  css  js  c++  java
  • 递归删除资源树 Ztree

    前言

    • 最近项目里有这么一个需求:现在有一个用Ztree编写的资源树,当删除资源树的某个节点时,则将此节点下面的所有节点全部删除,这里显然就用到了递归;若此节点被删除后无其它的兄弟节点了,我们还需要将其父节点更新成新的子节点。

    代码中用到的技术

    • 小编操作数据库用的是mybatis,大部分操作直接使用的mybatis的逆向工程,至于mapper的注入,我就不贴代码了。

    1、删除节点的入口

    public void deleteCategory(Long id) {
        //将此节点对象从数据库中搜出来
        TbContentCategory node = categoryMapper.selectByPrimaryKey(id);
        //删除此节点
        this.recursiveDelete(id);
        //判断是否更新父节点
        this.updateParentNode(node.getParentId());
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、递归删除资源树上的节点

    /**
     * 递归删除资源树上的节点
     * @param 要删除的节点id
     */
    public void recursiveDelete(Long id) {
        // 查询此节点下面的所有的子节点
        List<TbContentCategory> list = getListByParentId(id);
        // 若此节点下面没有子节点
        if (list.size() == 0) {
            TbContentCategory deleteNode = categoryMapper.selectByPrimaryKey(id);
            //得到此节点的父节点Id
            Long parentId = deleteNode.getParentId();
            //删除此节点
            categoryMapper.deleteByPrimaryKey(id);
            //删除此节点后,判断此节点的父节点是否为子节点,若是,则更新其父节点为子节点
            this.updateParentNode(parentId);
        } else {
            categoryMapper.deleteByPrimaryKey(id);
            for (TbContentCategory category : list) {
                if (category.getIsParent()) {
                    //递归删除节点
                    this.recursiveDelete(category.getId());
                } else {            categoryMapper.deleteByPrimaryKey(category.getId());
                }
            }
    
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3、删除子节点后,判断其父节点是否需要更新成子节点

    /**
     * 判断此节点是否存在兄弟节点,若不存在,则将其父节点更新成子节点
     * @param 节点Id
     */
    private void updateParentNode(Long parentId) {
        //查询此节点的所有的兄弟节点
        List<TbContentCategory> contentCat = getListByParentId(parentId);
        //若无兄弟节点
        if (contentCat.size() == 0) {
            //更新此节点的父节点为子节点
            TbContentCategory node2 = categoryMapper.selectByPrimaryKey(parentId);
            node2.setIsParent(false);
            categoryMapper.updateByPrimaryKeySelective(node2);
        }
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4、根据父节点Id 查询所有的兄弟节点

    /**
     * 根据父节点Id 查询所有的兄弟节点
     * @param parentId
     * @return
     */
    public List<TbContentCategory> getListByParentId(Long parentId) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list = categoryMapper.selectByExample(example);
        return list;
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    使用GetLogicalProcessorInformation获取逻辑处理器的详细信息(NUMA节点数、物理CPU数、CPU核心数、逻辑CPU数、各级Cache)
    Android学习-应用程序管理
    用户过2亿获取每个用户不到6分钱,闪传是怎么做到?(最大的成本是决策成本,否则全是无用功)
    Delphi应用程序的调试(十)调试器选项(在IDE中不要使用异常)
    无标题窗体拖动(三种方法)
    关于Qt在子线程中使用QMessageBox的折衷方法
    Qt自定义事件的实现(军队真正干活,但要增加监军,大平台通知事件,事件内容自定义)
    java对数据库的操作
    ddd
    伟大是熬出来的
  • 原文地址:https://www.cnblogs.com/shizhijie/p/8659127.html
Copyright © 2011-2022 走看看