【Java】Java实现嵌套型评论功能

文章介绍了如何使用MybatisPlus框架在Java中处理一个带有父子关系的评论列表,通过PageQueryDTO进行分页查询,并展示了数据库表结构设计和Controller接口实现。
摘要由CSDN通过智能技术生成

直接上代码:

dependency:

   <!--        mybatisPlus依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

impl:

@Override
public PageResult commentPagelist(PageQueryDTO pageQueryDTO) {
    //返回结果
    PageResult result = new PageResult();
    IPage page = new Page(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
    QueryWrapper<Comment> qw = new QueryWrapper<>();
   // 条件设置 todo
    
    IPage<Comment> resultPage = page(page, qw);
    //找出所以评论集合,查看他们是否有parent_id,有就循环遍历设置小孩
    List<Comment> commentList = resultPage.getRecords();
    //todo 需要修改成批量查询
    List<Comment> list = findChildren(commentList);
    result.setRecords(list);
    result.setTotal(resultPage.getTotal());
    return result;
}
 
 private List<Comment> findChildren(List<Comment> commentList) {
//        子评论集合
        List<Comment> childrenList = new ArrayList<>();

        for (Comment comment : commentList) {
            if (comment.getParentId() != -1) {
                childrenList.add(comment);
            }
        }
//    比较两个元素的id和parent_id是否相等,相等则将parent_id子节点set到他的父节点
        for (Comment comment : commentList) {
            comment.setChildrenComments(new ArrayList<>());//防止下面add的时候有空指针
            for (Comment children : childrenList) {
                if (children.getParentId() == comment.getId()) {
                    comment.getChildrenComments().add(children);//get到的是null
                }
            }
        }

        //如果孩子有parentId,且不是根节点,说明它已放入父的孩子集合中,不用重复放入
        Iterator<Comment> iterator = commentList.iterator();
        while (iterator.hasNext()) {
            Comment comment = iterator.next();
            if (comment.getParentId() != null && comment.getParentId() != -1) {
                iterator.remove();
            }
        }

        return commentList;//
    }

DTO:

@Data
public class PageQueryDTO {

    private Long id;
    //页码
    private int page;

    //每页显示记录数
    private int pageSize;
}

数据库表结构设计(其他字段看自己情况是否添加,重要的是有parent_id,这样才能形成树状结构):

Controller:

 @GetMapping("/list")
    public Result<PageResult> commentAllList(PageQueryDTO pageQueryDTO) {
        log.info("查询全部评论分页...");
        PageResult page= commentService.commentPagelist(pageQueryDTO);
        return Result.success(page);
    }

实现效果:

{
    "code": 1,
    "msg": null,
    "data": {
        "total": 0,
        "records": [
            {
                "id": 1,
                "nickname": "测试1",
                "avatar": null,
                "content": "你好1",
                "date": "2024-01-30T14:51:17",
                "postId": null,
                "userId": null,
                "parentId": -1,
                "status": 0,
                "childrenComments": [
                    {
                        "id": 2,
                        "nickname": "测试2",
                        "avatar": null,
                        "content": "你好2",
                        "date": "2024-01-30T14:51:09",
                        "postId": null,
                        "userId": null,
                        "parentId": 1,
                        "status": 0,
                        "childrenComments": []
                    },
                    {
                        "id": 3,
                        "nickname": "测试3",
                        "avatar": null,
                        "content": "你好3",
                        "date": "2024-01-30T14:51:06",
                        "postId": null,
                        "userId": null,
                        "parentId": 1,
                        "status": 0,
                        "childrenComments": [
                            {
                                "id": 6,
                                "nickname": "测试06",
                                "avatar": null,
                                "content": "你好06",
                                "date": "2024-01-30T14:57:20",
                                "postId": null,
                                "userId": null,
                                "parentId": 3,
                                "status": 0,
                                "childrenComments": [
                                    {
                                        "id": 7,
                                        "nickname": "测试07",
                                        "avatar": null,
                                        "content": "你好07",
                                        "date": "2024-01-30T17:33:40",
                                        "postId": null,
                                        "userId": null,
                                        "parentId": 6,
                                        "status": 0,
                                        "childrenComments": []
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "id": 5,
                        "nickname": "测试05",
                        "avatar": null,
                        "content": "你好05",
                        "date": "2024-01-30T11:35:14",
                        "postId": null,
                        "userId": null,
                        "parentId": 1,
                        "status": 0,
                        "childrenComments": []
                    }
                ]
            },
            {
                "id": 4,
                "nickname": "测试04",
                "avatar": null,
                "content": "你好04",
                "date": "2024-01-30T11:33:11",
                "postId": null,
                "userId": null,
                "parentId": -1,
                "status": 0,
                "childrenComments": []
            }
        ]
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值