spring删除父节点及其所有子节点

service

public void deleteCategoriesById(Long id,Long pid) {

        //获取对象
        Category category1 = this.categoryMapper.selectByPrimaryKey(id);

        //创建子对象的id的列表
        List<Long> sons = new ArrayList<>();

        //创建中间列表
        List<Long> temp = new ArrayList<>();

            //判断是否是父节点
        if (category1.getIsParent()) {
            //若是父节点
            Example example = new Example(Category.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("parentId", id);

            //删除本节点
            this.categoryMapper.deleteByPrimaryKey(id);

            while (true) {
                //获取所有子对象列表
                List<Category> categories = this.categoryMapper.selectByExample(example);

                //遍历子节点列表
                for (Category category2 : categories) {
                    //判断子节点是否为父节点
                    if (category2.getIsParent()) {
                        temp.add(category2.getId());
                    }
                    //将所有需要删除的id放入sons中
                    sons.add(category2.getId());
                }
//				此处如果使用增强for循环,会报ConcurrentModificationException,
//              因为增强for循环也是通过Iterator实现的,使用List的remove方法会与Iterator产生冲突
//                for (Long son:sons){
//                    this.categoryMapper.deleteByPrimaryKey(son);
//                    sons.remove(son);
//                }
                //遍历,获取子对象id
                Iterator<Long> iterator = sons.iterator();
                while (iterator.hasNext()) {
                    Long son = iterator.next();
                    this.categoryMapper.deleteByPrimaryKey(son);
                    iterator.remove();
                }

                //如果子节点还有子节点
                if (temp.size() > 0) {
                    sons=temp;
                    temp = new CopyOnWriteArrayList<Long>();
                } else {
                    break;
                }

            }


        }

        //若不是父节点,判断其父节点是否只剩一个子,如果只剩一个,则将父节点isparent改为false
        this.categoryMapper.deleteByPrimaryKey(id);
        Category category3 = new Category();
        category3.setParentId(pid);
        List<Category> list = this.categoryMapper.select(category3);
        //list.forEach(System.out::println);
        if (list.isEmpty()){

            Category category = new Category();
            category.setId(pid);
            category.setIsParent(false);
            this.categoryMapper.updateByPrimaryKeySelective(category);

        }

    }

pojo

public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private Long parentId;
    private Boolean isParent;
    private Integer sort;

//get、set
}

代码比较冗余,之后再改

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值