JAVA 实现评论功能设计开发
实现类似朋友圈的评论楼层
思路:
1、嵌套型的评论⽅式所需要的是树状型的,评论多起来的话层级结构会变得很复杂
实现原理为在评论表之中添加⼀个【parent_id】字段,定义评论和回复为⽗⼦级的关系,评论为⽗级,回复为⼦级,默认为【0】,表⽰为没有⽗级 DTO 设计如下:create table `comment` ( `id` int(11) not null auto_increment comment '主键id', `nickname` varchar(255) default null comment '评论者昵称', `avatar` varchar(255) comment '评论头像', `content` varchar(255) default null comment '评论的内容', `blog_id` int(11) default null comment '评论的博客id', `parent_id` int(11) default '-1' comment '⽗级评论id', primary key (`id`) ) comment '评论表';
1
2
3
4
5
6
7
8
9
2、需要使⽤stream流的groupingBy⼯具,将查出来的所有评论根据parent_id进⾏分组,⽣成⼀个以parent_id作为Key,评论记录集合为value的map结构 循环遍历评论记录,根据Id去map集合中去查相关⼦回复List集合,如果没有则设置为null
3、前端可通过控件对该List集合树结构进⾏解析,效果如下:public class CommentDetailsDto implements Serializable { @ApiModelProperty("id") private Long id ; @ApiModelProperty("上级评论id") private Long pid ; @ApiModelProperty("上级评论名称") private String repliedName ;
@ApiModelProperty("评论⽤户id") private Long userId ; @ApiModelProperty("⽤户头像") private String userPicture ; @ApiModelProperty("⼦回复") private List <CommentDetailsDto > commentDetailsListChild ;}
垂直风道机箱
1
2
3
4
5
6
7
8
9
人脸识别怎么建模10
11
12
13
14
15
16
17
18
19
20
21 list = commentDetailsRepository .list (map ); List <CommentDetailsDto > commentDetailsDtos = CommentDetails .fromModelList (list ); if (CollectionUtils .isNotEmpty (commentDetailsDtos )) { Map <Long , List <CommentDetailsDto >> zoneByParentIdMap = commentDetailsDtos .stream ().collect (Collectors .groupingBy (CommentDetailsDto commentDetailsDtos .forEach (regionTree -> { regionTree .setCommentDetailsListChild (zoneByParentIdMap .get (regionTree .getId ())); CommentDetails commentDetails = commentDetailsRepository .findById (regionTree .getPid ()); if (CollectionUtils .isEmpty (regionTree .getCommentDetailsListChild ())){ regionTree .setCommentDetailsListChild (new ArrayList <>()); } }); commentDetailsDtos = commentDetailsDtos .stream ().filter (v -> v .getPid () == 0).collect (Collectors .toList ()); }
1
2
3
4
led显指数5
6
拳击架
扇贝笼7
8
9
均质泵
10
11
12
13
14