当前位置:网站首页>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;
}
边栏推荐
- mmdetection训练一个模型相关命令
- General introduction to the Unity interface
- Clustering index, and what is the difference between a clustering index
- StringJoiner详解
- How to build a private yum source
- Basic introduction to ShardingJDBC
- Detailed explanation of STP election (step + case)
- Difference between CMOS and TTL?
- Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
- mysql index
猜你喜欢

11、Redis实现关注、取消关注以及关注和粉丝列表

Detailed explanation of STP election (step + case)

SQL注入 Less46(order by后的注入+rand()布尔盲注)

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

CentOS7下mysql5.7.37的安装【完美方案】

FPGA-based vending machine

10、Redis实现点赞(Set)和获取总点赞数

7. List of private messages

19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing

Coldfusion file read holes (CVE - 2010-2861)
随机推荐
拒绝加班,程序员开发的效率工具集
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
Modbus on AT32 MCU
Discourse Custom Header Links
共模电感的仿真应用来了,满满的干货送给大家
MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi
Teach you how to configure Jenkins automated email notifications
Coldfusion file read holes (CVE - 2010-2861)
mmdetection trains a model related command
医疗影像领域AI软件开发流程
7. List of private messages
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
TCP/IP四层模型
coldfusion8 background scheduled tasks take shell
BAT卖不动「医疗云」:医院逃离、山头林立、行有行规
什么是分布式锁?实现分布式锁的三种方式
图解lower_bound&upper_bound
Draw Your Cards
Force buckled brush the stairs (7/30)
Intranet Infiltration - Privilege Escalation