当前位置:网站首页>6、显示评论和回复
6、显示评论和回复
2022-07-31 02:36:00 【A snicker】

1、实体类:comment
@Data
@AllArgsConstructor
@NoArgsConstructor
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;
}
2、数据层
CommentMapper接口
@Mapper
public interface CommentMapper {
List<Comment> selectCommentsByEntity(int entityType, int entityId, int offset, int limit);
int selectCountByEntity(int entityType, int entityId);
}
3、业务层
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
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);
}
}
4、表现层
DiscussPostController
帖子详情页,有评论,评论中有回复
注入page,post,user,comments
@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<Comment> commentList = commentService.findCommentsByEntity(
ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());
// 评论显示vo列表
List<Map<String, Object>> commentVoList = new ArrayList<>();
if(commentList != null){
for (Comment comment : commentList) {
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";
}
5、增加评论(更新评论数量,事务管理)
事务管理,声明式配置(整个),编程式(部分),这里采用声明式配置。@Transactional(隔离级别、传播机制)
@Transactional(
isolation = Isolation.READ_COMMITTED,
propagation = Propagation.REQUIRED,
rollbackFor = Exception.class)
public int addComment(Comment comment){
if(comment == null){
throw new IllegalArgumentException("参数不能为空!");
}
// 添加评论
comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));
comment.setContent(sensitiveFilter.filter(comment.getContent()));
int rows = commentMapper.insertComment(comment);
// 更新帖子数量
if(comment.getEntityType() == ENTITY_TYPE_POST){
int count =
commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
discussPostService.updateCommentCount(comment.getEntityId(), count);
}
return rows;
}
边栏推荐
- General introduction to the Unity interface
- Drools WorkBench的简介与使用
- BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
- STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
- 【Bank Series Phase 1】People's Bank of China
- 数学解决——环形链表问题
- mmdetection trains a model related command
- Force buckled brush the stairs (7/30)
- JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
- 基于FPGA的图像实时采集
猜你喜欢

知识蒸馏7:知识蒸馏代码详解

What level of software testing does it take to get a 9K job?

Observer mode (1)

用户交互+格式化输出

你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?

怎样做好一个创业公司CTO?

Real-time image acquisition based on FPGA

二层广播风暴(产生原因+判断+解决)

Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph

SQL注入 Less54(限制次数的SQL注入+union注入)
随机推荐
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue
Manchester City confuses fans with smart scarf that detects emotions
STP选举(步骤+案列)详解
The difference between link and @import
Pythagorean tuple od js
mysql index
Introduction to flask series 】 【 flask - using SQLAlchemy
【银行系列第一期】中国人民银行
如何搭建私有yum源
mysql 索引
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
f.grid_sample
【shell基础】判断目录是否为空
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
SQL注入 Less46(order by后的注入+rand()布尔盲注)
力扣刷题之有效的正方形(每日一题7/29)
完整复制虚拟机原理(云计算)
12 磁盘相关命令
The application of AI in the whole process of medical imaging equipment