zoukankan      html  css  js  c++  java
  • 分享知识-快乐自己:递归 遍历删除信息

    首先看一下表结构:

    需求:

      根据 id 删除当前内容(判断当前父节点下是否还有子节点;如果没有则需要更改父类 is_parent=0 变成子节点)(如果有就不要更新)

        根据 id 删除当前内容时,如果是一个父节点 同时也需要删除所有的子节点。

    如下关键代码:递归实现...

    @Override
        public TaotaoResult deleteByPrimaryKey(Long id) {
            //01、首先查询要删除的对象信息
            TbContentCategory category = tbContentCategoryMapper.selectByPrimaryKey(id);
            if (category.getParentId() == 0) {
                return new TaotaoResult(-1, "不能删除根节点", null);
            }
            tbContentCategoryMapper.deleteByPrimaryKey(category.getId());
            //02、查询是否还有子节点,如果没有则把父节点改变成子节点
            TbContentCategoryExample example = new TbContentCategoryExample();
            example.createCriteria().andParentIdEqualTo(category.getParentId());
            //返回的子节点集合信息
            List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(example);
            if (null == tbContentCategories && tbContentCategories.size() == 0) {
                //将父节点变成子节点
                TbContentCategory category1 = new TbContentCategory();
                category1.setId(category.getParentId());
                category1.setIsParent(false);
                category1.setUpdated(new Date());
                tbContentCategoryMapper.updateByPrimaryKeySelective(category1);
            }
            //当前删除的对象信息要是父节点,则在进行递归删除
            if (category.getIsParent()) {
                del(category);
            }
            return TaotaoResult.ok();
        }
    
        /***
         * 递归删除
         * @param category
         */
        private void del(TbContentCategory category) {
            //01、判断是否为字节点还是父节点,子节点直接删除
            if (category.getIsParent()) {
                //02、查询出所有的子节点
                TbContentCategoryExample example = new TbContentCategoryExample();
                example.createCriteria().andParentIdEqualTo(category.getId());
                List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(example);
                /***
                 * 1、首先是根据id 查询出所有的子节点 对象集合
                 * 2、利用循环遍历每一项,进行删除,同时把当前便利的对象 当作参数 再次调用删除方法
                 *    来判断此对象下是否还有子节点
                 */
                for (TbContentCategory item : tbContentCategories) {
                    tbContentCategoryMapper.deleteByPrimaryKey(item.getId());
                    del(item);
                }
            }
    //        if (category.getIsParent()) {
    //            tbContentCategoryMapper.deleteByPrimaryKey(category.getId());
    //        }
        }
  • 相关阅读:
    开发,从需求出发 &#183; 之四 春天在这里
    面向基于英特尔&#174; 架构的 Android* 的 CoCos2D
    js左右切换 选择年龄
    先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
    [ACM] hdu 1251 统计难题 (字典树)
    设计模式:单例模式的三种创建方式及其各自的优缺点
    [Android 4.4.2] 泛泰A850 Mokee4.4.2 20140509 RC2.0 by syhost
    IA32 MMU paging初始化代码
    为Android开发人员定制的搜索引擎
    Android中Activity切换时共享视图元素的切换动画(5.0以上)
  • 原文地址:https://www.cnblogs.com/mlq2017/p/10165637.html
Copyright © 2011-2022 走看看