博客项目(四)——Service及其实现类

本文详细介绍了服务层的设计,包括Article、Category、Tag、Comment、Link、Menu、Notice、Options、User和Page等模块的接口定义及实现类,涵盖了文章统计、分类管理、标签操作、评论管理、链接管理、菜单操作、公告管理、站点配置、用户管理和页面管理等功能。
摘要由CSDN通过智能技术生成

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){
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值