- 演示
- 数据库
- 后端
演示评论、回复、点赞、取消点赞。
前端 vue+elementUI、vue脚手架vue-VLI、axios
后端 springboot、mybatis-plus(和其代码生成器)
数据库 MySQL
项目地址:https://gitee.com/panqiyi/comment-demo
评论:
回复:
点赞、取消点赞(图标颜色变化):
示例图片:
周杰伦用户:显示周杰伦点赞过的评论变色
刘德华用户:显示刘德华点赞过的评论变色
前端页面vue
ComTest.vue
数据库
评论表:
用户表:
点赞表:
核心业务是 取出所有评论 并递归组装为只有二层评论:顶级 >子级(包含子子孙孙级),还有就是点赞业务统计点赞数并判断该登录用户是否对评论点赞,点赞要将图标变色,同一个用户点一次+1,再点-1。
部分代码:TCommentVOServiceImpl
@Service public class TCommentVOServiceImpl extends ServiceImplimplements TCommentService { // 保存评论 @Override public Boolean saveComment(TComment TComment) { boolean save = save(TComment); return save; } // 返回 评论列表 @Override public List getCommentList(String itemId) { // 1、获取id=itemId文章下的 所有评论 QueryWrapper allwrapper = new QueryWrapper<>(); allwrapper.eq("item_id",itemId); List allList = list(allwrapper); // 所有评论集合 // 遍历所有评论集合,复制到 评论视图返回类 ArrayList commentVos = new ArrayList<>(); //评论视图返回类 集合 for (TComment tComment : allList) { CommentVo commentVo = new CommentVo(); BeanUtils.copyProperties(tComment,commentVo); commentVo.setTime(tComment.getTCreate()); // 时间 commentVo.setComment(tComment.getContent()); // 内容 commentVos.add(commentVo); } // for (CommentVo commentVo : commentVos) { // System.out.println(commentVo); // } // // System.out.println(".................."); ArrayList com = new ArrayList<>(); // 最终集合 HashMap map = new HashMap<>(); // 2、com集合存储顶级评论 for (CommentVo tComment : commentVos) { // 遍历每一条评论 if ("0".equals(tComment.getParentId())){ // 顶级评论 com.add(tComment); } map.put(tComment.getId(),tComment); // 所有评论 id:评论对象 } // 3、com集合内顶级评论 存储所有包含的子级评论 for (CommentVo tComment : commentVos) { // 遍历每一条评论 if (!"0".equals(tComment.getParentId())){ // 非顶级评论,既第一子级、第二子级等等评论 // 获取所有父级评论 (如 a->b, b->c, c->d, d="0") CommentVo parent = map.get(tComment.getParentId()); if (parent == null){ continue; // 跳出本次循环 } if (parent.getId().equals(tComment.getId())){ continue; } parent.getReply().add(tComment); // 所有父级存储子级 (b(reply[b]), c(replay[b(reply[a])]) ....) } } // 结果是 顶级包含子级,子级包含孙级....,层次嵌套, // 但是我们想返回的是 父级>子级(包含所有子子孙孙) 两层 // 组装好的评论数据 List formatList = getFormat(com); return formatList; } // 存储 新组装好的 子级(子子孙孙) 数据集合 ArrayList newReplys = new ArrayList<>(); public List getFormat(List com){ for (CommentVo tComment : com) { // 遍历顶点集合 // 获取顶点 子级集合 List replys = tComment.getReply(); for (CommentVo comment : replys) { // 遍历第一级(子级)回复集合 ( for(:) 只有集合元素>0才会进入) newReplys.add(comment); // 存储第一级子评论 recursively(comment); // 递归子级评论 } tComment.setReply(newReplys); // 重新设置子级(包含子子孙孙) tComment.setCommentNum(newReplys.size()); // 低级评论回复数 // 新建(或清除)组装集合,避免下一轮顶点集合组装有数据 newReplys = new ArrayList<>(); } return com; } private void recursively(CommentVo comment){ List replys = comment.getReply(); // 第二子级集合 if (replys.size()>0){ // 存在评论 for (CommentVo reply : replys) { newReplys.add(reply); // 存储 if (reply.getReply().size()>0){ recursively(reply); // 递归 } } } } }
建议直接上面我git链接弄下来看完整的。




{{ item.comment }}