java評論、回復功能設計與實現(xiàn)方法
最近實現(xiàn)了評論和回復、點贊、@的功能。在這里分享一下我的設計思路(先分享評論和回復功能)。希望各位讀者給出一些不一樣的建議后期改進。
效果展示

總共是兩層回復 (回復評論、回復評論下的回復)
數(shù)據(jù)庫設計
評論表(TFW_Comments)和回復內容表(TFW_UserResponse)以及評論回復關系表(TFW_MsgRelation)

數(shù)據(jù)庫設計思路:
注:各位讀者自動忽略評論表的服務機構ID字段,這個字段相當于這條評論是在哪個帖子(文章下面)
1、根據(jù)文章ID或者是帖子ID查詢評論表獲取評論(本文的服務機構ID)。第一層(評論)
2、根據(jù)評論ID并且回復類型等于1的去關系表獲取第二層的回復(commentsId)。第二層(評論下的回復)
3、根據(jù)評論ID、回復類型等于2、回復ID去關系表獲取第三層回復。第三層(評論下回復中的回復)注:回復ID是它的上級
實現(xiàn)類源碼
@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);
//查詢點贊次數(shù)
int countTag = 0;
MsgRelationTag msgRelationTag = new MsgRelationTag();
for (Map item : info){
item.put("inputShow",false);
int commentsId = (int) item.get("commentsId");
//查詢點贊次數(shù)
countTag = msgRelationDao.findCountTagByTagId(commentsId,1);
item.put("countTag",countTag);
//設置點贊狀態(tài)
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);
}
//二級回復數(shù)據(jù)
List<Map<String, Object>> twoReply = new ArrayList<>();
//所有數(shù)據(jù)
List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
for (Map userResponseInfo :userResponse){
int userResponseIds = Integer.parseInt(userResponseInfo.get("userResponseId").toString());
//查詢點贊次數(shù)
countTag = msgRelationDao.findCountTagByTagId(userResponseIds,2);
//設置點贊狀態(tài)
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); //二級回復數(shù)據(jù)
for (Map twoReplyItem : twoReply){
int twoReplyId = Integer.parseInt(twoReplyItem.get("userResponseId").toString());
twoReplyItem.put("inputShow",false);
//查詢點贊次數(shù)
countTag = msgRelationDao.findCountTagByTagId(twoReplyId,2);
twoReplyItem.put("countTag",countTag);
//設置點贊狀態(tài)
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);上圖根據(jù)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);
上圖根據(jù)commentsid獲取評論下的回復。(根據(jù)評論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); //二級回復數(shù)據(jù)
上圖是根據(jù)評論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評論、回復功能設計與實現(xiàn)方法的文章就介紹到這了,更多相關java評論回復功能內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗功能
這篇文章主要介紹了Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Intellij無法創(chuàng)建java文件解決方案
這篇文章主要介紹了Intellij無法創(chuàng)建java文件解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10

