java怎么实现评论和回复功能
效果展示
总共是两层回复 (回复评论、回复评论下的回复)
数据库设计
评论表(TFW_Comments)和回复内容表(TFW_UserResponse)以及评论回复关系表(TFW_MsgRelation)
数据库设计思路:
注:各位读者自动忽略评论表的服务机构ID字段,这个字段相当于这条评论是在哪个帖子(文章下面)
1、根据文章ID或者是帖子ID查询评论表获取评论(本文的服务机构ID)。第一层(评论)
获取第二层回复(commentsId),需要根据评论ID并且回复类型等于1,在关系表中查询。第二层(评论下的回复)
根据评论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怎么实现评论和回复功能的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处
