spring boot新闻管理项目——评论功能和按标签、类型显示新闻
2020-07-31 19:13
429 查看
本次实现了新闻页面的评论功能,能够对新闻进行评论和评论互动。同时也实现了点击新闻列表首页的分类和标签,可按分类和标签显示新闻的功能。
本次设计的难点主要集中在评论的展示功能,需将所有子评论展示到一个最初的父评论下,实现利用了递归思想。本次以评论功能的实现为例。
-
设计
po.Comment
类,并在po.News
类中添加相应的Comment
类,并更新Setter
、Getter
和toString
方法@Entity @Table(name = "t_comment") public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String nickname; private String email; private String content; private String avatar; @Temporal(TemporalType.TIMESTAMP) private Date createTime; @ManyToOne private News news; @OneToMany(mappedBy = "parentComment") private List<Comment> replyComments = new ArrayList<>(); @ManyToOne private Comment parentComment; private boolean adminComment; public Comment() { } }
添加
List<Comment>
数据@OneToMany(mappedBy = "news") private List<Comment> commentList = new ArrayList<>();
-
创建
dao/CommentRepository.java
类,进行数据库操作public interface CommentRepository extends JpaRepository<Comment, Long> { List<Comment> findByNewsIdAndParentCommentNull(Long newsId, Sort sort); }
-
创建
service.impl.CommentServiceImpl.java
类,提供保存评论和展示评论列表的任务@Service public class CommentServiceImpl implements CommentService { @Autowired private CommentRepository commentRepository; //提供展示评论的服务 @Override public List<Comment> listCommentByNewsId(Long newsId) { Sort sort = Sort.by("createTime"); List<Comment> comments = commentRepository.findByNewsIdAndParentCommentNull(newsId, sort); return eachComment(comments); } //遍历每条无父评论的评论 private List<Comment> eachComment(List<Comment> comments) { List<Comment> commentsView = new ArrayList<>(); for (Comment comment : comments) { Comment c = new Comment(); BeanUtils.copyProperties(comment, c); commentsView.add(c); } //合并评论的各层子代到第一级子代集合中 combineChildren(commentsView); return commentsView; } private void combineChildren(List<Comment> comments) { for (Comment comment : comments) { List<Comment> replys1 = comment.getReplyComments(); for (Comment reply1 : replys1) { //循环迭代,找出子代,存放在临时tempReplys中 recursively(reply1); } //将所有子孙评论放入一级子评论中 comment.setReplyComments(tempReplys); //清除临时存放区 tempReplys = new ArrayList<>(); } } //定义临时存放区 private List<Comment> tempReplys = new ArrayList<>(); //递归将所有子评论存入临时存放区 private void recursively(Comment comment) { tempReplys.add(comment); //顶节点添加到临时存放区 if (comment.getReplyComments().size() > 0) { List<Comment> replys = comment.getReplyComments(); for (Comment reply : replys) { tempReplys.add(reply); if (reply.getReplyComments().size() > 0) { recursively(reply); } } } } //保存评论 @Override public Comment saveComment(Comment comment) { Long parentCommentId = comment.getParentComment().getId(); if (parentCommentId != -1) { comment.setParentComment(commentRepository.findById(parentCommentId).orElse(null)); } else { comment.setParentComment(null); } comment.setCreateTime(new Date()); return commentRepository.save(comment); } }
-
创建
com.llanero.news.web.CommentController.java
类,通过Service
包中提供的对象方法响应web端页面的请求@Controller public class CommentController { @Autowired private CommentService commentService; @Autowired private NewsService newsService; private String avatar = "https://gitee.com/llanero/share/raw/master/img/20200727182846.jpeg"; @GetMapping("/comments/{newId}") public String comments(@PathVariable Long newId, Model model) { model.addAttribute("comments", commentService.listCommentByNewsId(newId)); return "new::commentList"; } @PostMapping("/comments") private String post(Comment comment, HttpSession session) { Long newsId = comment.getNews().getId(); comment.setNews(newsService.getNews(newsId)); User user = (User) session.getAttribute("user"); if (user != null) { comment.setAdminComment(true); comment.setAvatar(avatar); } else { comment.setAvatar(avatar); } commentService.saveComment(comment); return "redirect:/comments/" + newsId; } }
-
分类展示介绍
创建
web.TypeShowController
类,进行分类展示新闻@Autowired private TypeService typeService; @Autowired private NewsService newsService; @GetMapping("/types/{id}") public String types(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable, @PathVariable Long id, Model model) { List<Type> types = typeService.listTypeTop(20); if (id == -1) { id = types.get(0).getId(); } NewsQuery newsQuery = new NewsQuery(); newsQuery.setTypeId(id); model.addAttribute("types", types); model.addAttribute("page", newsService.listNews(pageable, newsQuery)); model.addAttribute("activeTypeId", id); return "types"; }
-
项目运行测试
相关文章推荐
- Springboot开发:新闻管理系统评论、分页以及标签页
- spring boot项目标签管理+分类管理
- springboot开发:新闻管理系统实现增删查改功能
- [个人笔记]基于SpringBoot新闻系统的分类管理和标签管理
- 使用struts2 hibernate spring整合实现新闻信息管理系统案例-实现新闻信息分页显示功能
- [个人笔记]基于SpringBoot的新闻管理系统 归档展示/登录拦截/异常处理功能
- 项目(一)springboot多模块工程(3)集成mybatis-plus实现基本crud、集成swagger2接口管理
- SpringBoot项目yml文件 不显示绿色小树叶的问题
- 奇幻网站实现点击分类~标签显示文章详情和多级评论功能
- Jenkins+Git+Maven+docker自动化部署管理springBoot项目
- springboot+shiro+mybatis整合发现部分功能事务没有被spring管理
- springboot项目实现批量新增功能
- springboot web项目发布页面显示net/sf/json/JSONArray解决
- spring boot新闻管理条件查询分页展示+新增+编辑
- springboot开发:新闻管理系统之添加和修改新闻
- Intellij2017下springboot项目启动热部署功能
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十四):项目打包部署
- 微信公众平台新增评论管理功能 可显示用户评论
- 关于Springboot项目的Pom.xml文件第一排标签报错原因
- SpringBoot项目 使用Sprin Data Jpa ——JpaRepository查询功能