新闻管理系统评论模块,主页分类页面和标签页面设计

一.评论模块

1.实体类设计

在po包中创建Comment实体类

package com.guang.demo.po;


import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@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;



}

2.dao层

创建CommentRepository继承JPA

package com.guang.demo.dao;

import com.guang.demo.po.Comment;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment,Long> {
   

    List<Comment> findByNewsIdAndParentCommentNull(Long newId, Sort sort);

}

3.service层

在service层中创建CommentService,定义功能实现方法:

package com.guang.demo.service;

import com.guang.demo.po.Comment;

import java.util.List;

public interface CommentService {
   

    //根据新闻id查询该新闻下的多有评论
    List<Comment> listCommentByNewId(Long newId);

    //发布保存评论
    Comment saveComment(Comment comment);
}

创建CommentServiceImpl,实现CommentService中的方法

package com.guang.demo.service;

import com.guang.demo.dao.CommentRepository;
import com.guang.demo.po.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
   

    @Autowired
    private CommentRepository commentRepository;

    @Override
    public List<Comment> listCommentByNewId(Long newId) {
   

        Sort sort = Sort.by("createTime");
        List<Comment> comments = commentRepository.findByNewsIdAndParentCommentNull(newId, sort);

        return eachComment(comments);
    }

    private List
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值