java评论、回复功能设计与实现方法
2022-06-20 11:00:56 来源:易采站长站 作者:
目录
效果展示数据库设计数据库设计思路:实现类源码获取帖子下的评论 获取评论下的回复 获取二级回复总结最近实现了评论和回复、点赞、@的功能。在这里分享一下我的设计思路(先分享评论和回复功能)。希望各位读者给出一些不一样的建议后期改进。
效果展示
总共是两层回复 (回复评论、回复评论下的回复)
数据库设计
评论表(TFW_Comments)和回复内容表(TFW_UserResponse)以及评论回复关系表(TFW_MsgRelation)
数据库设计思路:
注:各位读者自动忽略评论表的服务机构ID字段,这个字段相当于这条评论是在哪个帖子(文章下面)
1、根据文章ID或者是帖子ID查询评论表获取评论(本文的服务机构ID)。第一层(评论)
2、根据评论ID并且回复类型等于1的去关系表获取第二层的回复(commentsId)。第二层(评论下的回复)
3、根据评论ID、回复类型等于2、回复ID去关系表获取第三层回复。第三层(评论下回复中的回复)注:回复ID是它的上级
实现类源码
@Override public Map<String, Object> findComments(JSONObject jsonObject) { data.clear(); String userId = jsonObject.getString("userId"); String role = this.role(jsonObject); if (role.equals("-1")){ //没有权限 data.put("error","-1"); data.put("msg","当前用户没有权限"); return data; } List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null); //查询点赞次数 int countTag = 0; MsgRelationTag msgRelationTag = new MsgRelationTag(); for (Map item : info){ item.put("inputShow",false); int commentsId = (int) item.get("commentsId"); //查询点赞次数 countTag = msgRelationDao.findCountTagByTagId(commentsId,1); item.put("countTag",countTag); //设置点赞状态 msgRelationTag.setTagId(commentsId); msgRelationTag.setTagType(1); msgRelationTag.setTagUserId(Integer.parseInt(userId)); MsgRelationTag msgTag = msgRelationDao.findMsgTag(msgRelationTag); if (msgTag != null) { item.put("tagStatus",msgTag.getStatus()); }else { item.put("tagStatus",""); } //如果有@id if (item.get("atId") != null){ String content = item.get("content").toString(); StringBuffer tmrAtId = findUserName(item.get("atId").toString()); item.put("content",content+'@'+tmrAtId); } //二级回复数据 List<Map<String, Object>> twoReply = new ArrayList<>(); //所有数据 List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null); for (Map userResponseInfo :userResponse){ int userResponseIds = Integer.parseInt(userResponseInfo.get("userResponseId").toString()); //查询点赞次数 countTag = msgRelationDao.findCountTagByTagId(userResponseIds,2); //设置点赞状态 msgRelationTag.setTagId(userResponseIds); msgRelationTag.setTagType(2); msgTag = msgRelationDao.findMsgTag(msgRelationTag); if (msgTag != null) {userResponseInfo.put("tagStatus",msgTag.getStatus());}else {userResponseInfo.put("tagStatus","");} userResponseInfo.put("countTag",countTag); userResponseInfo.put("inputShow",false); Integer responseType = (Integer) userResponseInfo.get("responseType"); for (Map stairReplyInfo : userResponse){ Integer userResponseId = (Integer) stairReplyInfo.get("userResponseId"); int msgRelationId = Integer.parseInt(stairReplyInfo.get("msgRelationId").toString()); //接受者id*/ twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据 for (Map twoReplyItem : twoReply){ int twoReplyId = Integer.parseInt(twoReplyItem.get("userResponseId").toString()); twoReplyItem.put("inputShow",false); //查询点赞次数 countTag = msgRelationDao.findCountTagByTagId(twoReplyId,2); twoReplyItem.put("countTag",countTag); //设置点赞状态 msgRelationTag.setTagId(twoReplyId); msgTag = msgRelationDao.findMsgTag(msgRelationTag); if (msgTag != null) {twoReplyItem.put("tagStatus",msgTag.getStatus());}else {twoReplyItem.put("tagStatus","");} String userRepContent = twoReplyItem.get("userRepContent").toString(); if (twoReplyItem.get("tmrAtId") != null){ StringBuffer tmrAtId = findUserName(twoReplyItem.get("tmrAtId").toString()); twoReplyItem.put("userRepContent",userRepContent+'@'+tmrAtId); } } stairReplyInfo.put("twoReply",twoReply); } } item.put("stairReply",userResponse); } data.put("data",info); data.put("error",0); data.put("msg","查询成功"); return data; }
其它的代码可以忽略。主要语句有:
获取帖子下的评论
List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null);
上图根据FWJLID获取评论。(此处可以当成帖子的ID,获取帖子下的评论)一级展示
对应SQL语句(OPT是我的用户表)
select tc.content ,tc.commentsId,convert(varchar(19),tc.startTime,120) as startTime,tc.recipientId ,tc.operatorId,zo.NAME as operatorName,tc.atId,zo.HeadImgUrl as operatorHeadImgUrl from TFW_Comments tc left join zd_opt zo on zo.AID = tc.operatorId where tc.FWJLID = 5101
查询结果:
获取评论下的回复
List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
上图根据commentsid获取评论下的回复。(根据评论ID获取回复)二级展示
对应sql语句
select tur.userResponseId,tur.operatorId,tur.recipientId,convert(varchar(19),tur.startTime,120) as startTime,tur.userRepContent,tmr.atId as tmrAtId, tmr.msgRelationId ,tmr.responseType,tmr.replyId, zo.NAME as operatorName, zo1.NAME as recipientName, zo.HeadImgUrl as operatorHeadImgUrl, zo1.HeadImgUrl as recipientHeadImgUrl from TFW_MsgRelation tmr left join TFW_UserResponse tur on tur.userResponseId = tmr.userResponseId left join zd_opt zo on zo.AID = tur.operatorId left join zd_opt zo1 on zo1.AID = tur.recipientId where tmr.commentsId = 47
查询结果
获取二级回复
twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据
上图是根据评论ID(msgRelationId)和回复ID(userResponseId)去获取二级回复。回复ID也就是父类。就是回复那一条回复的ID。 第三层展示
对应sql
select tur.userResponseId,tur.operatorId,tur.recipientId,convert(varchar(19),tur.startTime,120) as startTime,tur.userRepContent,tmr.atId as tmrAtId, tmr.msgRelationId ,tmr.responseType,tmr.replyId, zo.NAME as operatorName, zo1.NAME as recipientName, zo.HeadImgUrl as operatorHeadImgUrl, zo1.HeadImgUrl as recipientHeadImgUrl from TFW_MsgRelation tmr left join TFW_UserResponse tur on tur.userResponseId = tmr.userResponseId left join zd_opt zo on zo.AID = tur.operatorId left join zd_opt zo1 on zo1.AID = tur.recipientId where tmr.commentsId = 136 and tmr.replyId = 155
查询结果
返回页面展示和返回体展示
总结
到此这篇关于java评论、回复功能设计与实现方法的文章就介绍到这了,更多相关java评论回复功能内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!
如有侵权,请联系QQ:279390809 电话:15144810328
最新图文推荐
相关文章
-
Spring Cloud 整合Apache-SkyWalking实现链路跟踪的方法
什么是SkyWalking 查看官网https://skywalking.apache.org/ 分布式系统的应用程序性能监视工具,专为微服务、云原生架构和基于容器(Docker、K8s、Mesos)架构而设计。 安装 进入下载页面https://2020-06-18
-
成功解决IDEA2020 Plugins 连不上、打不开的方法
IntelliJ IDEA 2020.1 插件中心一直打不开,鉴于有部分同学反馈设置http proxy不能解决,所以可按以下顺序检查 一、设置 http proxy—勾上Auto-detect proxy setting,参照下图,加上地址 http://127.0.02020-06-25
-
IDEA2020 1.1中Plugins加载不出来的问题及解决方法
进入File-Setting 如图,取消勾选,点击确认后重启,点击了以后等一会就可以正常显示 ps:下面看下解决IDEA 2020.1.1 找不到程序包和符号 问题描述 IDEA 2020.1.1 maven项目build的时候报错,找2020-06-28
-
springboot + rabbitmq 如何实现消息确认机制(踩坑经验)
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 最近部门号召大伙多组织一些技术分享会,说是要活跃公司的技术氛围,但早就看穿一切的我知道,这 T M 就是为了刷2020-07-01
-
JetBrains IntelliJ IDEA 2020安装与使用教程详解
对于JetBrains IntelliJ IDEA 2020的认识 IntelliJ IDEA 2020是一款JAVA编程软件,捷克IntelliJ公司研发推出。该软件提供了一个非常强大的JAVA集成开发环境,不仅添加了对Records的完整代码洞察支持,2020-06-28