zoukankan      html  css  js  c++  java
  • mybatis-递归删除思路

    @Override
    public Result deleteById(String id) {
        if (StringUtils.isEmpty(id)) {
            return Result.error("评论ID不能为空");
        }
    
        // 要删除的所有评论id
        List<String> ids = new ArrayList<>();
        // 将当前评论id放入集合中
        ids.add(id);
    
        // 递归所有的评论id,并将id装到要删除集合中
        this.getIds(ids, id);
    
        // 批量删除集合中的评论id
        baseMapper.deleteBatchIds(ids);
        return Result.ok();
    }
    
    private void getIds(List<String> ids, String parentId) {
        // 查询子评论信息
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<Comment> commentList = baseMapper.selectList(wrapper);
        // 如果子评论不为则,则取出每条评论的评论id
        if (CollectionUtils.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                String id = comment.getId();
                // 将当前查询到评论id放到要删除的id集合中
                ids.add(id);
                // 递归继续查询子评论id
                this.getIds(ids, id);
            }
        }
    }
  • 相关阅读:
    工坊第五天
    工坊第四天
    工坊第三天
    工坊第二天
    工坊第一天
    莫队 优雅暴力出奇迹
    状压 DP 总结
    关于MatlabGUI清除WorkSpace的用法
    ArduinoNano卡在上传,无法烧录
    两轮差速驱动机器人的坐标轨迹计算
  • 原文地址:https://www.cnblogs.com/linding/p/14845680.html
Copyright © 2011-2022 走看看