当前位置:网站首页>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;
}
边栏推荐
- 1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance
- 【Bank Series Phase 1】People's Bank of China
- 12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
- 数学解决——环形链表问题
- Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
- StringJoiner详解
- Brute Force/Adjacency List Breadth First Directed Weighted Graph Undirected Weighted Graph
- AtCoder Beginner Contest 261 部分题解
- General introduction to the Unity interface
- TCP/IP四层模型
猜你喜欢

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

The real CTO is a technical person who understands products

系统需求多变如何设计

tcp框架需要解决的问题

12 磁盘相关命令

coldfusion8 background scheduled tasks take shell

The Sad History of Image Processing Technology

英特尔软硬优化,赋能东软加速智慧医疗时代到来
![The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]](/img/8a/28427aa773e46740eda9e95f6669f2.png)
The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]

Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
随机推荐
Drools规则属性,高级语法
【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
【Android】Room —— SQLite的替代品
系统需求多变如何设计
How to design the changing system requirements
开题报告之论文框架
The modification is not properly placed in the sandbox, causing Apple compatibility issues
221. Largest Square
mmdetection训练一个模型相关命令
mysql 视图
Static routing + PAT + static NAT (explanation + experiment)
Can an inexperienced college graduate switch to software testing?my real case
基于FPGA的售货机
The principle of complete replication of virtual machines (cloud computing)
execsnoop 工具
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
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
如何搭建私有yum源
Intranet Infiltration - Privilege Escalation
Software Testing Defect Reporting - Definition, Composition, Defect Lifecycle, Defect Tracking Post-Production Process, Defect Tracking Process, Purpose of Defect Tracking, Defect Management Tools