栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

3-显示评论

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

3-显示评论

显示评论
    • 1、功能需求
    • 2、util
    • 3、entity包
    • 4、dao层
    • 5、service层
    • 6、controller层

1、功能需求

2、util

CommunityConstant类中添加实体类型: 帖子,实体类型: 评论两个字段。

package com.gerrard.community.util;

public interface CommunityConstant {

    
    int ACTIVATION_SUCCESS = 0;

    
    int ACTIVATION_REPEAT = 1;

    
    int ACTIVATION_FAILURE = 2;

    
    int DEFAULT_EXPIRED_SECONDS = 3600 * 12;

    
    int REMEMBER_EXPIRED_SECONDS = 3600 * 24 * 100;

    
    int ENTITY_TYPE_POST = 1;

    
    int ENTITY_TYPE_COMMENT = 2;

}


3、entity包
package com.gerrard.community.entity;

import java.util.Date;

public class Comment {

    private int id;
    private int userId;
    private int entityType;
    private int entityId;
    private int targetId;
    private String content;
    private int status;
    private Date createTime;

}


comment MySQL数据库:

4、dao层
package com.gerrard.community.dao;

import com.gerrard.community.entity.Comment;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface CommentMapper {

    List selectCommentsByEntity(int entityType, int entityId, int offset, int limit);

    int selectCountByEntity(int entityType, int entityId);

}






    
        id, user_id, entity_type, entity_id, target_id, content, status, create_time
    

    
        select count(id)
        from comment
        where status = 0
        and entity_type = #{entityType}
        and entity_id = #{entityId}
    



5、service层
@Service
public class CommentService {

    @Autowired
    private CommentMapper commentMapper;

    public List findCommentsByEntity(int entityType, int entityId, int offset, int limit) {
        return commentMapper.selectCommentsByEntity(entityType, entityId, offset, limit);
    }

    public int findCommentCount(int entityType, int entityId) {
        return commentMapper.selectCountByEntity(entityType, entityId);
    }

}


6、controller层
@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @RequestMapping(path = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String addDiscussPost(String title, String content) {
        User user = hostHolder.getUser();
        if (user == null) {
            return CommunityUtil.getJSONString(403, "你还没有登录哦!");
        }

        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        post.setTitle(title);
        post.setContent(content);
        post.setCreateTime(new Date());
        discussPostService.addDiscussPost(post);

        // 报错的情况,将来统一处理.
        return CommunityUtil.getJSONString(0, "发布成功!");
    }

    @RequestMapping(path = "/detail/{discussPostId}", method = RequestMethod.GET)
    public String getDiscussPost(@PathVariable("discussPostId") int discussPostId, Model model, Page page) {
        // 帖子
        DiscussPost post = discussPostService.findDiscussPostById(discussPostId);
        model.addAttribute("post", post);
        // 作者
        User user = userService.findUserById(post.getUserId());
        model.addAttribute("user", user);

        // 评论分页信息
        page.setLimit(5);
        page.setPath("/discuss/detail/" + discussPostId);
        page.setRows(post.getCommentCount());

        // 评论: 给帖子的评论
        // 回复: 给评论的评论
        // 评论列表
        List commentList = commentService.findCommentsByEntity(
                ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
        // 评论VO列表
        List> commentVoList = new ArrayList<>();
        if (commentList != null) {
            for (Comment comment : commentList) {
                // 数据增强
                // 评论VO
                Map commentVo = new HashMap<>();
                // 评论
                commentVo.put("comment", comment);
                // 查询到评论的作者
                commentVo.put("user", userService.findUserById(comment.getUserId()));

                // 回复列表  复用查询评论与回复
                List replyList = commentService.findCommentsByEntity(
                        ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
                // 回复VO列表
                List> replyVoList = new ArrayList<>();
                if (replyList != null) {
                    for (Comment reply : replyList) {
                        Map replyVo = new HashMap<>();
                        // 回复
                        replyVo.put("reply", reply);
                        // 作者
                        replyVo.put("user", userService.findUserById(reply.getUserId()));
                        // 回复目标
                        User target = reply.getTargetId() == 0 ? null : userService.findUserById(reply.getTargetId());
                        replyVo.put("target", target);

                        replyVoList.add(replyVo);
                    }
                }
                commentVo.put("replys", replyVoList);

                // 回复数量
                int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());
                commentVo.put("replyCount", replyCount);

                commentVoList.add(commentVo);
            }
        }

        model.addAttribute("comments", commentVoList);

        return "/site/discuss-detail";
    }

}


第一类:针对帖子的评论;

第二类:针对评论的评论,但其targetId为0;

第三类:针对评论的评论,其targetId不为0。

先从comment MySQL数据库中查到分页条件下,指定帖子的“针对帖子的评论”集合commentList,接着遍历commentList,对commentList中的Comment元素,进行数据增强,查到评论的作者,再查到评论附带的回复集合,回复的数量,添加到各自Map集合中。

查到评论附带的回复集合:先查到回复列表,再进行数据增强:遍历回复列表,添加回复作者实体,回复目标用户实体(如果target_id为0则目标用户实体为空)。

最后将查到的信息相应给前端,返回discuss-detail.html页面。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032642.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号