5、文章修改
这里需要注意的是在修改文章的时候可能会修改标签,这里我不管标签有没有修改都先把标签删除掉,然后再添加。
在ArticleTagService.java
类中再添加一个接口,根据文章id删除关联表的标签数据。
/** * 根据文章id删除关联表的标签数据 * * @param articleId */ void deleteTag(Integer articleId);
实现类ArticleTagServiceImpl.java
@Override public void deleteTag(Integer articleId) { articleTagMapper.deleteByArticleId(articleId); }
数据操作接口ArticleTagMapper.java
/** * 根据文章id删除关联表的数据 * @param articleId */ void deleteByArticleId(@Param("articleId") Integer articleId);
xml
文件的数据操作
<delete id="deleteByArticleId" parameterType="java.lang.Integer"> DELETE FROM person_article_tag WHERE article_id = #{articleId} </delete>
文章修改的代码:
修改文章基本上和添加的一样,就多了个删除的操作。
@Override public void updateArticle(Article article) { articleMapper.updateArticle(article); articleMap.put(article.getId(), article); //更新文章先把原来的标签删除掉 articleTagService.deleteTag(article.getId()); //添加文章标签 if (article.getTagIdList() != null) { List<ArticleTag> articleTagList = article.getTagIdList().stream().map(tagId -> ArticleTag.builder() .tagId(tagId) .articleId(article.getId()) .build()).collect(Collectors.toList()); articleTagService.insertBatch(articleTagList); } }
6、删除文章
删除文章的时候也要将关联的标签删除掉
@Override public void deleteArticle(Integer articleId) { articleMapper.deleteArticle(articleId); articleMap.remove(articleId); //关联标签删除掉 articleTagService.deleteTag(articleId); }
好啦,还剩一个文章列表的方法
文章列表查询的时候关联的标签也要查出来,所以我们还要根据文章id去查找关联表中的标签
在ArticleTagService.java
中添加一个查找的接口
/** * 根据文章id查找出所有的关联标签数据 * * @param articleId * @return */ List<ArticleTag> findArticleTagById(Integer articleId);
实现类:
@Override public List<ArticleTag> findArticleTagById(Integer articleId) { List<ArticleTag> articleTagList = articleTagMapper.getArticleTagById(articleId); return articleTagList; }
数据库接口:
/** * 根据文章id查找出所有的关联标签数据 * @param articleId * @return */ List<ArticleTag> getArticleTagById(@Param("articleId") Integer articleId);
xml文件:
<select id="getArticleTagById" resultMap="BaseResultMap"> SELECT * FROM person_article_tag WHERE article_id = #{articleId} </select>
准备工作完成,接下里修改文章列表,将查找的文章进行遍历,再将标签整合到返回的对象中。
@Override public List<Article> getArticlePage(ArticleBO articleBO) { int pageNum = articleBO.getPageNum(); int pageSize = articleBO.getPageSize(); PageHelper.startPage(pageNum,pageSize); List<Article> articleList = articleMapper.getArticlePage(articleBO); List<Tag> tagList = new ArrayList<>(); if (articleList != null) { for (Article article : articleList) { List<ArticleTag> articleTags = articleTagService.findArticleTagById(article.getId()); if (articleTags != null) { for (ArticleTag articleTag : articleTags) { Tag tag = tagService.findTagById(articleTag.getTagId()); tagList.add(tag); } } article.setTagList(tagList); } } return articleList; }
好啦,到目前为止,文章的开发整体结束,接下来的一篇写操作日志,还差个登录,基本上我们的博客系统就比较完善了,还有数据的统计,一步步的来,自己也可以先写写。
二、完整代码
这里我粘贴一下完整的代码(只贴修改的类)。供大家参考
1、ArticleServiceImpl.java
package com.blog.personalblog.service.Impl; import com.blog.personalblog.bo.ArticleBO; import com.blog.personalblog.config.mail.MailInfo; import com.blog.personalblog.config.mail.SendMailConfig; import com.blog.personalblog.entity.Article; import com.blog.personalblog.entity.ArticleTag; import com.blog.personalblog.entity.Tag; import com.blog.personalblog.entity.User; import com.blog.personalblog.mapper.ArticleMapper; import com.blog.personalblog.service.ArticleService; import com.blog.personalblog.service.ArticleTagService; import com.blog.personalblog.service.TagService; import com.blog.personalblog.service.UserService; import com.github.pagehelper.PageHelper; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.text.MessageFormat; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * @author: SuperMan * @create: 2021-12-01 */ @Log4j2 @Service public class ArticleServiceImpl implements ArticleService { @Autowired ArticleMapper articleMapper; @Resource ArticleTagService articleTagService; @Resource UserService userService; @Resource TagService tagService; /** * key:文章id * value: 文章 */ Map<Integer, Article> articleMap = new LinkedHashMap<>(); @Override @PostConstruct public void init() { List<Article> articleList = articleMapper.findAll(); try { for(Article article : articleList) { articleMap.put(article.getId(), article); } log.info("文章缓存数据加载完成"); } catch (Exception e) { log.error("文章缓存数据加载失败!", e.getMessage()); } } @Override public List<Article> getArticlePage(ArticleBO articleBO) { int pageNum = articleBO.getPageNum(); int pageSize = articleBO.getPageSize(); PageHelper.startPage(pageNum,pageSize); List<Article> articleList = articleMapper.getArticlePage(articleBO); List<Tag> tagList = new ArrayList<>(); if (articleList != null) { for (Article article : articleList) { List<ArticleTag> articleTags = articleTagService.findArticleTagById(article.getId()); if (articleTags != null) { for (ArticleTag articleTag : articleTags) { Tag tag = tagService.findTagById(articleTag.getTagId()); tagList.add(tag); } } article.setTagList(tagList); } } return articleList; } @Override public void saveArticle(Article article) { articleMapper.createArticle(article); articleMap.put(article.getId(), article); //添加文章标签 if (article.getTagIdList() != null) { List<ArticleTag> articleTagList = article.getTagIdList().stream().map(tagId -> ArticleTag.builder() .tagId(tagId) .articleId(article.getId()) .build()).collect(Collectors.toList()); articleTagService.insertBatch(articleTagList); } User user = userService.findByUserId(article.getUserId()); //添加文章发送邮箱提醒 String content = "【{0}】您好:\n" + "您已成功发布了标题为: {1} 的文章 \n" + "请注意查收!\n"; MailInfo build = MailInfo.builder() .receiveMail(user.getEmail()) .content(MessageFormat.format(content, user.getUserName(), article.getTitle())) .title("文章发布") .build(); SendMailConfig.sendMail(build); } @Override public void updateArticle(Article article) { articleMapper.updateArticle(article); articleMap.put(article.getId(), article); //更新文章先把原来的标签删除掉 articleTagService.deleteTag(article.getId()); //添加文章标签 if (article.getTagIdList() != null) { List<ArticleTag> articleTagList = article.getTagIdList().stream().map(tagId -> ArticleTag.builder() .tagId(tagId) .articleId(article.getId()) .build()).collect(Collectors.toList()); articleTagService.insertBatch(articleTagList); } } @Override public void deleteArticle(Integer articleId) { articleMapper.deleteArticle(articleId); articleMap.remove(articleId); //关联标签删除掉 articleTagService.deleteTag(articleId); } @Override public Article findById(Integer articleId) { Article article = articleMap.get(articleId); if (article == null) { Article art = articleMapper.getById(articleId); return art; } return article; } }
2、ArticleTagServiceImpl.java
package com.blog.personalblog.service.Impl; import com.blog.personalblog.entity.ArticleTag; import com.blog.personalblog.mapper.ArticleTagMapper; import com.blog.personalblog.service.ArticleTagService; import lombok.extern.log4j.Log4j2; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; /** * @author: SuperMan * @create: 2022-01-24 **/ @Log4j2 @Service public class ArticleTagServiceImpl implements ArticleTagService { @Resource ArticleTagMapper articleTagMapper; @Override public void insertBatch(List<ArticleTag> articleTagList) { try { articleTagMapper.insertBatch(articleTagList); } catch (Exception e) { log.error("批量添加文章标签失败!" + e.getMessage()); } } @Override public void deleteTag(Integer articleId) { articleTagMapper.deleteByArticleId(articleId); } @Override public List<ArticleTag> findArticleTagById(Integer articleId) { List<ArticleTag> articleTagList = articleTagMapper.getArticleTagById(articleId); return articleTagList; } }
3、ArticleTagService.java
package com.blog.personalblog.service; import com.blog.personalblog.entity.ArticleTag; import java.util.List; /** * @author: SuperMan * @create: 2022-01-24 **/ public interface ArticleTagService { /** * 批量插入文章标签数据 * * @param articleTagList */ void insertBatch(List<ArticleTag> articleTagList); /** * 根据文章id删除关联表的标签数据 * * @param articleId */ void deleteTag(Integer articleId); /** * 根据文章id查找出所有的关联标签数据 * * @param articleId * @return */ List<ArticleTag> findArticleTagById(Integer articleId); }
4、ArticleTagMapper.java
package com.blog.personalblog.mapper; import com.blog.personalblog.entity.ArticleTag; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; import java.util.List; /** * @author: SuperMan * @create: 2022-01-24 **/ @Repository public interface ArticleTagMapper { /** * 批量插入 * @param articleTagList */ int insertBatch(@Param("articleTagList") List<ArticleTag> articleTagList); /** * 根据文章id删除关联表的数据 * @param articleId */ void deleteByArticleId(@Param("articleId") Integer articleId); /** * 根据文章id查找出所有的关联标签数据 * @param articleId * @return */ List<ArticleTag> getArticleTagById(@Param("articleId") Integer articleId); }
5、ArticleTagMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.blog.personalblog.mapper.ArticleTagMapper"> <resultMap id="BaseResultMap" type="com.blog.personalblog.entity.ArticleTag"> <result column="id" jdbcType="INTEGER" property="id"/> <result column="article_id" jdbcType="INTEGER" property="articleId"/> <result column="tag_id" jdbcType="INTEGER" property="tagId"/> </resultMap> <insert id="insertBatch"> INSERT INTO person_article_tag <trim prefix="(" suffix=")" suffixOverrides=","> article_id, tag_id </trim> VALUES <foreach collection="articleTagList" item="item" separator=","> <trim prefix="(" suffix=")" suffixOverrides=","> #{item.articleId}, #{item.tagId} </trim> </foreach> </insert> <delete id="deleteByArticleId" parameterType="java.lang.Integer"> DELETE FROM person_article_tag WHERE article_id = #{articleId} </delete> <select id="getArticleTagById" resultMap="BaseResultMap"> SELECT * FROM person_article_tag WHERE article_id = #{articleId} </select> </mapper>
三、测试
添加文章测试
这里你要确定标签表里要有添加的标签id。
{ "author": "码上言1", "title": "我的第一篇博客上线啦!", "userId": 1, "categoryId": 2, "content": "这是博客的内容", "views": 0, "totalWords": 0, "commentableId": 1, "artStatus": 0, "description": "这是备注", "imageUrl": "www.baidu.com", "tagIdList":[ 1,2] }
修改和删除大家自己测试一下