1.Article
- 获得文章总数
- 获得评论总数
- 获得浏览总数
- 统计某一分类的文章数(根据分类ID)
- 统计某一标签的文章数(根据标签ID)
- 获得文章(根据查询条件)
- 获得最新文章
- 修改文章
- 删除文章
- 批量删除文章
- 添加文章
- 文章分页显式
- 文章详细信息显示
- 获得访问量最多的文章
- 获得上一篇文章
- 获得下一篇文章
- 获得随机文章
- 获得评论数较多的文章
- 更新文章评论数
- 获得相关文章
- 根据文章ID获得分类ID
- 获得所有文章
- 根据文章作者ID获得文章ID列表
- 获得最后更新时间
1.1 接口ArticleService
package com.test.ssm.blog.service;
import com.github.pagehelper.PageInfo;
import com.test.ssm.blog.entity.Article;
import java.util.HashMap;
import java.util.List;
public interface ArticleService {
//获得文章总数
Integer countArticle(Integer status);
//获得评论总数
Integer countArticleComment();
//获得浏览总数
Integer countArticleView();
//统计有这个分类的文章数
Integer countArticleByCategoryId(Integer categoryId);
//统计有这个标签的
Integer countArticleByTagId(Integer tagId);
//获得所有文章不分页
List<Article> listArticle(HashMap<String,Object> criteria);
//获得最新文章
List<Article> listRecentArticle(Integer limit);
//修改文章详细信息
void updateArticleDetail(Article article);
//修改文章
void updateArticle(Article article);
//批量删除文章
void deleteArticleBatch(List<Integer> ids);
//删除文章
void deleteArticle(Integer id);
/**
* 分页显示
*
* @param pageIndex 第几页开始
* @param pageSize 一页显示多少
* @param criteria 查询条件
* @return 文章列表
*/
PageInfo<Article> pageArticle(Integer pageIndex,
Integer pageSize,
HashMap<String, Object> criteria);
/**
* 文章详情页面显示
*
* @param status 状态
* @param id 文章ID
* @return 文章
*/
Article getArticleByStatusAndId(Integer status, Integer id);
/**
* 获取访问量较多的文章
*
* @param limit 查询数量
* @return 列表
*/
List<Article> listArticleByViewCount(Integer limit);
/**
* 获得上一篇文章
*
* @param id 文章ID
* @return 文章
*/
Article getAfterArticle(Integer id);
/**
* 获得下一篇文章
*
* @param id 文章ID
* @return 文章
*/
Article getPreArticle(Integer id);
/**
* 获得随机文章
*
* @param limit 查询数量
* @return 列表
*/
List<Article> listRandomArticle(Integer limit);
/**
* 获得评论数较多的文章
*
* @param limit 查询数量
* @return 列表
*/
List<Article> listArticleByCommentCount(Integer limit);
/**
* 添加文章
*
* @param article 文章
*/
void insertArticle(Article article);
/**
* 更新文章的评论数
*
* @param articleId 文章ID
*/
void updateCommentCount(Integer articleId);
/**
* 获得最后更新记录
*
* @return 文章
*/
Article getLastUpdateArticle();
/**
* 获得相关文章
*
* @param cateId 分类ID
* @param limit 查询数量
* @return 列表
*/
List<Article> listArticleByCategoryId(Integer cateId, Integer limit);
/**
* 获得相关文章
*
* @param cateIds 分类ID集合
* @param limit 数量
* @return 列表
*/
List<Article> listArticleByCategoryIds(List<Integer> cateIds, Integer limit);
/**
* 根据文章ID获得分类ID列表
*
* @param articleId 文章Id
* @return 列表
*/
List<Integer> listCategoryIdByArticleId(Integer articleId);
/**
* 获得所有的文章
*
* @return 列表
*/
List<Article> listAllNotWithContent();
List<Integer> listArticleIdByUserId(Integer articleUserId);
}
1.2 实现类ArticleServiceImpl
package com.test.ssm.blog.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ssm.blog.entity.*;
import com.test.ssm.blog.enums.ArticleCommentStatus;
import com.test.ssm.blog.mapper.ArticleCategoryRefMapper;
import com.test.ssm.blog.mapper.ArticleMapper;
import com.test.ssm.blog.mapper.ArticleTagRefMapper;
import com.test.ssm.blog.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
@Autowired(required = false)
private ArticleMapper articleMapper;
@Autowired(required = false)
private ArticleCategoryRefMapper articleCategoryRefMapper;
@Autowired(required = false)
private ArticleTagRefMapper articleTagRefMapper;
@Override
public Integer countArticle(Integer status) {
Integer count = 0;
try {
count = articleMapper.countArticle(status);
} catch (Exception e) {
e.printStackTrace();
log.error("根据状态统计文章数, status:{}, cause:{}", status, e);
}
return count;
}
@Override
public Integer countArticleComment() {
Integer count = 0;
try {
count = articleMapper.countArticleComment();
} catch (Exception e) {
e.printStackTrace();
log.error("统计文章评论数失败, cause:{}", e);
}
return count;
}
@Override
public Integer countArticleView() {
Integer count = 0;
try {
count = articleMapper.countArticleView();
} catch (Exception e) {
e.printStackTrace();
log.error("统计文章访问量失败, cause:{}", e);
}
return count;
}
@Override
public Integer countArticleByCategoryId(Integer categoryId) {
Integer count = 0;
try {
count = articleCategoryRefMapper.countArticleByCategoryId(categoryId);
} catch (Exception e) {
e.printStackTrace();
log.error("根据分类统计文章数量失败, categoryId:{}, cause:{}", categoryId, e);
}
return count;
}
@Override
public Integer countArticleByTagId(Integer tagId) {
return articleTagRefMapper.countArticleByTagId(tagId);
}
@Override
public List<Article> listArticle(HashMap<String, Object> criteria) {
return articleMapper.findAll(criteria);
}
@Override
public List<Article> listRecentArticle(Integer limit) {
return articleMapper.listArticleByLimit(limit);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateArticleDetail(Article article) {
article.setArticleUpdateTime(new Date());
articleMapper.update(article);
if (article.getTagList() != null) {
//删除标签和文章关联
articleTagRefMapper.deleteByArticleId(article.getArticleId());
//添加标签和文章关联
for (int i = 0; i < article.getTagList().size(); i++) {
ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId());
articleTagRefMapper.insert(articleTagRef);
}
}
if (article.getCategoryList() != null) {
//添加分类和文章关联
articleCategoryRefMapper.deleteByArticleId(article.getArticleId());
//删除分类和文章关联
for (int i = 0; i < article.getCategoryList().size(); i++) {
ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId());
articleCategoryRefMapper.insert(articleCategoryRef);
}
}
}
@Override
public void updateArticle(Article article) {
articleMapper.update(article);
}
@Override
public void deleteArticleBatch(List<Integer> ids) {
articleMapper.deleteBatch(ids);
}
@Override
public void deleteArticle(Integer id) {
System.out.println("into");
Article article=articleMapper.getArticleById(id);
if(articleTagRefMapper.listTagByArticleId(article.getArticleId())!=null){