文章详情查看
1.数据访问层实现
进行文章查询功能实现时,还包括对文章对应的评论信息进行查询,因此,虽然前面已经编写了文章类Article和文章统计类Statistic对应的接口文件,还要补充编写评论类Comment对应的接口文件。
在com.itheima.dao包下创建评论类Comment对应的Mapper接口文件,内容如文件10-14所示。
CommentMapper.java
package com.itheima.dao;
import com.itheima.model.domain.Comment;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
* @Classname CommentMapper
* @Description TODO
* @Date 2019-3-14 10:12
* @Created by CrazyStone
*/
@Mapper
public interface CommentMapper {
// 分页展示某个文章的评论
@Select("SELECT * FROM t_comment WHERE article_id=#{aid} ORDER BY id DESC")
public List<Comment> selectCommentWithPage(Integer aid);
// 后台查询最新几条评论
@Select("SELECT * FROM t_comment ORDER BY id DESC")
public List<Comment> selectNewComment();
// 发表评论
@Insert("INSERT INTO t_comment (article_id,created,author,ip,content)" +
" VALUES (#{articleId}, #{created},#{author},#{ip},#{content})")
public void pushComment(Comment comment);
// 站点服务统计,统计评论数量
@Select("SELECT COUNT(1) FROM t_comment")
public Integer countComment();
// 通过文章id删除评论信息
@Delete("DELETE FROM t_comment WHERE article_id=#{aid}")
public void deleteCommentWithId(Integer aid);
}
文件10-14中,除了分页查询评论方法外,还包括本项目后续需要的其他数据操作方法,具体说明可以参考注释。
2.业务处理层的实现
文章详情查询要处理的业务包括:查询文章详情并使用Redis缓存管理、封装文章评论、统计更新文章点击量。业务处理层实现的具体步骤如下。
(1)创建Service层接口文件
打开文章业务接口文件IArticleService,首先在该接口文件中编写一个根据文章id查询文章详情的接口方法,示例代码如下。
//根据文章id查询单个文章详情
public Article selectArticleWithId(Integer id);
然后,在com.itheima.service 包下分别创建评论业务处理和博客站点业务处理的Service接口文件,内容分别如文件10-15和10-16所示。
ICommentService.java
package com.itheima.service;
import com.github.pagehelper.PageInfo;
import com.itheima.model.domain.Comment;
/**
* @Classname ICommentService
* @Description 文章评论业务处理接口
* @Date 2019-3-14 10:13
* @Created by CrazyStone
*/
public interface ICommentService {
// 获取文章下的评论
public PageInfo<Comment> getComments(Integer aid, int page, int count);
// 用户发表评论
public void pushComment(Comment comment);
}
ISiteService.java
package com.itheima.service;
import com.itheima.model.ResponseData.StaticticsBo;
import com.itheima.model.domain.Article;
import com.itheima.model.domain.Comment;
im