新聞管理系統的首頁新聞評論功能和分類标簽功能
- 新聞評論功能
-
- 效果展示
- 功能實作
- 分類功能
-
- 效果展示
- 功能實作
- 标簽功能
-
- 效果展示
- 功能實作
新聞評論功能
效果展示
可以直接在新聞底下輸入姓名和郵箱評論,也可以回複别人發表的評論
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLysmeNJTV65UeRpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1EDNwUDOyEjMxADOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
功能實作
-
建立實體類
在pojo目錄下建立一個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;
@OneToMany(mappedBy = "parentComment")
private List<Comment> replyComment = new ArrayList<>();
@ManyToOne
private Comment parentComment;
//管理者評論
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 List<Comment> getReplyComment() {
return replyComment;
}
public void setReplyComment(List<Comment> replyComment) {
this.replyComment = replyComment;
}
public Comment getParentComment() {
return parentComment;
}
public void setParentComment(Comment parentComment) {
this.parentComment = parentComment;
}
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 +
", news=" + news +
", replyComment=" + replyComment +
", parentComment=" + parentComment +
", adminComment=" + adminComment +
'}';
}
}
-
建立repository
在dao目錄下建立一個CommentRepository,繼承jpa,并且聲明一個用來查找父級評論的方法
public interface CommentRepository extends JpaRepository<Comment,Long> {
//根據新聞id和父級評論是空查詢
// @Query
List<Comment> findByNewsIdAndParentCommentNull(Long newId, Sort sort);
}
-
建立Service
在Service目錄下建立一個CommentService的接口方法,聲明發表評論方法和查找所有評論方法,儲存評論的方法實作隻需要查找到是否有父級評論,并調用jpa儲存到資料庫中即可,但按父子級進行評論的展示需要循環疊代找出所有子代評論,并申請一個臨時存放區域,再按照父->子的方向儲存到一個commentsView連結清單中
public interface CommentService {
//發表評論
Comment saveComment(Comment comment);
//展示所有評論
List<Comment> listCommentByNewId(Long newId);
}
之後再在CommentServiceImpl中實作該方法,這裡
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
CommentRepository commentRepository;
@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);
}
@Override
public List<Comment> listCommentByNewId(Long newId) {
Sort sort = Sort.by("createTime");
List<Comment> comments = commentRepository.findByNewsIdAndParentCommentNull(newId,sort);
System.out.println("運作到listCommentByNewId");
return eachComment(comments);
}
//循環查找所有評論
private List<Comment> eachComment(List<Comment> comments){
List<Comment> commentsView = new ArrayList<>();
System.out.println("eachComment");
for(Comment comment:comments){
Comment c = new Comment();
BeanUtils.copyProperties(comment,c);
commentsView.add(c);
}
//合并評論的各層子代到第一代集合
combineChildrenComment(commentsView);
return commentsView;
}
private void combineChildrenComment(List<Comment> comments){
System.out.println("combineChildrenComment");
for(Comment comment:comments){
List<Comment> replies = comment.getReplyComment();
for(Comment reply : replies){
//循環疊代,找出子代 存放在臨時存放區
recursively(reply);
}
comment.setReplyComment(tempReplies);
//清除臨時存放區
tempReplies = new ArrayList<>();
}
}
//臨時找出子代
private List<Comment> tempReplies = new ArrayList<>();
private void recursively(Comment comment){
tempReplies.add(comment); //頂節點添加到臨時存放區
if(comment.getReplyComment().size()>0){
List<Comment> replies = comment.getReplyComment();
for(Comment reply:replies){
tempReplies.add(reply);
if(comment.getReplyComment().size()>0){
recursively(reply);
}
}
}
}
}
- 建立controller
在web目錄下建立一個Controller,由于該部分功能針對所有使用者,是以需要再全局web下進行建立,目前再界面中輸入的隻能是昵稱和郵箱和評論,頭像直接在controller中聲明的,後續可改成在評論時自定義頭像
@Controller
public class CommentController {
@Autowired
CommentService commentService;
@Autowired
NewService newService;
//儲存
private String avatar = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1091405991,859863778&fm=26&gp=0.jpg";
@GetMapping("/comments/{newId}")
public String comments(@PathVariable Long newId, Model model){
System.out.println("運作到這裡"+newId);
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));
User user = (User)session.getAttribute("user");
if(user!=null){
comment.setAdminComment(true);
}
comment.setAvatar(avatar);
commentService.saveComment(comment);
return "redirect:/comments/"+newId;
}
}
在該檔案中,comments方法用來展示目前新聞的評論,這裡的listCommentByNewId(newID)方法将直接從service中實作的方法傳回的連結清單裡按父子級進行評論展示,而post方法用于送出輸入的評論,用到service中的save方法
分類功能
效果展示
根據目前的分類目錄進行相應的新聞展示
功能實作
-
添加TypeRepository方法
由于在實體類中已經聲明了各個屬性,是以直接從TypeRepository添加查詢方法開始,在TypeRepository接口中添加方法
@Query("select t from Type t")
List<Type> findTop(Pageable pageable);
-
添加TypeService方法
和TypeRepository類似,直接在TypeService中添加方法
并在Impl中實作,實作的時候指定是通過分類下的新聞總數進行排序,進而将新聞多的分類顯示在前面
//顯示在首頁
@Override
public List<Type> listTypeTop(Integer size) {
Sort sort = Sort.by(Sort.Direction.DESC,"news.size");
Pageable pageable = PageRequest.of(0,size,sort);
return typeRepository.findTop(pageable);
}
- 建立TypeShowController
在web目錄下建立一個TypeShowController,并按照更新時間再對目前分類下的新聞進行排序展示
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private 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.listNew(pageable,newQuery));
model.addAttribute("activeTypeId",id);
return "types";
}
}
需要調用到的NewService中的查找方法之前已經實作過了
标簽功能
效果展示
功能實作
功能實作步驟和type差不多,隻是在調用NewService處的方法傳參不一樣,需要對每種傳參都進行方法實作
- 添加TagRepository方法
@Query("select t from Tag t")
List<Tag> findTop(Pageable pageable);
- 添加TagService方法
//顯示在首頁
List<Tag> listTagTop(Integer size);
在Impl中實作
@Override
public List<Tag> listTagTop(Integer size) {
Sort sort = Sort.by(Sort.Direction.DESC,"newsList.size");
Pageable pageable = PageRequest.of(0,size,sort);
return tagRepository.findTop(pageable);
}
-
建立TagShowController
在web目錄下建立一個TagShowController
@Controller
public class TagShowController {
@Autowired
private NewService newService;
@Autowired
private 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(20);
if(id==-1){
id = tags.get(0).getId();
}
model.addAttribute("tags",tags);
model.addAttribute("page",newService.listNew(pageable));
model.addAttribute("activeTagId",id);
return "tags";
}
}
這裡需要在NewService中新聲明一個隻有pageable參數的listNew方法,并在Impl中實作
//首頁分頁展示
Page<News> listNew(Pageable pageable);
Impl實作
@Override
public Page<News> listNew(Pageable pageable) {
return newRepository.findAll(pageable);
}