這篇文章,我來介紹一下我的項目中的另外一個功能:顯示評論和添加評論。
其實這兩個功能都不怎么重要,我感覺最重要的應該是用戶注冊登錄功能,這個也了解一下,知道這么一回事兒就好。
首先設計DAO層。
@Mapper
public interface CommentMapper {List<Comment> selectCommentsByEntity(int entityType, int entityId, int offset, int limit);//根據實體類型、實體ID以及分頁偏移量和限制數量,查詢對應實體的評論列表,并返回一個評論對象的列表int selectCountByEntity(int entityType, int entityId);//根據實體類型和實體ID,查詢對應實體的評論數量,并返回結果int insertComment(Comment comment);//插入一條評論記錄到數據庫中,參數為一個評論對象Comment selectCommentById(int id);//根據評論ID,查詢并返回對應的評論對象}
這些方法用于對評論表進行常見的數據庫操作,例如查詢某個實體的評論列表、查詢評論數量、插入評論以及根據評論ID查詢評論詳情。
?
再來設計service層。
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);}@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)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;}public Comment findCommentById(int id) {return commentMapper.selectCommentById(id);}
上面的代碼是一個評論服務類,用于處理與評論相關的業務邏輯。
findCommentCount()
?方法用于根據實體類型和實體ID,調用?commentMapper.selectCountByEntity()
?方法查詢對應實體的評論數量,并返回結果。
addComment()
?方法用于添加一條評論。在方法中,首先對評論內容進行處理,使用?HtmlUtils.htmlEscape()
?方法進行 HTML 轉義,以防止 XSS 攻擊,然后使用?sensitiveFilter.filter()
?方法過濾敏感詞。接下來調用?commentMapper.insertComment()
?方法將評論插入到數據庫中。
findCommentById()
?方法用于根據評論ID查詢對應的評論對象,調用?commentMapper.selectCommentById()
?方法進行查詢,并返回結果。
這些方法結合了評論數據庫訪問對象(DAO)的方法,處理評論的查詢、添加和統計等業務邏輯,并且在添加評論時還會更新帖子的評論數量。
最后寫controller層。
public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {comment.setUserId(hostHolder.getUser().getId());comment.setStatus(0);comment.setCreateTime(new Date());commentService.addComment(comment);// 觸發評論事件Event event = new Event().setTopic(TOPIC_COMMENT).setUserId(hostHolder.getUser().getId()).setEntityType(comment.getEntityType()).setEntityId(comment.getEntityId()).setData("postId", discussPostId);if (comment.getEntityType() == ENTITY_TYPE_POST) {DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());event.setEntityUserId(target.getUserId());} else if (comment.getEntityType() == ENTITY_TYPE_COMMENT) {Comment target = commentService.findCommentById(comment.getEntityId());event.setEntityUserId(target.getUserId());}eventProducer.fireEvent(event);if (comment.getEntityType() == ENTITY_TYPE_POST) {// 觸發發帖事件event = new Event().setTopic(TOPIC_PUBLISH).setUserId(comment.getUserId()).setEntityType(ENTITY_TYPE_POST).setEntityId(discussPostId);eventProducer.fireEvent(event);// 計算帖子分數String redisKey = RedisKeyUtil.getPostScoreKey();redisTemplate.opsForSet().add(redisKey, discussPostId);}return "redirect:/discuss/detail/" + discussPostId;}}
在方法體中,首先設置評論的用戶ID為當前登錄用戶的ID,評論的狀態為0(表示正常狀態),創建時間為當前時間。然后調用?commentService.addComment()
?方法將評論添加到數據庫中。
接下來,根據評論的實體類型(帖子或評論),創建一個事件對象?event
,設置事件的相關屬性,例如話題(topic)、用戶ID、實體類型、實體ID等。如果評論的實體類型是帖子,則還會獲取該帖子的作者ID,如果是評論,則獲取評論的目標對象(評論的回復對象)的作者ID。
最后,返回一個重定向的視圖,將頁面重定向到帖子詳情頁,以展示添加評論后的頁面效果。
這就是顯示評論和添加評論的功能。簡單了解一下既可!