可以做本地生活服務的有哪些網(wǎng)站營銷型網(wǎng)站建設服務
顯示評論
數(shù)據(jù)庫
entity_type代表評論的目標類型,評論帖子和評論評論
entity_id代表評論的目標id,具體是哪個帖子/評論
targer_id代表評論指向哪個人
entity
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;}
mapper
根據(jù)實體查詢一頁評論數(shù)據(jù)
根據(jù)實體查詢評論的數(shù)量(為了分頁)
@Mapper
public interface CommentMapper {List<Comment> selectCommentsByEntity(int entityType, int entityId, int offset, int limit);int selectCountByEntity(int entityType, int entityId);}
<select id="selectCommentsByEntity" resultType="Comment">select <include refid="selectFields"></include>from commentwhere status = 0and entity_type = #{entityType}and entity_id = #{entityId}order by create_time asclimit #{offset}, #{limit}</select><select id="selectCountByEntity" resultType="int">select count(id)from commentwhere status = 0and entity_type = #{entityType}and entity_id = #{entityId}</select>
service
處理查詢評論的業(yè)務
處理查詢評論數(shù)量的業(yè)務
@Service
public class CommentService implements CommunityConstant {@Autowiredprivate 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);}}
controller
查詢回復,查詢回復數(shù)量是在查看帖子的時候進行的,所以修改discussPostController里面的getDiscussPost函數(shù)就可以
顯示帖子詳細數(shù)據(jù)時,同時顯示該帖子所有的評論數(shù)據(jù)
@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列表,VO-》view objectList<Map<String, Object>> commentVoList = new ArrayList<>();if (commentList != null) {for (Comment comment : commentList) {// 評論VOMap<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);// 回復數(shù)量int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId());commentVo.put("replyCount", replyCount);commentVoList.add(commentVo);}}model.addAttribute("comments", commentVoList);return "/site/discuss-detail";}
前端
index
<ul class="d-inline float-right"><li class="d-inline ml-2">贊 11</li><li class="d-inline ml-2">|</li><li class="d-inline ml-2">回帖 <span th:text="${map.post.commentCount}">7</span></li>
</ul>
?discuss-detail
這個改的有點多,但是估計不會問前端的東西
增加評論
mapper
增加評論數(shù)據(jù)
@Mapper
public interface CommentMapper {int insertComment(Comment comment);}
<insert id="insertComment" parameterType="Comment">insert into comment(<include refid="insertFields"></include>)values(#{userId},#{entityType},#{entityId},#{targetId},#{content},#{status},#{createTime})</insert>
?修改帖子的評論數(shù)量
@Mapper
public interface DiscussPostMapper {int updateCommentCount(int id, int commentCount);}
<update id="updateCommentCount">update discuss_post set comment_count = #{commentCount} where id = #{id}</update>
service
處理添加評論的業(yè)務
CommentService
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)public int addComment(Comment comment) {if (comment == null) {throw new IllegalArgumentException("參數(shù)不能為空!");}// 添加評論comment.setContent(HtmlUtils.htmlEscape(comment.getContent()));comment.setContent(sensitiveFilter.filter(comment.getContent()));int rows = commentMapper.insertComment(comment);// 更新帖子評論數(shù)量if (comment.getEntityType() == ENTITY_TYPE_POST) {int count = commentMapper.selectCountByEntity(comment.getEntityType(), comment.getEntityId());discussPostService.updateCommentCount(comment.getEntityId(), count);}return rows;}
先增加評論,再更新帖子的評論數(shù)量
DiscussPostService
public int updateCommentCount(int id, int commentCount) {return discussPostMapper.updateCommentCount(id, commentCount);}
controller
處理添加評論數(shù)據(jù)的請求
設置添加評論的表單
@Controller
@RequestMapping("/comment")
public class CommentController {@Autowiredprivate CommentService commentService;@Autowiredprivate HostHolder hostHolder;@RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment) {comment.setUserId(hostHolder.getUser().getId());comment.setStatus(0);comment.setCreateTime(new Date());commentService.addComment(comment);return "redirect:/discuss/detail/" + discussPostId;}}
前端
評論
<!-- 回帖輸入 --><div class="container mt-3"><form class="replyform" method="post" th:action="@{|/comment/add/${post.id}|}"><p class="mt-3"><a name="replyform"></a><textarea placeholder="在這里暢所欲言你的看法吧!" name="content"></textarea><input type="hidden" name="entityType" value="1"><input type="hidden" name="entityId" th:value="${post.id}"></p><p class="text-right"><button type="submit" class="btn btn-primary btn-sm"> 回 帖 </button></p></form></div>
回復
<!-- 回復輸入框 --><li class="pb-3 pt-3"><form method="post" th:action="@{|/comment/add/${post.id}|}"><div><input type="text" class="input-size" name="content" placeholder="請輸入你的觀點"/><input type="hidden" name="entityType" value="2"><input type="hidden" name="entityId" th:value="${cvo.comment.id}"></div><div class="text-right mt-2"><button type="submit" class="btn btn-primary btn-sm" onclick="#"> 回 復 </button></div></form></li>
<div th:id="|huifu-${rvoStat.count}|" class="mt-4 collapse"><form method="post" th:action="@{|/comment/add/${post.id}|}"><div><input type="text" class="input-size" name="content" th:placeholder="|回復${rvo.user.username}|"/><input type="hidden" name="entityType" value="2"><input type="hidden" name="entityId" th:value="${cvo.comment.id}"><input type="hidden" name="targetId" th:value="${rvo.user.id}"></div><div class="text-right mt-2"><button type="submit" class="btn btn-primary btn-sm" onclick="#"> 回 復 </button></div></form></div>