2023年6月22日发(作者:)
springboot博客项⽬实现多级评论(后端)⾸先是这个接⼝需要返回的json数据:可以看到是⼀层套着⼀层来实现多级评论的。```java{ "success": true, "code": 200, "msg": "success", "data": [ { "id": 53, "author": { "nickname": "李四", "avatar": "localhost:8080/static/img/", "id": 1 }, "content": "写的好", "childrens": [ { "id": 54, "author": { "nickname": "李四", "avatar": "localhost:8080/static/img/", "id": 1 }, "content": "111", "childrens": [], "createDate": "1973-11-26 08:52", "level": 2, "toUser": { "nickname": "李四", "avatar": "localhost:8080/static/img/", "id": 1 } } ], "createDate": "1973-11-27 09:53", "level": 1, "toUser": null } ]}数据库的结构:要返回给页⾯数据的vo:public class CommentVo { private Long id; private SysUserVo author; private String content; private List childrens; private String createDate; private Integer level; private SysUserVo toUser; //⽗节点id private Long parentId; }⼤致思路是⾸先从数据库中查出所有记录,根据库中各个评论的⽗⼦关系(通过id确定)来⽣成树。根节点是⼀个空节点,每个1级评论是这个根节点的⼦节点。树的节点类:public class MyTreeNode { private Long parentId; private Long Id; //⼦节点 private List commentVoList; //内容 private CommentVo content; //创建节点 public static MyTreeNode buildNode(CommentVo commentVo) { MyTreeNode node=new MyTreeNode(); (()); entId(entId()); tent(commentVo); return node; } public static MyTreeNode buildRoot() { MyTreeNode node=new MyTreeNode(); (0L); entId(-999L); tent(null); ArrayList children=new ArrayList<>(); ldren(children); return node; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { Id = parentId; } public Long getId() { return Id; } public void setId(Long id) { Id = id; } public List getChildren() { return commentVoList; } public void setChildren(List commentVoList) { tVoList = commentVoList; } public CommentVo getContent() { return content; } public void setContent(CommentVo content) { t = content; }}将所有的评论Vo⽣成树的⼯具类:在findParentNode⽅法中,根据每⼀个节点的id来寻找他的⽗节点。找到⽗节点后将⼦节点所携带的CommentVo对象添加到⽗节点的CommentVoList中(为了⽣成前端返回的json数据)。//实现多级评论,将此篇⽂章的全部评论封装成为⼀棵树@Componentpublic class TreeUtils { public MyTreeNode buildTree(List commentVoList){ //创建根节点 id为0 MyTreeNode root = oot(); //根节点的⼦节点列表 List rootChildren=new ArrayList<>(); //将评论封装为树的节点 List nodes=new ArrayList<>(); for (CommentVo commentVo : commentVoList) { MyTreeNode TreeNode = ode(commentVo); (TreeNode); } for (MyTreeNode node : nodes) { MyTreeNode parentNode=findParentNode(node,nodes); if(parentNode!=null){ //不为空,说明有⽗节点 if(ldren()==null){ //说明这个节点是⽗节点的第⼀个⼦节点,创建⼀个⼦节点数组 List children=new ArrayList<>(); (node); ldren(children); }else{ ldren().add(node); } }else{ //为空,说明⽗节点为根节点 //⽗节点添加⼦节点 (node); //⼦节点添加⽗节点id entId(()); } ldren(rootChildren); } return root; } //找到⼦节点的⽗节点 public MyTreeNode findParentNode(MyTreeNode childNode,List allNodes){ for (MyTreeNode node : allNodes) { if(entId().equals(())){ //将⼦评论的内容添加到⽗评论的children列表中。 //如果⽗评论(CommentVo)的⼦评论列表为null,则新建⼀个列表。 List fatherCommentVoList = tent().getChildrens(); if(fatherCommentVoList==null){ fatherCommentVoList=new ArrayList<>(); (tent()); tent().setChildrens(fatherCommentVoList); }else{ tent().getChildrens().add(tent()); } //添加此节点的⽗亲节点 entId(()); return node; } } return null; }}CommentService实现类中查找⽂章评论功能的实现:使⽤的是MybatisPlus,并使⽤BeanUtil类的copyProperties⽅法将查询到的comment数据转变为CommentVo数据。public Result getCommentsById(Long id) { //查出⽂章的所有评论 QueryWrapper qw=new QueryWrapper<>(); ("article_id",id); List comments = List(qw); //所有CommentVo对象的列表 List commentVos=new ArrayList<>(); //补充评论内容 copy属性转换为vo for (Comment comment : comments) { CommentVo commentVo=new CommentVo(); operties(comment,commentVo); //作者信息 SysUser author = d(horId()); SysUserVo authorVo=new SysUserVo(); operties(author,authorVo); hor(authorVo); //toUser信息 level>1
说明有回复对象 if(el()>1){ SysUserVo toUserVo=new SysUserVo(); SysUser toUser=d(id()); operties(toUser,toUserVo); ser(toUserVo); } (commentVo); } //构造成树 MyTreeNode myTree = ree(commentVos); //根节点的⼦节点(即level为1的评论
) List children = ldren(); List level1CommentVo=new ArrayList<>(); for (MyTreeNode child : children) { (tent()); } return s(level1CommentVo); }controller类:@RequestMapping("/comments")public class CommentsController { @Autowired CommentService commentService; @GetMapping("/article/{id}") public Result getCommentsById(@PathVariable("id") Long id){ return mentsById(id); }}最后是发送请求后得到的数据:{ "success": true, "code": 200, "msg": "success", "data": [ { "id": 53, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "写的好", "childrens": [ { "id": 54, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "111", "childrens": [ { "id": 58, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "444", "childrens": null, "createDate": null, "level": 3, "toUser": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "parentId": 54 } ], "createDate": null, "level": 2, "toUser": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "parentId": 53 }, { "id": 57, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "333", "childrens": [ { "id": 59, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "555", "childrens": [ { "id": 60, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "666", "childrens": null, "createDate": null, "level": 4, "toUser": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "parentId": 59 } ], "createDate": null, "level": 3, "toUser": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "parentId": 57 } ], "createDate": null, "level": 2, "toUser": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "parentId": 53 } ], "createDate": null, "level": 1, "toUser": null, "parentId": 0 }, { "id": 56, "author": { "id": 1, "nickname": "李四", "avatar": "localhost:8080/static/img/" }, "content": "222", "childrens": null, "createDate": null, "level": 1, "toUser": null, "parentId": 0 } ]}最后再次感谢给与参考的⼤佬,这是⼤佬的博客:由于本⼈是新⼿,所以写的代码可能不堪⼊⽬。发这篇⽂章也是记录⼀下好不容易实现的功能。肯定有不正确的地⽅,希望各位⼤佬不吝指教。
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687381030a5776.html
评论列表(0条)