新闻评论功能
功能实现
新建实体类
1.在po目录下新建一个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 +
'}';
}
}
2.新建repository
在dao目录下新建一个CommentRepository,继承jpa,并且声明一个用来查找父级评论的方法
//涉及数据库的操作
public interface CommentRepository extends JpaRepository<Comment,Long> {
//把父级评论找出来即可
//是父级评论是空的
List<Comment> findByNewsIdAndParentCommentNull(Long newId, Sort sort);
}
3.新建Service
在Service目录下新建一个CommentService的接口方法,声明发表评论方法和查找所有评论方法,保存评论的方法实现只需要查找到是否有父级评论,并调用jpa保存到数据库中即可,但按父子级进行评论的展示需要循环迭代找出所有子代评论,并申请一个临时存放区域,再按照父评论到子评论的方向保存到一个commentsView链表中
public interface CommentService {
// 通过新闻的Id查看所有的评论并展示
List<Comment> listCommentByNewId(Long newId);
//发表评论功能 把评论保存下来
Comment saveComment(Comment comment);
}
之后再在CommentServiceImpl中实现该方法:
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentRepository commentRepository;
@Override
//根据新闻Id绑定所有的新闻评论
public List<Comment> listCommentByNewId(Long newId) {
//定义评论排序的规则
Sort sort = Sort.by("createTime");
List<Comment> comments = commentRepository.findByNewsIdAndParentCommentNull(newId, sort);
return eachComment(comments);
}
//循环获取分级评论
private List<Comment> eachComment(List<Comment> comments){
//commentsView是个集合
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> replys1 =comment.getReplyComments();
//找出那条评论的子评论
for (Comment reply1:replys1){
//把每一条评论的子回复都获取 使用循环迭代 找出子代,存放在临时tempReplys中
recursively(reply1);
}
comment.setReplyComments(tempReplys);
//清空临时存放区
tempReplys = new ArrayList<>();
}
}
//设置临时存放区
private List<Comment> tempReplys = new ArrayList<>();
//不停的找子回复 然后通通存到临时存放区
private void recursively(Comment comment){
tempReplys.add(comment); //comment为顶节点 把他添加到临时存放区
if (comment.getReplyComments().size()>0){//获取顶节点的回复大于0 就是有回复
List<Comment> replys = comment.getReplyComments();
for (Comment reply:replys){//把单独的每条评论添加
tempReplys.add(reply);
if (reply.getReplyComments().size()>0){
recursively(reply);
}
}
}
}
@Override
//传进来comment对象为参数
public Comment saveComment(Comment comment) {
//先把父级评论的对象提取
Long parenCommentId = comment.getParentComment().getId();
//再把父级评论的Id提取
if (parenCommentId!=-1){
comment.setParentComment(commentRepository.findById(parenCommentId).orElse(null));
}
//没有父级评论
else{
comment.setParentComment(null);
}
//创建时间
comment.setCreateTime(new Date());
return commentRepository.save(comment);
}
}
4.新建controller
在web目录下新建一个Controller,由于该部分功能针对所有用户,所以需要再全局web下进行创建,在界面中输入的只能是昵称和邮箱和评论,头像直接在controller中声明的,后续可改成在评论时自定义头像
@Controller
public class CommentController {
@Autowired
private CommentService commentService;
@Autowired
private NewService newService;
//默认头像
private String avatar = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2515183491,81250008&fm=26&gp=0.jpg";
@GetMapping("/comments/{newId}")
public 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();
//把你的新闻Id拿过来跟数据库比较查找 就可以找到这个新闻的评论
comment.setNews(newService.getNew(newId));
//验证判断身份是否为管理员
User user=(User) session.getAttribute("user");
if (user!=null){
comment.setAdminComment(true);
//默认头像
comment.setAvatar(avatar);
}else {
comment.setAvatar(avatar);
}
//保存评论
commentService.saveComment(comment);
//保存完后暂时跳到主页
return "redirect:/comments/"+ newId;
}
}
在这个类中,comments方法用来展示当前新闻的评论,这里的listCommentByNewId(newID)方法将直接从service中实现的方法返回的链表里按父子级进行评论展示,而post方法用于提交输入的评论,用到service中的save方法
效果图:
分类展示
根据当前的分类目录进行相应的新闻展示
功能实现
1.添加TypeRepository接口
由于在实体类中已经声明了各个属性,所以直接从TypeRepository添加查询方法开始,在TypeRepository接口中添加方法
//指定要使用实体的类型为Type,和主键的类型为Long
public interface TypeRepository extends JpaRepository<Type,Long> {
//不允许添加同样的标签 查数据库 如果查到了就提示已存在
Type findByName(String name);
@Query("select t from Type t")
List<Type> findTop(Pageable pageable);
}
2.添加TypeService接口
和TypeRepository类似,直接在TypeService中添加方法
List<Type> listTypeTop(Integer size);
并在Impl中实现,实现的时候指定是通过分类下的新闻总数进行排序,进而将新闻多的分类显示在前面
//分类展示在主页
@Override
public List<Type> listTypeTop(Integer size) {
//实体属性
Sort sort = Sort.by(Sort.Direction.DESC,"news.size");
//从第0页查询 size为外部传参 sort为显示页数的方式
Pageable pageable = PageRequest.of(0, size, sort);
return typeRepository.findTop(pageable);
}
3.新建TypeShowController
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private NewService newService;
@GetMapping("/types/{id}")
public String type(@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
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处的方法传参不一样,需要对每种传参都进行方法实现
1.添加TagRepository方法
//标签显示在主页
public interface TagRepository extends JpaRepository<Tag,Long> {
Tag findByName(String name);
@Query("select t from Tag t")
List<Tag> findTop(Pageable pageable);
}
2.添加TagService方法
//在主页显示标签分类
List<Tag> listTagTop(Integer size);
3.新建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
id = tags.get(0).getId();
}
model.addAttribute("tags", tags);
model.addAttribute("page", newService.listNew(id, pageable));
model.addAttribute("activeTagId", id);
return "tags";
}
}
这里需要在NewService中新声明一个只有pageable参数的listNew方法,并在Impl中实现
//标签页面查看新闻
Page<News> listNew(Long tagId,Pageable pageable);
Impl实现
//在主页分页展示
@Override
public Page<News> listNew(Long tagId, Pageable pageable) {
return newRepository.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);
}
效果图: