【SpringBoot仿牛客网】05、显示评论

【SpringBoot仿牛客网】05、显示评论,第1张

【SpringBoot仿牛客网】05、显示评论

中间有些功能由于不是核心功能所以跳过了没有实现~
具体可参考:【Spring Boot论坛项目实战】3、开发社区核心功能

文章目录
  • 0 需求分析
  • 1 数据访问层
    • 1.1 表
    • 1.2 写实体类
    • 1.3 写Mapper接口
    • 1.4 写Mapper对应的xml
    • 1.5 测试
  • 2 业务层
    • 2.1 需求分析
    • 2.2 写代码
  • 3 视图层
    • 3.1 需求分析
    • 3.2 写代码
  • 4 动态页面

0 需求分析

现在帖子下面的回复都是静态页面写死的,需要根据数据库查到的不同内容动态显示回帖

1 数据访问层 1.1 表

先对数据库有个了解:comment表

id:主键
user_id:发表评论的用户
entity_type:实体类型,可以是对帖子的评论、对评论的评论、对课程的评论、对题目的评论等
entity_id:该实体的id
target_id:该评论指向的人(注意不是实体),就像图里红框所示,虽然这条评论是对评论的评论(实体类型是评论),但是这条评论也是有指向一个人的(图里是sissi)

content:评论内容
status:状态,0代表正常,1代表被删除
create_time:评论时间

1.2 写实体类

类名与表名一致(这里与下面的“一致”是内容一致,命名规范不同,如comment表的实体类类名叫Comment)
属性与表列名一致、get/set方法、toString

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 createDate;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getEntityType() {
        return entityType;
    }

    public void setEntityType(int entityType) {
        this.entityType = entityType;
    }

    public int getEntityId() {
        return entityId;
    }

    public void setEntityId(int entityId) {
        this.entityId = entityId;
    }

    public int getTargetId() {
        return targetId;
    }

    public void setTargetId(int targetId) {
        this.targetId = targetId;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }


    @Override
    public String toString() {
        return "Comment{" +
                "id=" + id +
                ", userId=" + userId +
                ", entityType=" + entityType +
                ", entityId=" + entityId +
                ", targetId=" + targetId +
                ", content='" + content + ''' +
                ", status=" + status +
                ", createDate=" + createDate +
                '}';
    }
}

1.3 写Mapper接口

1.注解@Mapper
2.写方法声明

首先帖子评论跟首页的帖子一样,要分页展示的,所以需要一个根据offset和limit查找帖子列表的方法。查找评论都要通过实体类型和实体id确定是给哪个实体评论的,再查找

分页需要知道一共多少条评论

添加评论的方法

@Mapper
public interface CommentMapper {
    List selectCommentsByEntity(int entityType, int entityId, int offset, int limit);

    int selectCountByEntity(int entityType, int entityId);

    int insertComment(Comment comment);
}
1.4 写Mapper对应的xml

头与sql语法都在官网:https://mybatis.org/mybatis-3/zh/getting-started.html
1.头
namespace写mapper路径





2.sql语法

  • #{}里写mapper方法里传入的参数,其他就是普通sql语句
  • id是mapper方法名
  • resultType是mapper返回类型,简单数据类型可不写,自定义数据类型(我不写了也没事,但最好写)
  • parameterType是mapper传入参数的类型,简单数据类型可不写,自定义数据类型(我不写了也没事,但最好写)



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

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

    
        select count(id) from comment
        where entity_type=#{entityType} and entity_id=#{entityId} and status=0
    
    
    
        insert into comment ()
        values(#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})
    

1.5 测试
    @Autowired
    private CommentMapper commentMapper;
    @Test
    public void testComment(){
        List list = commentMapper.selectCommentsByEntity(1,228,1,5);
        for (Comment comment:list){
            System.out.println(comment);
        }
        System.out.println("------------------------");

        System.out.println(commentMapper.selectCountByEntity(1,228));
        System.out.println("------------------------");

        Comment comment = new Comment();
        comment.setStatus(0);
        comment.setUserId(1);
        comment.setContent("你好");
        comment.setCreateTime(new Date());
        comment.setEntityId(1);
        comment.setEntityType(1);
        commentMapper.insertComment(comment);
        System.out.println("------------------------");
    }



成功

2 业务层 2.1 需求分析

1.总共有多少条回帖
2.分页显示回帖
3.某条评论有多少条评论

2.2 写代码

1.@Service注解
2.注入相应Mapper

@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;

    //一共有多少条回复。适用于帖子和评论
    public int findCommentCount(int entityType, int entityId){
        return commentMapper.selectCountByEntity(entityType, entityId);
    }

    //查找范围内的回复,适用于分页
    public List findCommentsByEntity(int entityType, int entityId, int offset, int limit){
        return commentMapper.selectCommentsByEntity(entityType, entityId, offset, limit);
    }
}
3 视图层 3.1 需求分析 3.2 写代码

由于显示评论的页面是帖子详情页面,有对应的Controller,就在该Controller里写

@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.getStartRow(), 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";
    }
4 动态页面

1.处理分页

复用index的分页

2.将其他需要动态展示的改为引擎模板

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5637798.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存