How to implement comment and reply functions in java
Effect display
There are two levels of reply in total (reply to comments, reply to replies under comments)
Database design
Comments Table (TFW_Comments), reply content table (TFW_UserResponse) and comment reply relationship table (TFW_MsgRelation)
Database design ideas:
Note: Readers automatically ignore the service organization ID field of the comment table. This field is equivalent to which post this comment is in (below the article)
1. Query the comment table based on the article ID or post ID. Get the comment (service ID for this article). First level (comments)
To obtain the second level reply (commentsId), you need to query in the relationship table based on the comment ID and the reply type equals 1. The second level (reply under the comment)
Get the third level reply information in the relationship table based on the comment ID, reply type 2, and reply ID. The third level (reply in the reply under the comment) Note: The reply ID is its superior
Implementation class source code
@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; }
Other codes can be ignored. The main statements are:
Get the comments under the post
List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null);
The above picture gets the comments based on FWJLID. (This can be used as the ID of the post to get the comments under the post) Level 1 display
corresponds to the SQL statement (OPT is my user table)
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
Query results:
Get the reply under the comment
List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
The above picture gets the reply under the comment based on commentsid. (Get the reply based on the comment ID) Second-level display
Corresponding sql statement
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
Query result
Get the second-level reply
twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据
The above picture is to obtain the secondary reply based on the comment ID (msgRelationId) and reply ID (userResponseId). The reply ID is also the parent class. It is the ID of the reply. The third layer display
corresponds to 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
query result
The above is the detailed content of How to implement comment and reply functions in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is
