当前位置:网站首页>6. Display comments and replies
6. Display comments and replies
2022-07-31 02:40: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
帖子详情页,有评论,Reply in comments
注入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、增加评论(更新评论数量,事务管理)
事务管理,声明式配置(整个),编程式(部分),Declarative configuration is used [email protected](隔离级别、传播机制)
@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);
// Update the number of posts
if(comment.getEntityType() == ENTITY_TYPE_POST){
int count =
commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());
discussPostService.updateCommentCount(comment.getEntityId(), count);
}
return rows;
}
边栏推荐
- The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]
- 8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
- Installation, start and stop of redis7 under Linux
- execsnoop 工具
- 221. Largest Square
- 你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
- 分布式与集群是什么 ? 区别是什么?
- golang GUI for nuxui — HelloWorld
- 图解lower_bound&upper_bound
- 7. List of private messages
猜你喜欢
Unity界面总体介绍
SQL注入 Less46(order by后的注入+rand()布尔盲注)
StringJoiner详解
11、Redis实现关注、取消关注以及关注和粉丝列表
f.grid_sample
Introduction to flask series 】 【 flask - using SQLAlchemy
Installation, start and stop of redis7 under Linux
SQL注入 Less54(限制次数的SQL注入+union注入)
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
FPGA-based vending machine
随机推荐
Installation, start and stop of redis7 under Linux
coldfusion8 background scheduled tasks take shell
Mathematical Ideas in AI
STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
LeetCode 每日一题 2022/7/25-2022/7/31
真正的CTO,是一个懂产品的技术人
Calculate S=a+aa+…+aa…a
如何搭建私有yum源
Drools basic introduction, introductory case, basic syntax
How to build a private yum source
CMOS和TTL的区别?
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
软件积累 -- 截图软件ScreenToGif
Why is String immutable?
The effective square of the test (one question of the day 7/29)
Verify the integer input
图解lower_bound&upper_bound
Manchester City confuses fans with smart scarf that detects emotions
AI中的数学思想