Spring Boot 项目实战5(评论、分类页面、标签页面)
2020-08-04 21:33
603 查看
接上:
https://blog.csdn.net/qq_43923042/article/details/107799148
上次讲到新闻详情,详情界面如下:
显然,单单有新闻是不够的,还要有底下评论
1、新建实体类Comment
@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; @ManyToOne Comment parentComment; private boolean adminComment; @OneToMany(mappedBy = "parentComment") List<Comment> replyComments = new ArrayList<>(); }
2、DAO层:
public interface CommentReposity extends JpaRepository<Comment, Long> { List<Comment> findByNewsIdAndParentCommentNull(Long newId, Sort sort); }
3、Service层:
@Service public class CommentServiceImpl implements CommentService { @Autowired CommentReposity commentReposity; @Override public List<Comment> listCommentByNewId(Long newId) { Sort sort = Sort.by("createTime"); //所有顶级回复 List<Comment> comments = commentReposity.findByNewsIdAndParentCommentNull(newId, sort); return eachComment(comments); } @Override public Comment saveComment(Comment comment) { Long parenCommentId = comment.getParentComment().getId(); comment.setCreateTime(new Date()); if (parenCommentId != -1) { comment.setParentComment(commentReposity.findById(parenCommentId).orElse(null)); } else { comment.setParentComment(null); } return commentReposity.save(comment); } 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> replys = comment.getReplyComments(); for (Comment reply1 : replys) { 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); } } } } }
4、Controller层:
@Controller public class CommentController { @Autowired private CommentService commentService; @Autowired private NewService newService; private String avatar = "https://c-ssl.duitang.com/uploads/item/201804/23/2018042312947_wykaJ.thumb.1000_0.jpeg"; @GetMapping("/comments/{newId}") String comments(@PathVariable Long newId, Model model) { model.addAttribute("comments", commentService.listCommentByNewId(newId)); return "new :: commentList"; } @PostMapping("/comments") public String post(Comment comment, HttpSession session) { Long newId = comment.getNews().getId(); comment.setNews(newService.getNew(newId)); System.out.println(comment.getParentComment().getId()); User user = (User) session.getAttribute("user"); if (user != null) { comment.setAdminComment(true); comment.setAvatar(avatar); } else { comment.setAvatar(avatar); } System.out.println("newId:" + newId); commentService.saveComment(comment); return "redirect:/comments/" + newId; } }
再就是分类页面,将数据展现出来
主要是通过统计不同类别的新闻
5、DAO不用新增函数,使用jpa原装的就行了
Service层新增以下函数:
@Override public Page<Type> listType(Pageable pageable) { return typeRepository.findAll(pageable); }
6、Controller层:
@Controller public class TypeShowController { @Autowired TypeService typeService; @Autowired NewService newService; @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(); } NewQuery newQuery = new NewQuery(); newQuery.setTypeId(id); model.addAttribute("types", types); model.addAttribute("page", newService.listNews(pageable, newQuery)); model.addAttribute("activeTypeId", id); return "types"; } }
标签页面也是大同小异。
相关文章推荐
- 中软国际实训十一:SpringBoot项目——评论+分类、标签页面实现
- 项目迭代之评论、分类、标签页面实现
- Springboot项目-评论+分类页面+标签页面
- 0731_评论+分类页面+标签页面
- 评论+分类页面+标签页面
- MyEclipse修改页面模板(JSP和HTML等) 分类: WEB项目应用 2010-01-31 00:03 698人阅读 评论(1) 收藏
- 【中软实训】11.评论+分类+标签页面实现
- 实战caffe多标签分类——汽车品牌与车辆外观(C++接口)[详细实现+数据集]
- jQuery获得页面元素的绝对/相对位置 分类: JavaScript 2010-08-09 15:53 868人阅读 评论(0) 收藏
- Vue.js项目中管理每个页面的头部标签的方法
- 网络应用系统---系统设计架构个人项目实战示例(各种页面代码A)
- 《Go语言入门》第一个Go语言程序——HelloWorld 标签: go语言helloworld 2015-11-29 18:46 4066人阅读 评论(4) 收藏 举报 分类: go语言入门(3)
- [thinkPHP5项目实战_06]引入前台页面
- Python全栈(八)Flask项目实战之12.前台页面完善
- 复制A项目到B项目,作为B项目的子包报错:提示:R cannot be resolved to a variable 分类: Android安装及配置 2014-07-26 16:57 82人阅读 评论(0) 收藏
- 程序员在囧途之垃圾创业团队 . 分类: 项目管理 2014-06-24 09:20 641人阅读 评论(0) 收藏
- iOS开发小技巧--适当的清空模型中的某个数据,达到自己的需求,记得最后将数据还原(百思项目评论页面处理最热评论)
- iOS项目开发实战——使用三种方式实现页面跳转与参数传递(三)
- QQ登陆页面实现 分类: ios开发 2014-12-12 21:06 150人阅读 评论(0) 收藏
- MAVEN项目中JSP页面使用JSTL标签