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);
        }
    }
}
原文地址:https://www.cnblogs.com/linding/p/14845680.html