当前位置: 首页 > 工具软件 > blog-service > 使用案例 >

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

葛越泽
2023-12-01

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){
            System.out.println("into Tag");
            //删除标签和文章关联
            articleTagRefMapper.deleteByArticleId(article.getArticleId());
            }
        if(articleCategoryRefMapper.listCategoryByArticleId(article.getArticleId())!=null)
        {
            System.out.println("into Category");
            articleCategoryRefMapper.deleteByArticleId(id);
        }
        articleMapper.deleteById(id);
    }




    @Override
    public PageInfo<Article> pageArticle(Integer pageIndex,
                                         Integer pageSize,
                                         HashMap<String, Object> criteria) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Article> articleList = articleMapper.findAll(criteria);
        for (int i = 0; i < articleList.size(); i++) {
            //封装CategoryList
            List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(articleList.get(i).getArticleId());
            if (categoryList == null || categoryList.size() == 0) {
                categoryList = new ArrayList<>();
                categoryList.add(Category.Default());
            }
            articleList.get(i).setCategoryList(categoryList);
//            //封装TagList
//            List<Tag> tagList = articleTagRefMapper.listTagByArticleId(articleList.get(i).getArticleId());
//            articleList.get(i).setTagList(tagList);
        }
        return new PageInfo<>(articleList);
    }

    @Override
    public Article getArticleByStatusAndId(Integer status, Integer id) {
        Article article = articleMapper.getArticleByStatusAndId(status, id);
        if (article != null) {
            List<Category> categoryList = articleCategoryRefMapper.listCategoryByArticleId(article.getArticleId());
            List<Tag> tagList = articleTagRefMapper.listTagByArticleId(article.getArticleId());
            article.setCategoryList(categoryList);
            article.setTagList(tagList);
        }
        return article;
    }


    @Override
    public List<Article> listArticleByViewCount(Integer limit) {
        return articleMapper.listArticleByViewCount(limit);
    }

    @Override
    public Article getAfterArticle(Integer id) {
        return articleMapper.getAfterArticle(id);
    }

    @Override
    public Article getPreArticle(Integer id) {
        return articleMapper.getPreArticle(id);
    }

    @Override
    public List<Article> listRandomArticle(Integer limit) {
        return articleMapper.listRandomArticle(limit);
    }

    @Override
    public List<Article> listArticleByCommentCount(Integer limit) {
        return articleMapper.listArticleByCommentCount(limit);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertArticle(Article article) {
        //添加文章
        article.setArticleCreateTime(new Date());
        article.setArticleUpdateTime(new Date());
        article.setArticleIsComment(ArticleCommentStatus.ALLOW.getValue());
        article.setArticleViewCount(0);
        article.setArticleLikeCount(0);
        article.setArticleCommentCount(0);
        article.setArticleOrder(1);
        articleMapper.insert(article);
        //添加分类和文章关联
        for (int i = 0; i < article.getCategoryList().size(); i++) {
            ArticleCategoryRef articleCategoryRef = new ArticleCategoryRef(article.getArticleId(), article.getCategoryList().get(i).getCategoryId());
            articleCategoryRefMapper.insert(articleCategoryRef);
        }
        //添加标签和文章关联
        for (int i = 0; i < article.getTagList().size(); i++) {
            ArticleTagRef articleTagRef = new ArticleTagRef(article.getArticleId(), article.getTagList().get(i).getTagId());
            articleTagRefMapper.insert(articleTagRef);
        }
    }


    @Override
    public void updateCommentCount(Integer articleId) {
        articleMapper.updateCommentCount(articleId);
    }

    @Override
    public Article getLastUpdateArticle() {
        return articleMapper.getLastUpdateArticle();
    }

    @Override
    public List<Article> listArticleByCategoryId(Integer cateId, Integer limit) {
        return articleMapper.findArticleByCategoryId(cateId, limit);
    }

    @Override
    public List<Article> listArticleByCategoryIds(List<Integer> cateIds, Integer limit) {
        if (cateIds == null || cateIds.size() == 0) {
            return null;
        }
        return articleMapper.findArticleByCategoryIds(cateIds, limit);
    }

    @Override
    public List<Integer> listCategoryIdByArticleId(Integer articleId) {
        return articleCategoryRefMapper.selectCategoryIdByArticleId(articleId);
    }

    @Override
    public List<Article> listAllNotWithContent() {
        return articleMapper.listAllNotWithContent();
    }

    @Override
    public List<Integer> listArticleIdByUserId(Integer articleUserId) {
        return articleMapper.selectArticleIdByUserId(articleUserId);
    }


}

