How to efficiently use java to implement a comment like list (interactive message) similar to Douyin and the basic operation of like

foreword

This article is a like function implemented based on Java, which is a sub-module of the comment function on microservices, and a comment like list similar to Douyin, which can be linked to query the list of comments liked by users and users who have been clicked Like the comment list, which does not use redis and mq middleware for optimization.

Realize the function

Realize adding and canceling likes, judging whether to like, viewing the list of comments that users like and the list of comments that users like, etc.

1. Database design

1. Comment like table database

2. Entity classes and VO objects

1. Entity class

The content of the entity class is the same as that of the database, so it will not be shown here.

2. VO object

 
 

arduino

copy code

public class LikeVO { /** * 点赞表主键 */ Integer likeId; /** * 用户信息 */ UserSimpleVO userInfo; /** * 评论内容 */ String content; /** * 点赞创建时间 */ Timestamp createTime; }

 3. Code implementation

1. Judging whether to like

 
 

perl

copy code

@Override public boolean isLike(Integer userId, Integer commentId) { LambdaQueryWrapper<Like> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Like::getUserId,userId) .eq(Like::getCommentId,commentId); Long count = likeMapper.selectCount(queryWrapper); return count>0; }

2. Add or cancel likes

 
 

This

copy code

@Override public boolean like(Integer userId, Integer commentId) { //lambda查询是否存在点赞记录 LambdaQueryWrapper<Like> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Like::getCommentId,commentId) .eq(Like::getUserId,userId); Like like = likeMapper.selectOne(queryWrapper); //判断点赞记录是否为空,为空则添加,不为空则删除 if(like!=null){ int row = likeMapper.delete(queryWrapper); commentMapper.updateLikeNum(commentId,-1); return row>0; } like = new Like(); like.setCommentId(commentId); like.setUserId(userId); int row = likeMapper.insert(like); //进行评论点赞数的减少 commentMapper.updateLikeNum(commentId,1); return row>0; }

3. Query the record of users being liked and commented

Operations such as ArrayList, HashMap, and stream are used to speed up query efficiency.

Since I only have the id of the user who likes and the comment id in the like table, I first performed the operation of querying all the comments of the user. If there is any optimization method, please comment and teach me.

 
 

This

copy code

@Override public List<LikeVO> likeByCommons(Integer userId) { //查询出该用户的所有评论 LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Comment::getUserId,userId); List<Comment> comments = commentMapper.selectList(queryWrapper); //判断该用户是否有评论,若没有则返回空集合。 if (comments.size() == 0) { return new ArrayList<>(); } //统计这些评论的id List<Integer> commentIds = new ArrayList<>(); //创建评论id和评论内容的映射 Map<Integer,String> commentInfo = new HashMap<>(); for(Comment comment:comments) { commentIds.add(comment.getId()); commentInfo.put(comment.getId(),comment.getContent()); } //查询出点赞关系 LambdaQueryWrapper<Like> likeLambdaQueryWrapper = new LambdaQueryWrapper<>(); likeLambdaQueryWrapper.in(Like::getCommentId,commentIds); likeLambdaQueryWrapper.orderByDesc(Like::getCreateTime); List<Like> likes = likeMapper.selectList(likeLambdaQueryWrapper); //把查出的点赞关系按照时间排序 //likes = likes.stream().sorted(Comparator.comparing(Like::getCreateTime).reversed()).collect(Collectors.toList()); //查出点赞用户信息 Set<Integer> userIds = new HashSet<>(); for(Like like:likes) { userIds.add(like.getUserId()); } List<Integer> userList = new ArrayList<>(userIds); Map<Integer, UserSimpleVO> userInfo = userClient.getUserDeatilInfoMap(userList).getData(); //stream流把comment对象赋值到likeVO中 return likes.stream().map((item) -> { LikeVO likeVO = new LikeVO(); likeVO.setLikeId(item.getLikeId()); likeVO.setContent(commentInfo.get(item.getCommentId())); likeVO.setUserInfo(userInfo.get(item.getUserId())); likeVO.setCreateTime(item.getCreateTime()); return likeVO; }).collect(Collectors.toList()); }

4. Query the list of comments liked by users

 
 

This

copy code

@Override public List<LikeVO> likeCommons(Integer userId) { //查出该用户点赞的所有评论 LambdaQueryWrapper<Like> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Like::getUserId,userId); queryWrapper.orderByDesc(Like::getCreateTime); List<Like> likeList = likeMapper.selectList(queryWrapper); if (likeList.size() == 0) { return new ArrayList<>(); } //查出对应的评论信息 List<Integer> commentIds = new ArrayList<>(); for(Like like:likeList) { commentIds.add(like.getCommentId()); } List<Comment> commentList = commentMapper.selectBatchIds(commentIds); //查询出被点赞评论的用户信息和创建评论关系的映射 Map<Integer,Comment> commentInfo = new HashMap<>(); Set<Integer> userIds = new HashSet<>(); for(Comment comment:commentList) { userIds.add(comment.getUserId()); commentInfo.put(comment.getId(),comment); } List<Integer> userList = new ArrayList<>(userIds); Map<Integer, UserSimpleVO> userInfo = userClient.getUserDeatilInfoMap(userList).getData(); //stream流把comment对象赋值到likeVO中 return likeList.stream().map((item) -> { LikeVO likeVO = new LikeVO(); likeVO.setLikeId(item.getLikeId()); likeVO.setUserInfo(userInfo.get(item.getUserId())); likeVO.setContent(commentInfo.get(item.getCommentId()).getContent()); likeVO.setCreateTime(item.getCreateTime()); return likeVO; }).collect(Collectors.toList()); }

Summarize

This is some functional code sharing when I was writing the project, please point out if there are any deficiencies.

Guess you like

Origin blog.csdn.net/BASK2312/article/details/131359656
Recommended