音乐网站用dw怎么做,兔展,中国移动网站建设怎么做,展开网站建设这篇文章主要讲讲帖子详情功能。其实帖子详情功能简单来说就是你点进去可以看到文章#xff0c;这就叫帖子详情功能。那接下来我讲讲我的这个项目是如何实现这个功能的。 首先写DAO层。
Mapper
public interface DiscussPostMapper {ListDiscussPost selectDiscussPo…这篇文章主要讲讲帖子详情功能。其实帖子详情功能简单来说就是你点进去可以看到文章这就叫帖子详情功能。那接下来我讲讲我的这个项目是如何实现这个功能的。 首先写DAO层。
Mapper
public interface DiscussPostMapper {ListDiscussPost selectDiscussPosts(int userId, int offset, int limit, int orderMode);//查询讨论帖子列表的方法根据给定的用户ID、偏移量、限制数量和排序方式来查询讨论帖子int selectDiscussPostRows(Param(userId) int userId);//查询讨论帖子的总行数的方法根据给定的用户ID来查询该用户发表的讨论帖子总数int insertDiscussPost(DiscussPost discussPost);//插入一条讨论帖子记录的方法将给定的讨论帖子对象插入到数据库中DiscussPost selectDiscussPostById(int id);//根据给定的帖子ID查询讨论帖子的方法返回对应ID的讨论帖子对象int updateCommentCount(int id, int commentCount);//更新讨论帖子的评论数量的方法根据给定的帖子ID更新评论数量int updateType(int id, int type);//更新讨论帖子的类型的方法根据给定的帖子ID更新类型int updateStatus(int id, int status);//更新讨论帖子的状态的方法根据给定的帖子ID更新状态int updateScore(int id, double score);//更新讨论帖子的评分的方法根据给定的帖子ID更新评分} 再来写Service层。 PostConstructpublic ListDiscussPost findDiscussPosts(int userId, int offset, int limit, int orderMode) {if (userId 0 orderMode 1) {return postListCache.get(offset : limit);}logger.debug(load post list from DB.);return discussPostMapper.selectDiscussPosts(userId, offset, limit, orderMode);}public int findDiscussPostRows(int userId) {if (userId 0) {return postRowsCache.get(userId);}logger.debug(load post rows from DB.);return discussPostMapper.selectDiscussPostRows(userId);}public int addDiscussPost(DiscussPost post) {if (post null) {throw new IllegalArgumentException(参数不能为空!);}// 转义HTML标记post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));post.setContent(HtmlUtils.htmlEscape(post.getContent()));// 过滤敏感词post.setTitle(sensitiveFilter.filter(post.getTitle()));post.setContent(sensitiveFilter.filter(post.getContent()));return discussPostMapper.insertDiscussPost(post);}public DiscussPost findDiscussPostById(int id) {return discussPostMapper.selectDiscussPostById(id);}public int updateCommentCount(int id, int commentCount) {return discussPostMapper.updateCommentCount(id, commentCount);}public int updateType(int id, int type) {return discussPostMapper.updateType(id, type);}public int updateStatus(int id, int status) {return discussPostMapper.updateStatus(id, status);}public int updateScore(int id, double score) {return discussPostMapper.updateScore(id, score);}service层的代码是一个帖子DiscussPost的服务类提供了一些操作帖子的方法并使用了缓存来提高性能。这段代码有点长所以读起来有点费力我讲一下我对这些代码的理解吧。
findDiscussPosts() 方法用于查询帖子列表。如果传入的用户ID为0且排序方式为1orderMode 1则直接从缓存中获取对应的结果否则调用数据库查询方法从数据库中获取结果。findDiscussPostRows() 方法用于查询帖子总数。如果传入的用户ID为0则直接从缓存中获取对应的结果否则调用数据库查询方法从数据库中获取结果。addDiscussPost() 方法用于添加帖子。在添加之前会对帖子的标题和内容进行转义HTML标记和敏感词过滤的处理然后调用数据库操作方法将帖子插入到数据库中并返回插入的结果。其他方法如 findDiscussPostById()、updateCommentCount()、updateType()、updateStatus()、updateScore() 都是调用数据库操作方法来更新或查询帖子的相关信息。
service层大概就是这些虽然说比较长但是还是很容易懂的controller层更加长。
最后写controller层。
Controller
RequestMapping(/discuss)
public class DiscussPostController implements CommunityConstant {RequestMapping(path /add, method RequestMethod.POST)ResponseBodypublic String addDiscussPost(String title, String content) {User user hostHolder.getUser();if (user null) {return CommunityUtil.getJSONString(403, 你还没有登录哦!);}DiscussPost post new DiscussPost();post.setUserId(user.getId());post.setTitle(title);post.setContent(content);post.setCreateTime(new Date());discussPostService.addDiscussPost(post);// 触发发帖事件Event event new Event().setTopic(TOPIC_PUBLISH).setUserId(user.getId()).setEntityType(ENTITY_TYPE_POST).setEntityId(post.getId());eventProducer.fireEvent(event);// 计算帖子分数String redisKey RedisKeyUtil.getPostScoreKey();redisTemplate.opsForSet().add(redisKey, post.getId());// 报错的情况,将来统一处理.return CommunityUtil.getJSONString(0, 发布成功!);}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);// 点赞数量long likeCount likeService.findEntityLikeCount(ENTITY_TYPE_POST, discussPostId);model.addAttribute(likeCount, likeCount);// 点赞状态int likeStatus hostHolder.getUser() null ? 0 :likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, discussPostId);model.addAttribute(likeStatus, likeStatus);// 评论分页信息page.setLimit(5);page.setPath(/discuss/detail/ discussPostId);page.setRows(post.getCommentCount());// 评论: 给帖子的评论// 回复: 给评论的评论// 评论列表ListComment commentList commentService.findCommentsByEntity(ENTITY_TYPE_POST, post.getId(), page.getOffset(), page.getLimit());// 评论VO列表ListMapString, Object commentVoList new ArrayList();if (commentList ! null) {for (Comment comment : commentList) {// 评论VOMapString, Object commentVo new HashMap();// 评论commentVo.put(comment, comment);// 作者commentVo.put(user, userService.findUserById(comment.getUserId()));// 点赞数量likeCount likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, comment.getId());commentVo.put(likeCount, likeCount);// 点赞状态likeStatus hostHolder.getUser() null ? 0 :likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId());commentVo.put(likeStatus, likeStatus);// 回复列表ListComment replyList commentService.findCommentsByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);// 回复VO列表ListMapString, Object replyVoList new ArrayList();if (replyList ! null) {for (Comment reply : replyList) {MapString, 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);// 点赞数量likeCount likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, reply.getId());replyVo.put(likeCount, likeCount);// 点赞状态likeStatus hostHolder.getUser() null ? 0 :likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId());replyVo.put(likeStatus, likeStatus);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;}// 置顶RequestMapping(path /top, method RequestMethod.POST)ResponseBodypublic String setTop(int id) {discussPostService.updateType(id, 1);// 触发发帖事件Event event new Event().setTopic(TOPIC_PUBLISH).setUserId(hostHolder.getUser().getId()).setEntityType(ENTITY_TYPE_POST).setEntityId(id);eventProducer.fireEvent(event);return CommunityUtil.getJSONString(0);}// 加精RequestMapping(path /wonderful, method RequestMethod.POST)ResponseBodypublic String setWonderful(int id) {discussPostService.updateStatus(id, 1);// 触发发帖事件Event event new Event().setTopic(TOPIC_PUBLISH).setUserId(hostHolder.getUser().getId()).setEntityType(ENTITY_TYPE_POST).setEntityId(id);eventProducer.fireEvent(event);// 计算帖子分数String redisKey RedisKeyUtil.getPostScoreKey();redisTemplate.opsForSet().add(redisKey, id);return CommunityUtil.getJSONString(0);}// 删除RequestMapping(path /delete, method RequestMethod.POST)ResponseBodypublic String setDelete(int id) {discussPostService.updateStatus(id, 2);// 触发删帖事件Event event new Event().setTopic(TOPIC_DELETE).setUserId(hostHolder.getUser().getId()).setEntityType(ENTITY_TYPE_POST).setEntityId(id);eventProducer.fireEvent(event);return CommunityUtil.getJSONString(0);}}
这段代码是一个控制器类处理与帖子相关的请求。
首先是 addDiscussPost() 方法用于处理发布帖子的请求。在方法中首先判断用户是否登录如果未登录则返回相应的提示信息。然后根据传入的参数创建一个 DiscussPost 对象并设置相应的属性。接下来调用 discussPostService.addDiscussPost(post) 方法将帖子插入到数据库中。之后触发一个发帖事件并计算帖子的分数。最后返回一个 JSON 格式的发布成功信息。
接下来是 getDiscussPost() 方法用于获取帖子详情的请求。首先根据传入的帖子ID调用 discussPostService.findDiscussPostById(discussPostId) 方法获取帖子对象并将帖子对象添加到 Model 中。然后获取帖子的作者、点赞数量和点赞状态并将它们添加到 Model 中。接着设置评论的分页信息并调用 commentService.findCommentsByEntity() 方法获取帖子的评论列表。通过遍历评论列表将每个评论及其相关信息封装成一个评论VOMapString, Object并将评论VO添加到评论VO列表中。对于每个评论还会获取其回复列表并将每个回复及其相关信息封装成一个回复VOMapString, Object再将回复VO添加到回复VO列表中。最后将评论VO列表添加到 Model 中并返回一个指向帖子详情页面的视图。
接下来是 setTop() 方法用于置顶帖子的请求。在方法中调用 discussPostService.updateType(id, 1) 方法将帖子的类型设置为置顶。然后触发一个发帖事件并返回一个 JSON 格式的成功信息。
最后是 setDelete() 方法用于删除帖子的请求。在方法中调用 discussPostService.updateStatus(id, 2) 方法将帖子的状态设置为删除。然后触发一个删帖事件并返回一个 JSON 格式的成功信息。 其实对于帖子详情这里并不算太重要我觉得好好看看知道有这么一回事儿就行不需要刻意的去背背的话其实也用不到但是你确实需要知道这么一回事儿。