2.Category

  • 获得分类总数
  • 获得分类列表
  • 获得分类列表(设置分类下的文章数)
  • 根据用户ID获得分类列表
  • 删除分类
  • 根据分类ID查询
  • 添加分类
  • 编辑分类
  • 根据名称查询分类

2.1 接口CategoryService

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.Category;

import java.util.List;

public interface CategoryService {

    //获得分类总数
    Integer countCategory();

    //获得分类列表
    List<Category> listCategory();

    //获得分类列表
    List<Category> listCategoryWithCount();

    //根据用户ID获得分类列表
    List<Category> listCategoryWithCountByUserId(Integer categoryUserId);

    //删除分类
    void deleteCategory(Integer id);

    //根据ID查询分类信息
    Category getCategoryById(Integer id);

    //添加分类
    Category insertCategory(Category category);

    //更新
    void updateCategory(Category category);

    //根据名称查询
    Category getCategoryByName(String name);
}

2.2 实现类CategoryServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Category;
import com.test.ssm.blog.mapper.ArticleCategoryRefMapper;
import com.test.ssm.blog.mapper.CategoryMapper;
import com.test.ssm.blog.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    CategoryMapper categoryMapper;

    @Autowired(required = false)
    ArticleCategoryRefMapper articleCategoryRefMapper;

    @Override
    public Integer countCategory() {
        Integer count = 0;
        try {
            count = categoryMapper.countCategory();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("统计分类失败, cause:{}", e);
        }
        return count;
    }

    @Override
    public List<Category> listCategory() {
        List<Category> categoryList = null;
        try {
            categoryList = categoryMapper.listCategory();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据文章获得分类列表失败, cause:{}", e);
        }
        return categoryList;
    }

    //获得分类列表的时候,设置了和这个分类有关的文章数
    @Override
    public List<Category> listCategoryWithCount() {
       List<Category> categoryList=null;
       try{
           categoryList=categoryMapper.listCategory();
           for(int i=0;i<categoryList.size();i++)
           {
               //根据分类的ID去查找这个分类下的文章数
               Integer count=articleCategoryRefMapper.countArticleByCategoryId(categoryList.get(i).getCategoryId());
               categoryList.get(i).setArticleCount(count);
           }
       }catch (Exception e) {
           e.printStackTrace();
           log.error("根据文章获得分类列表失败, cause:{}", e);
       }
        return categoryList;
    }

    @Override
    public List<Category> listCategoryWithCountByUserId(Integer categoryUserId) {
        List<Category> categoryList=null;
        try
        {
            categoryList=categoryMapper.listCategoryByUserId(categoryUserId);
            for(int i=0;i<categoryList.size();i++){
                Integer count=articleCategoryRefMapper.countArticleByCategoryId(categoryList.get(i).getCategoryId());
                categoryList.get(i).setArticleCount(count);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return categoryList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategory(Integer id) {
        try{
            //因为文章和分类有关,所以在分类中删除,也要在文章和分类关联的表里删除
            categoryMapper.deleteCategory(id);
            articleCategoryRefMapper.deleteByCategoryId(id);
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除分类失败, id:{}, cause:{}", id, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    public Category getCategoryById(Integer id) {
        return categoryMapper.getCategoryById(id);
    }

    @Override
    public Category insertCategory(Category category) {
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public void updateCategory(Category category) {
        categoryMapper.update(category);
    }

    @Override
    public Category getCategoryByName(String name) {
        Category category = null;
        try {
            category = categoryMapper.getCategoryByName(name);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("更新分类失败, category:{}, cause:{}", category, e);
        }
        return category;
    }
}

3.Tag

  • 获得标签总数
  • 获得标签列表
  • 获得标签列表(设置标签下的文章数)
  • 根据ID查询信息
  • 添加标签
  • 删除标签
  • 更新标签
  • 根据名称获得标签
  • 根据文章ID获得标签列表
  • 根据用户ID获得标签列表

3.1 接口TagService

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.Tag;
import javafx.scene.control.TableFocusModel;

import java.util.List;

public interface TagService {

    //获得总数
    Integer countTag();

    //获得标签列表
    List<Tag> listTag();

    //获得标签列表,及文章数目
    List<Tag> listTagWithCount();

    //根据ID
    Tag getTagById(Integer id);

    //添加
    Tag insertTag(Tag tag);

    //更新
    void updateTag(Tag tag);

    //删除
    void deleteTag(Integer id);

    //根据名称
    Tag getTagByName(String name);

    //根据文章ID获得标签
    List<Tag> listTagByArticleId(Integer articleId);

    List<Tag> listTagByUserId(Integer tagUserId);
}

3.2 实现类TageServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Tag;
import com.test.ssm.blog.mapper.ArticleTagRefMapper;
import com.test.ssm.blog.mapper.TagMapper;
import com.test.ssm.blog.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Slf4j
public class TagServiceImpl implements TagService {

    @Autowired(required = false)
    TagMapper tagMapper;

    @Autowired(required = false)
    ArticleTagRefMapper articleTagRefMapper;

    @Override
    public Integer countTag() {
        return tagMapper.countTag();
    }

    @Override
    public List<Tag> listTag() {
        List<Tag> tagList = null;
        try {
            tagList = tagMapper.listTag();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获得所有标签失败, cause:{}", e);
        }
        return tagList;
    }

    @Override
    public List<Tag> listTagWithCount() {
        List<Tag> tagList = null;
        try {
            tagList = tagMapper.listTag();
            for (int i = 0; i < tagList.size(); i++) {
                Integer count = articleTagRefMapper.countArticleByTagId(tagList.get(i).getTagId());
                tagList.get(i).setArticleCount(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获得所有标签失败, cause:{}", e);
        }
        return tagList;

    }

    @Override
    public Tag getTagById(Integer id) {
        return tagMapper.getTagById(id);
    }

    @Override
    public Tag insertTag(Tag tag) {
        tagMapper.insert(tag);
        return tag;
    }

    @Override
    public void updateTag(Tag tag) {
        tagMapper.insert(tag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTag(Integer id) {
        try {
            tagMapper.deleteById(id);
            articleTagRefMapper.deleteByTagId(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除标签失败, id:{}, cause:{}", id, e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    public Tag getTagByName(String name) {
        Tag tag = null;
        try {
            tag = tagMapper.getTagByName(name);
        } catch (Exception e) {            e.printStackTrace();
            log.error("根据名称获得标签, name:{}, cause:{}", name, e);
        }
        return tag;
    }

    @Override
    public List<Tag> listTagByArticleId(Integer articleId) {
        List<Tag> tagList = null;
        try {
            tagList = articleTagRefMapper.listTagByArticleId(articleId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据文章ID获得标签失败,articleId:{}, cause:{}", articleId, e);
        }
        return tagList;
    }

    @Override
    public List<Tag> listTagByUserId(Integer tagUserId) {
        List<Tag> tagList=null;
        try
        {
            tagList=tagMapper.listTagByUserId(tagUserId);
            for(int i=0;i<tagList.size();i++)
            {
                Integer count=articleTagRefMapper.countArticleByTagId(tagList.get(i).getTagId());
                tagList.get(i).setArticleCount(count);
            }

        }catch (Exception e) {
            e.printStackTrace();
        }
        return tagList;
    }
}

4.Comment

  • 添加评论
  • 根据文章ID获得评论列表
  • 根据评论ID获得评论
  • 获得所有评论
  • 获得评论列表
  • 删除评论
  • 更新评论
  • 统计评论数
  • 获得评论子评论
  • 获得最新评论
  • 根据文章列表ID查找评论列表

4.1 接口CommentService

package com.test.ssm.blog.service;

import com.github.pagehelper.PageInfo;
import com.test.ssm.blog.entity.Article;
import com.test.ssm.blog.entity.Comment;

import java.util.List;

public interface CommentService {

    //添加
    void insertComment(Comment comment);

    //根据文章ID获取评论列表
    List<Comment> listCommentByArticleId(Integer articleId);

    //根据ID获取
    Comment getCommentById(Integer id);


    /**
     * 获得所有评论
     * pageIndex 第几页开始
     * pageSize 一页显示的数量
     */
    PageInfo<Comment> listCommentByPage(Integer pageIndex,Integer pageSize);

    //获得评论列表
    List<Comment> listComment();

    //删除评论
    void deleteComment(Integer id);

    //更新
    void updateComment(Comment comment);

    //统计评论数
    Integer countComment();

    //获得最新评论
    List<Comment> listRecentComment(Integer limit);

    //获得评论的子评论
    List<Comment> listChildComment(Integer id);

    PageInfo<Comment> listCommentByArticleIds(Integer pageIndex,Integer pageSize,List<Integer> articleIds);

}

4.2 实现类CommentServiceImpl 

package com.test.ssm.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.ssm.blog.entity.Article;
import com.test.ssm.blog.entity.Comment;
import com.test.ssm.blog.entity.Page;
import com.test.ssm.blog.enums.ArticleStatus;
import com.test.ssm.blog.mapper.ArticleMapper;
import com.test.ssm.blog.mapper.CommentMapper;
import com.test.ssm.blog.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService {

    @Autowired(required = false)
    private CommentMapper commentMapper;

    @Autowired(required = false)
    private ArticleMapper articleMapper;

    @Override
    public void insertComment(Comment comment) {
        try
        {
            commentMapper.insert(comment);
        }catch (Exception e)
        {
            e.printStackTrace();
            log.error("创建评论失败:comment:{},cause:{}",comment,e);
        }

    }

    @Override
    public List<Comment> listCommentByArticleId(Integer articleId) {
        List<Comment> commentList=null;
        try {
            commentList=commentMapper.listCommentByArticleId(articleId);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("根据文章ID获得评论列表失败,articleId:{},cause:{}", articleId, e);
        }
        return commentList;

    }

    @Override
    public Comment getCommentById(Integer id) {
        Comment comment=null;
        try{
            comment=commentMapper.getCommentById(id);
        }catch (Exception e){
            e.printStackTrace();
            log.error("根据评论ID获得评论,id:{}, cause:{}", id, e);
        }
        return comment;
    }

    @Override
    public PageInfo<Comment> listCommentByPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Comment> commentList=null;
        try{
            commentList=commentMapper.listComment();
            for(int i=0;i<commentList.size();i++)
            {
                //根据文章状态(已发布)和评论中的文章ID获得文章
                Article article=articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(),commentList.get(i).getCommentArticleId());
               //comment中有一个这样的属性      private  Article article;  //非数据库字段。评论的文章
                commentList.get(i).setArticle(article);
            }
        }catch (Exception e) {
            e.printStackTrace();
            log.error("分页获得评论失败,pageIndex:{}, pageSize:{}, cause:{}", pageIndex, pageSize, e);
        }
        return new PageInfo<>(commentList);
    }

    @Override
    public List<Comment> listComment() {
       List<Comment> commentList=null;
       try
       {
           commentList=commentMapper.listComment();
       } catch (Exception e) {
           e.printStackTrace();
           log.error("获得评论列表失败:cause:{}", e);
       }
       return commentList;

    }

    @Override
    public void deleteComment(Integer id) {
        commentMapper.deleteById(id);
    }

    @Override
    public void updateComment(Comment comment) {
        commentMapper.update(comment);
    }

    @Override
    public Integer countComment() {
        Integer commentCount = null;
        try {
            commentCount = commentMapper.countComment();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("统计评论数量失败, cause:{}", e);
        }
        return commentCount;
    }

    @Override
    public List<Comment> listRecentComment(Integer limit) {
        List<Comment> commentList=null;
        try{
            commentList=commentMapper.listRecentComment(limit);
            for(int i=0;i<commentList.size();i++)
            {
                Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId());
                commentList.get(i).setArticle(article);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("获得最新评论失败, limit:{}, cause:{}", limit, e);
        }
        return  commentList;
    }

    @Override
    public List<Comment> listChildComment(Integer id) {
       List<Comment> childCommentList=null;
       try
       {
           childCommentList=commentMapper.listChildComment(id);
       }catch (Exception e) {
           e.printStackTrace();
           log.error("获得子评论失败, id:{}, cause:{}", id, e);
       }
        return childCommentList;

    }

    @Override
    public PageInfo<Comment> listCommentByArticleIds(Integer pageIndex, Integer pageSize,List<Integer> articleIds) {
        PageHelper.startPage(pageIndex,pageSize);
        System.out.println(articleIds.size());
        List<Comment> commentList=null;
        try{
            commentList=commentMapper.listCommentByArticleIds(articleIds);
            for(int i=0;i<commentList.size();i++){
                Article article = articleMapper.getArticleByStatusAndId(ArticleStatus.PUBLISH.getValue(), commentList.get(i).getCommentArticleId());
                commentList.get(i).setArticle(article);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return new PageInfo<>(commentList);
    }


}

5.Link

  • 获得链接总数
  • 获得链接列表
  • 添加链接
  • 删除链接
  • 更新链接
  • 根据ID查询链接 

5.1 接口LinkService

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.Link;

import java.util.List;

public interface LinkService {

    //获得链接总数
    Integer countLink(Integer status);

    //获得链接列表
    List<Link> listLink(Integer status);

    //添加
    void insertLink(Link link);

    //删除
    void deleteLink(Integer id);

    //更新
    void updateLink(Link link);

    //根据ID查询
    Link getLinkById(Integer id);

}

5.2 实现类LinkServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Link;
import com.test.ssm.blog.mapper.LinkMapper;
import com.test.ssm.blog.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LinkServiceImpl implements LinkService {

    @Autowired(required = false)
    LinkMapper linkMapper;

    @Override
    public Integer countLink(Integer status) {
        return linkMapper.countLink(status);
    }

    @Override
    public List<Link> listLink(Integer status) {
        return linkMapper.listLink(status);
    }

    @Override
    public void insertLink(Link link) {
        linkMapper.insert(link);
    }

    @Override
    public void deleteLink(Integer id) {
        linkMapper.deleteById(id);
    }

    @Override
    public void updateLink(Link link) {
        linkMapper.update(link);
    }

    @Override
    public Link getLinkById(Integer id) {
        return linkMapper.getLinkById(id);
    }
}

6.Menu

  • 获得菜单列表
  • 添加菜单
  • 删除菜单
  • 更新菜单
  • 根据ID查询菜单

6.1 接口MenuService

package com.test.ssm.blog.service;


import com.test.ssm.blog.entity.Menu;

import java.util.List;

public interface MenuService {

    //获得菜单列表
    List<Menu> listMenu();

    //添加菜单项目
    Menu insertMenu(Menu menu);

    //删除菜单项目
    void deleteMenu(Integer id);

    //更新
    void updateMenu(Menu menu);

    //根据ID获取菜单项目信息
    Menu getMenuById(Integer id);
}

6.2 实现类MenuServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Menu;
import com.test.ssm.blog.mapper.MenuMapper;
import com.test.ssm.blog.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired(required = false)
    MenuMapper menuMapper;

    @Override
    public List<Menu> listMenu() {
        List<Menu> menuList=menuMapper.listMenu();
        return menuList;
    }

    @Override
    public Menu insertMenu(Menu menu) {
        menuMapper.insert(menu);
        return menu;
    }

    @Override
    public void deleteMenu(Integer id) {
        menuMapper.deleteById(id);
    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.update(menu);
    }

    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.getMenuById(id);
    }
}

7.Notice

  • 获得公告列表
  • 添加公告
  • 删除公告
  • 更新公告
  • 根据ID查询公告

7.1 接口NoticeService

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.Notice;

import java.util.List;

public interface NoticeService {

    //获得公告列表
    List<Notice> listNotice(Integer status);

    //添加公告
    void insertNotice(Notice notice);

    //删除
    void deleteNotice(Integer id);

    //更新
    void updateNotice(Notice notice);

    //根据ID查询
    Notice getNoticeById(Integer id);
}

7.2 实现类NoticeServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Notice;
import com.test.ssm.blog.mapper.NoticeMapper;
import com.test.ssm.blog.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired(required = false)
   NoticeMapper noticeMapper;


    @Override
    public List<Notice> listNotice(Integer status) {
        return noticeMapper.listNotice(status);
    }

    @Override
    public void insertNotice(Notice notice) {
       noticeMapper.insert(notice);
    }

    @Override
    public void deleteNotice(Integer id) {
      noticeMapper.deleteById(id);
    }

    @Override
    public void updateNotice(Notice notice) {
        noticeMapper.update(notice);
    }

    @Override
    public Notice getNoticeById(Integer id) {
        return noticeMapper.getNoticeById(id);
    }
}

8.Options

  • 获得站点信息
  • 新建站点
  • 更新站点

8.1 接口OptionsService

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.Options;

public interface OptionsService {

    //获得基本信息
    Options getOptions();

    //新建基本信息
    void insertOptions(Options options);

    //更新基本信息
    void updateOptions(Options options);
}

8.2 实现类OptionsServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Options;
import com.test.ssm.blog.mapper.OptionsMapper;
import com.test.ssm.blog.service.OptionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class OptionsServiceImpl implements OptionsService {

    @Autowired(required = false)
    OptionsMapper optionsMapper;

    @Override
    @Cacheable(value = "default", key = "'options'")
    public Options getOptions() {
        return optionsMapper.getOptions();
    }

    /**
     * @Cacheable : 主要针对方法配置,能够根据方法的请求参数对结果进行缓存
     * 参数值:
     *         value 缓存的名称
     *         key 缓存的key(如果指定要按照SpEL表达式编写,如果不指定,则缺省按照方法的所有参数进行组合
     *         condition 缓存的条件,可以为空
     *  加这个注解的意思,当调用这个方法时,会从一个名叫value值的缓存中查询,如果没有,则执行实际方法(即查询数据库),并将执行结果保存在缓存中
     *                   否则返回缓存中的对象
     * @param options
     */
    @Override
    @Cacheable(value = "default", key = "'options'")
    public void insertOptions(Options options) {
        optionsMapper.insert(options);
    }

    @Override
    @Cacheable(value = "default", key = "'options'")
    public void updateOptions(Options options) {
        optionsMapper.update(options);
    }
}

9.User

  • 获得用户列表
  • 根据ID查询用户
  • 添加用户
  • 删除用户
  • 更新用户信息
  • 根据用户名或邮箱查询用户
  • 根据用户名查询用户
  • 根据邮箱查询用户

9.1 接口UserService

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.User;

import java.util.List;

public interface UserService {
    /**
     * 获得用户列表
     */
    List<User> listUser();

    /**
     * 根据ID查询信息
     */
    User getUserById(Integer id);

    /**
     * 修改用户信息
     */
    void updateUser(User user);

    /**
     * 删除用户
     */
    void deleteUser(Integer id);

    /**
     * 添加用户
     */
    User insertUser(User user);

    /**
     * 根据用户名或邮箱查询用户
     */
    User getUserByNameOrEmail(String str);

    /**
     * 根据用户名查询用户
     */
    User getUserByName(String name);

    /**
     * 根据邮箱查询用户
     */
    User getUserByEmail(String email);

}

9.2 实现类UserServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.User;
import com.test.ssm.blog.mapper.ArticleMapper;
import com.test.ssm.blog.mapper.UserMapper;
import com.test.ssm.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private ArticleMapper articleMapper;

    @Override
    public List<User> listUser() {
        List<User> userList=userMapper.listUser();
        for (int i=0;i<userList.size();i++)
        {
            Integer articleCount=articleMapper.countArticleByUser(userList.get(i).getUserId());
            userList.get(i).setArticleCount(articleCount);
        }
        return userList;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public void updateUser(User user) {
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteById(id);
    }

    @Override
    public User insertUser(User user) {
       user.setUserRegisterTime(new Date());
       userMapper.insert(user);
       return user;
    }

    @Override
    public User getUserByNameOrEmail(String str) {
        return userMapper.getUserByNameOrEmail(str);
    }

    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }

    @Override
    public User getUserByEmail(String email) {
        return userMapper.getUserByEmail(email);
    }
}

10.Page

  • 获得页面列表
  • 根据页面关键字获得页面列表
  • 添加页面
  • 删除页面
  • 编辑页面

10.1 接口PageService 

package com.test.ssm.blog.service;

import com.test.ssm.blog.entity.Page;

import java.util.List;

public interface PageService {

    //获得页面列表
    List<Page> listPage(Integer status);

    //根据页面key获得页面
    Page getPageByKey(Integer status,String key);

    //根据ID获取页面
    Page getPageById(Integer id);

    //添加页面
    void insertPage(Page page);

    //删除页面
    void deletePage(Integer id);

    //编辑页面
    void updatePage(Page page);
}

10.2 实现类PageServiceImpl

package com.test.ssm.blog.service.impl;

import com.test.ssm.blog.entity.Page;
import com.test.ssm.blog.mapper.PageMapper;
import com.test.ssm.blog.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PageServiceImpl implements PageService {

    @Autowired(required = false)
    PageMapper pageMapper;

    @Override
    public List<Page> listPage(Integer status) {
        return pageMapper.listPage(status);
    }

    @Override
    public Page getPageByKey(Integer status, String key) {
        return pageMapper.getPageByKey(status,key);
    }

    @Override
    public Page getPageById(Integer id) {
        return pageMapper.getPageById(id);
    }

    @Override
    public void insertPage(Page page) {
        pageMapper.insert(page);
    }

    @Override
    public void deletePage(Integer id) {
        pageMapper.deleteById(id);
    }

    @Override
    public void updatePage(Page page) {
        pageMapper.update(page);
    }
}

 

 类似资料: