数据层:
(1)根据实体查询一页评论数据。
(2)根据实体查询评论的数量。
业务层:
(1)处理查询评论的业务。
(2)处理查询评论数量的业务。
表现层:
(1)显示帖子详情数据时,同时显示该帖子所有的评论数据。
在entity包下新建Comment类。在dao包下新建CommentMapper接口类。在resources->mapper下新建comment-mapper.xml,添加
<sql id="selectFields">
id,user_id,entity_type,entity_id,target_id,content,status,create_time
</sql>
<select id="selectCommentsByEntity" resultType="Comment">
select <include refid="selectFields"></include>
from comment
where status = 0
and entity_type = #{entityType}
and entity_id = #{entityId}
order by create_time asc
limit #{offset},#{limit} <!--offset起始行、limit最大条目数-->
</select>
<select id="selectCountByEntity" resultType="int">
select count(id)
from comment
where status = 0
and entity_type = #{entityType}
and entity_id = #{entityId} <!--这里因为只查一个整数,所以不需要排序、分页-->
</select>
在service包下新建CommentService类,添加
public List<Comment> 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);
}
在DiscussPostController类里,添加
// 查评论的分页信息
page.setLimit(5);
page.setPath("/discuss/detail/" + discussPostId);
page.setRows(post.getCommentCount());
//评论:给帖子的评论
//回复:给评论的评论
//评论列表
List<Comment> commentList = commentService.findCommentsByEntity(
ENTITY_TYPE_POST,post.getId(),page.getOffset(),page.getLimit());
//评论VO(View Object)列表
List<Map<String,Object>> commentVoList = new ArrayList<>();
if (commentList != null){
for (Comment comment:commentList){
//评论VO
Map<String,Object> commentVo = new HashMap<>();
//评论
commentVo.put("comment",comment);
//作者
commentVo.put("user",userService.findUserById(comment.getUserId()));
//接下来查询【回复】列表(评论的评论)
List<Comment> replyList = commentService.findCommentsByEntity(
ENTITY_TYPE_COMMENT,comment.getId(),0,Integer.MAX_VALUE); //有多少条查多少条,这里就不分页了
//回复VO列表
List<Map<String,Object>> replyVoList = new ArrayList<>();
if(replyList != null){
for(Comment reply:replyList){
Map<String,Object> 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";
}
index.html里把对应的回帖数量(评论数量)动态引用上。在discuss-detailed.html里也把回帖数量、comments系列相关信息。