功能需求及处理策略1.dao层处理相关数据
增加评论数据
接口方法声明sql编写 更新帖子的评论数量
接口方法声明sql编写 2. Service处理-事务管理3. Controller层处理添加请求4. View层模板页面处理
4.1 对帖子的回复
RestFul风格路径动态拼接==placeholder====隐藏域====submit== 4.2 对评论的一级回复
同样,用表单的post请求同样使用隐藏框向服务器提交请求数据 4.3 对回复的回复
回复框的层次动态调整同样,设定post请求表单要对指定targetUser回复,提示框内容动态调整同样,使用隐藏框向服务器提交数据 测试结果
功能需求及处理策略参考牛客网高级项目教程
能够给帖子进行评论-即新增评论能够给评论进行回复-即新增一级回复能够给指定的回复进行回复-并能指明向哪个用户的回复 1.dao层处理相关数据
由于,在帖子的数据库表设计时,增加了冗余字段:评论数量因此,每增加一条帖子的评论,都要更新帖子数据库的评论数量
因此,在业务层要增加事务处理 增加评论数据 接口方法声明
int insertComment(Comment comment);sql编写
insert into comment(user_id, entity_type, entity_id, target_id, content, status, create_time) values (#{userId}, #{entityType}, #{entityId}, #{targetId}, #{content}, #{status}, #{createTime})
更新帖子的评论数量
接口方法声明
int updateCommentCount(int id, int commentCount);sql编写
update discuss_post set comment_count = #{commentCount} where id = #{id}
2. Service处理-事务管理
先执行添加评论的业务添加后,要及时更新帖子的评论数量对这个两个操作添加进一个事务,进行管理,要么都成功,要么失败一个就回滚注意:只更新帖子的评论数量,评论的评论不包括
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public int addComment(Comment comment) {
// 先添加评论
// -注意过滤敏感词
// -业务层,只处理接受来的原材料进行加工入库和查询,如何创造原材料是上级的逻辑
if(comment == null) {
throw new IllegalArgumentException("参数不能为空!");
}
comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
comment.setContent(sensitiveFilter.filter(comment.getContent()));
int res = commentMapper.insertComment(comment);
// 在一个事务中,进行更新帖子评论
// 只更新帖子的评论数量,评论的评论不包括
if(comment.getEntityType() == ENTITY_TYPE_POST) {
discussPostMapper.updateCommentCount(comment.getEntityId(),
commentMapper.selectComments(ENTITY_TYPE_POST, comment.getEntityId()));
}
return res;
}
3. Controller层处理添加请求
直接用Comment类接受参数,参数中的标签name属性名要和类中的属性名一致即可自动接受
还要对Comment类做一些其他基本设置
@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {
@Autowired
CommentService commentService;
@Autowired
HostHolder hostHolder;
@RequestMapping(value = "/add/{discussPostId}", method = RequestMethod.POST)
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {
// 除请求中需要写的评论内容外,需提供其他素材
comment.setCreateTime(new Date());
comment.setUserId(hostHolder.getUser().getId());
comment.setStatus(0);
return "redirect:/discuss/detail/" + discussPostId;
}
}
4. View层模板页面处理
4.1 对帖子的回复
RestFul风格路径动态拼接
可以直接用访问帖子详情页面的请求中传入的model数据-post



