您的位置:首页 > 编程语言 > Java开发

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";
}
}

标签页面也是大同小异。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