> Java > java지도 시간 > 본문

Java에서 댓글 및 응답 기능을 구현하는 방법

WBOY
풀어 주다: 2023-05-25 14:46:22
앞으로
3288명이 탐색했습니다.

효과 표시

Java에서 댓글 및 응답 기능을 구현하는 방법

답글에는 총 2단계가 있습니다(댓글에 대한 답변, 댓글 아래의 답변에 대한 답변)

데이터베이스 디자인

댓글 테이블(TFW_Comments)과 댓글 내용 테이블(TFW_UserResponse) 및 댓글 답변 관계 테이블 (TFW_MsgRelation)

Java에서 댓글 및 응답 기능을 구현하는 방법

Java에서 댓글 및 응답 기능을 구현하는 방법

데이터베이스 디자인 아이디어:

참고: 독자는 댓글 테이블의 서비스 조직 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+&#39;@&#39;+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+&#39;@&#39;+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로 사용되어 게시물 아래의 댓글을 가져올 수 있습니다.) 레벨 1 표시

는 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
로그인 후 복사

쿼리 결과:

Java에서 댓글 및 응답 기능을 구현하는 방법

응답 받기 under the comment

List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
로그인 후 복사

위 사진은 commentsid를 기준으로 댓글 아래의 답글을 가져옵니다. (댓글 ID를 기준으로 응답 가져오기) 2차 디스플레이

는 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
로그인 후 복사

에 해당합니다. 쿼리 결과

Java에서 댓글 및 응답 기능을 구현하는 방법

2차 응답 가져오기

 twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据
로그인 후 복사

위 그림은 2차 응답을 가져오는 것입니다. -댓글 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에서 댓글 및 응답 기능을 구현하는 방법

Java에서 댓글 및 응답 기능을 구현하는 방법

에 해당합니다.

위 내용은 Java에서 댓글 및 응답 기능을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!