当前位置: 首页 > news >正文

java评论、回复功能设计和实现

最近实现了评论和回复、点赞、@的功能。在这里分享一下我的设计思路(先分享评论和回复功能)。希望各位读者给出一些不一样的建议后期改进。

效果展示

总共是两层回复 (回复评论、回复评论下的回复)

数据库设计

评论表(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开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

小编已加密:aHR0cHM6Ly9kb2NzLnFxLmNvbS9kb2MvRFVrVm9aSGxQZUVsTlkwUnc==出于安全原因,我们把网站通过base64编码了,大家可以通过base64解码把网址获取下来。

相关文章:

  • IPIDEA的使用方式
  • 【机器学习笔记】【决策树】【回归树】
  • IDEA版Postman插件Restful Fast Request,细节到位,功能好用
  • jmeter-12-Jenkins持续集成测试一键式使用
  • 9月10日OpenCV学习笔记——Mask、彩色直方图、人脸检测
  • 【JavaWeb】一篇文章复习JDBC、DAO及相关实现类
  • java计算机毕业设计ssm 音乐播放交流论坛网站
  • GO开发环境配置
  • Docker-compose安装mysql
  • 字符函数和字符串函数(C语言)
  • zynq pl访问ps ddr
  • JavaEE初阶:HTML
  • IDEA中JDBC连接MYSQL数据库步骤超详细总结
  • docker 开启 nginx 容器
  • 109 使用Ajax传递请求本地数据库
  • 【笔记】你不知道的JS读书笔记——Promise
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • 2017 年终总结 —— 在路上
  • C++类的相互关联
  • CSS3 聊天气泡框以及 inherit、currentColor 关键字
  • C语言笔记(第一章:C语言编程)
  • github指令
  • Javascript编码规范
  • JSONP原理
  • Linux各目录及每个目录的详细介绍
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • Python 使用 Tornado 框架实现 WebHook 自动部署 Git 项目
  • python大佬养成计划----difflib模块
  • Shadow DOM 内部构造及如何构建独立组件
  • Spring Security中异常上抛机制及对于转型处理的一些感悟
  • 不上全站https的网站你们就等着被恶心死吧
  • 工作手记之html2canvas使用概述
  • 工作中总结前端开发流程--vue项目
  • 技术:超级实用的电脑小技巧
  • 前端之Sass/Scss实战笔记
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 为什么要用IPython/Jupyter?
  • 我从编程教室毕业
  • 小程序开发中的那些坑
  • 一天一个设计模式之JS实现——适配器模式
  • 湖北分布式智能数据采集方法有哪些?
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • ###项目技术发展史
  • #include<初见C语言之指针(5)>
  • #Linux(帮助手册)
  • #大学#套接字
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (39)STM32——FLASH闪存
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (Redis使用系列) Springboot 使用redis的List数据结构实现简单的排队功能场景 九
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (接口封装)
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析