直接上代码:
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": []
}
]
}
}