基于SpringBoot框架的新闻后台管理系统05
新闻后台管理系统5.0
本次项目内容
- 本次项目开发是在新闻后台管理系统4.0的基础上进行的
- 本次开发主要是实现评论功能以及分类主页、标签主页
项目开发过程
- 实现comment的功能和之前的News、Type、Tag是类似的,大家可以尝试自己去实现一下,无非就是po层、dao层、service层、controller层。之前的博客中也对于这些层也进行了相应的解释,所以下面直接放代码。
- 在po包下创建Comment实体类
@Entity
@Table(name = "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
private Comment parentComment;
@OneToMany(mappedBy = "parentComment")
private List<Comment> replyComments = new ArrayList<>();
private boolean adminComment;
public Comment() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public Comment getParentComment() {
return parentComment;
}
public void setParentComment(Comment parentComment) {
this.parentComment = parentComment;
}
public List<Comment> getReplyComments() {
return replyComments;
}
public void setReplyComments(List<Comment> replyComments) {
this.replyComments = replyComments;
}
public boolean isAdminComment() {
return adminComment;
}
public void setAdminComment(boolean adminComment) {
this.adminComment = adminComment;
}
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", nickname='" + nickname + '\'' +
", email='" + email + '\'' +
", content='" + content + '\'' +
", avatar='" + avatar + '\'' +
", createTime=" + createTime +
", parentComment=" + parentComment +
", replyComments=" + replyComments +
", adminComment=" + adminComment +
'}';
}
}
- 在dao包下创建CommentRepository接口
public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findByNewsIdAndParentCommentNull(Long newsId, Sort sort);
}
- 在service包下创建CommentService接口
public interface CommentService {
List<Comment> listCommentsByNewsId(Long id);
Comment saveComment(Comment comment);
}
- 在impl包下创建CommentServiceImpl类并实现CommentService接口
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
public List<Comment> listCommentsByNewsId(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> replays = comment.getReplyComments();
for(Comment reply: replays){
//循环迭代,找出子代,存放在临时tempReply中
recursively(reply);
}
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);
}
}
- 在web包下创建CommentController类,这个控制类之所以不放在admin包下,是因为评论是普通用户和管理员都能使用的功能,且评论是放在新闻详情里的,不在新闻管理后台。
@Controller
public class CommentController {
//这是两张随意找来的照片,一张用作普通用户的头像,一张是管理员的,可以随意替换
private String userAvatar = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1654409480,2232044795&fm=26&gp=0.jpg";
private String adminAvatar = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2034740944,4251903193&fm=26&gp=0.jpg";
@Autowired
private CommentService commentService;
@Autowired
private NewsService newsService;
@GetMapping("/comments/{newsId}")
public String comments(@PathVariable Long newsId, Model model){
model.addAttribute("comments", commentService.listCommentsByNewsId(newsId));
return "new::commentList";
}
@PostMapping("/comments")
public 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(adminAvatar);
}else{
comment.setAvatar(userAvatar);
}
commentService.saveComment(comment);
return "redirect:/comments/" + newsId;
}
}
-
以上操作已经实现了评论功能,但需要读者注意的是,News有一个属性是commentabled,这个属性的含义是News是否能评论,所以对于未勾选“评论”的News,用户是不能进行评论的。
-
接下来,我们将来实现新闻主页的分类页面和标签页面,在这两个页面中,News将分别以Type、Tag而分类显示。
-
在NewsService下新增查询服务
//标签页面查看新闻
Page<News> listNews(Long tagId, Pageable pageable);
- 在NewsServiceImpl实现新增查询服务
@Override
public Page<News> listNews(Long tagId, Pageable pageable) {
return newsRepository.findAll(new Specification<News>() {
@Override
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
Join join = root.join("tags");
return cb.equal(join.get("id"), tagId);
}
}, pageable);
}
- 在web包下创建TypeShowController类
@Controller
public class 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(100);
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";
}
}
- 在web包下创建TagShowController类
@Controller
public class TagShowController {
@Autowired
NewsService newsService;
@Autowired
TagService tagService;
@GetMapping("/tags/{id}")
public String tags(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
@PathVariable Long id, Model model){
List<Tag> tags = tagService.listTagTop(100);
if(id == -1){
id = tags.get(0).getId();
}
model.addAttribute("tags", tags);
model.addAttribute("page", newsService.listNews(id, pageable));
model.addAttribute("activeTagId", id);
return "tags";
}
}
-
运行程序
-
打开浏览器并在网址栏内输入"localhost:8081/admin",这个是新闻后台管理网站,管理员可以在这里面对新闻、类型、标签进行各种操作。
-
打开浏览器并在网址栏内输入"localhost:8081/",这个是新闻主页,用户可以在这里面浏览、查询新闻。
项目演示
-
评论功能
-
分类页面
-
标签页面
项目下载
链接:news5