SpringBoot前后端分離實現(xiàn)個人博客系統(tǒng)
一、項目簡介
本項目使用springboot+mybatis+前端vue,使用前后端分離架構實現(xiàn)的個人博客系統(tǒng),共7個模塊,首頁,寫博客,博客詳情頁,評論管理,文章分類,標簽管理和文章歸檔。該項目沒有后端管理功能,比較適合用于大作業(yè)!
二、環(huán)境介紹
語言環(huán)境:Java: jdk1.8
數(shù)據(jù)庫:Mysql: mysql5.7/redis
應用服務器:Tomcat: tomcat8.5.31
開發(fā)工具:IDEA或eclipse
項目管理工具: maven
三、系統(tǒng)展示
首頁

文章分類

標簽

登錄
發(fā)布文章

四、核心代碼展示
package com.mszlu.blog.controller;
import com.mszlu.blog.common.aop.LogAnnotation;
import com.mszlu.blog.common.cache.Cache;
import com.mszlu.blog.service.ArticleService;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.params.ArticleParam;
import com.mszlu.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//json數(shù)據(jù)進行交互
@RestController
@RequestMapping("articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
/**
* 首頁 文章列表
* @param pageParams
* @return
*/
@PostMapping
//加上此注解 代表要對此接口記錄日志
@LogAnnotation(module="文章",operator="獲取文章列表")
@Cache(expire = 5 * 60 * 1000,name = "listArticle")
public Result listArticle(@RequestBody PageParams pageParams){
// int i = 10/0;
return articleService.listArticle(pageParams);
}
/**
* 首頁 最熱文章
* @return
*/
@PostMapping("hot")
@Cache(expire = 5 * 60 * 1000,name = "hot_article")
public Result hotArticle(){
int limit = 5;
return articleService.hotArticle(limit);
}
/**
* 首頁 最新文章
* @return
*/
@PostMapping("new")
@Cache(expire = 5 * 60 * 1000,name = "news_article")
public Result newArticles(){
int limit = 5;
return articleService.newArticles(limit);
}
/**
* 首頁 最新文章
* @return
*/
@PostMapping("listArchives")
public Result listArchives(){
return articleService.listArchives();
}
@PostMapping("view/{id}")
public Result findArticleById(@PathVariable("id") Long articleId){
return articleService.findArticleById(articleId);
}
//接口url:/articles/publish
//
//請求方式:POST
@PostMapping("publish")
public Result publish(@RequestBody ArticleParam articleParam){
return articleService.publish(articleParam);
}
}package com.mszlu.blog.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mszlu.blog.dao.dos.Archives;
import com.mszlu.blog.dao.mapper.ArticleBodyMapper;
import com.mszlu.blog.dao.mapper.ArticleMapper;
import com.mszlu.blog.dao.mapper.ArticleTagMapper;
import com.mszlu.blog.dao.pojo.Article;
import com.mszlu.blog.dao.pojo.ArticleBody;
import com.mszlu.blog.dao.pojo.ArticleTag;
import com.mszlu.blog.dao.pojo.SysUser;
import com.mszlu.blog.service.*;
import com.mszlu.blog.utils.UserThreadLocal;
import com.mszlu.blog.vo.ArticleBodyVo;
import com.mszlu.blog.vo.ArticleVo;
import com.mszlu.blog.vo.Result;
import com.mszlu.blog.vo.TagVo;
import com.mszlu.blog.vo.params.ArticleParam;
import com.mszlu.blog.vo.params.PageParams;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
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.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleMapper articleMapper;
@Autowired
private TagService tagService;
@Autowired
private SysUserService sysUserService;
@Autowired
private ArticleTagMapper articleTagMapper;
@Override
public Result listArticle(PageParams pageParams) {
Page<Article> page = new Page<>(pageParams.getPage(),pageParams.getPageSize());
IPage<Article> articleIPage = articleMapper.listArticle(
page,
pageParams.getCategoryId(),
pageParams.getTagId(),
pageParams.getYear(),
pageParams.getMonth());
List<Article> records = articleIPage.getRecords();
return Result.success(copyList(records,true,true));
}
// @Override
// public Result listArticle(PageParams pageParams) {
// /**
// * 1. 分頁查詢 article數(shù)據(jù)庫表
// */
// Page<Article> page = new Page<>(pageParams.getPage(),pageParams.getPageSize());
// LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
// if (pageParams.getCategoryId() != null){
// // and category_id=#{categoryId}
// queryWrapper.eq(Article::getCategoryId,pageParams.getCategoryId());
// }
// List<Long> articleIdList = new ArrayList<>();
// if (pageParams.getTagId() != null){
// //加入標簽 條件查詢
// //article表中 并沒有tag字段 一篇文章 有多個標簽
// //article_tag article_id 1 : n tag_id
// LambdaQueryWrapper<ArticleTag> articleTagLambdaQueryWrapper = new LambdaQueryWrapper<>();
// articleTagLambdaQueryWrapper.eq(ArticleTag::getTagId,pageParams.getTagId());
// List<ArticleTag> articleTags = articleTagMapper.selectList(articleTagLambdaQueryWrapper);
// for (ArticleTag articleTag : articleTags) {
// articleIdList.add(articleTag.getArticleId());
// }
// if (articleIdList.size() > 0){
// // and id in(1,2,3)
// queryWrapper.in(Article::getId,articleIdList);
// }
// }
// //是否置頂進行排序
// //order by create_date desc
// queryWrapper.orderByDesc(Article::getWeight,Article::getCreateDate);
// Page<Article> articlePage = articleMapper.selectPage(page, queryWrapper);
// List<Article> records = articlePage.getRecords();
// //能直接返回嗎? 很明顯不能
// List<ArticleVo> articleVoList = copyList(records,true,true);
// return Result.success(articleVoList);
// }
@Override
public Result hotArticle(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Article::getViewCounts);
queryWrapper.select(Article::getId,Article::getTitle);
queryWrapper.last("limit "+limit);
//select id,title from article order by view_counts desc limit 5
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}
@Override
public Result newArticles(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Article::getCreateDate);
queryWrapper.select(Article::getId,Article::getTitle);
queryWrapper.last("limit "+limit);
//select id,title from article order by create_date desc desc limit 5
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}
@Override
public Result listArchives() {
List<Archives> archivesList = articleMapper.listArchives();
return Result.success(archivesList);
}
@Autowired
private ThreadService threadService;
@Override
public Result findArticleById(Long articleId) {
/**
* 1. 根據(jù)id查詢 文章信息
* 2. 根據(jù)bodyId和categoryid 去做關聯(lián)查詢
*/
Article article = this.articleMapper.selectById(articleId);
ArticleVo articleVo = copy(article, true, true,true,true);
//查看完文章了,新增閱讀數(shù),有沒有問題呢?
//查看完文章之后,本應該直接返回數(shù)據(jù)了,這時候做了一個更新操作,更新時加寫鎖,阻塞其他的讀操作,性能就會比較低
// 更新 增加了此次接口的 耗時 如果一旦更新出問題,不能影響 查看文章的操作
//線程池 可以把更新操作 扔到線程池中去執(zhí)行,和主線程就不相關了
threadService.updateArticleViewCount(articleMapper,article);
return Result.success(articleVo);
}
@Override
public Result publish(ArticleParam articleParam) {
//此接口 要加入到登錄攔截當中
SysUser sysUser = UserThreadLocal.get();
/**
* 1. 發(fā)布文章 目的 構建Article對象
* 2. 作者id 當前的登錄用戶
* 3. 標簽 要將標簽加入到 關聯(lián)列表當中
* 4. body 內容存儲 article bodyId
*/
Article article = new Article();
article.setAuthorId(sysUser.getId());
article.setWeight(Article.Article_Common);
article.setViewCounts(0);
article.setTitle(articleParam.getTitle());
article.setSummary(articleParam.getSummary());
article.setCommentCounts(0);
article.setCreateDate(System.currentTimeMillis());
article.setCategoryId(Long.parseLong(articleParam.getCategory().getId()));
//插入之后 會生成一個文章id
this.articleMapper.insert(article);
//tag
List<TagVo> tags = articleParam.getTags();
if (tags != null){
for (TagVo tag : tags) {
Long articleId = article.getId();
ArticleTag articleTag = new ArticleTag();
articleTag.setTagId(Long.parseLong(tag.getId()));
articleTag.setArticleId(articleId);
articleTagMapper.insert(articleTag);
}
}
//body
ArticleBody articleBody = new ArticleBody();
articleBody.setArticleId(article.getId());
articleBody.setContent(articleParam.getBody().getContent());
articleBody.setContentHtml(articleParam.getBody().getContentHtml());
articleBodyMapper.insert(articleBody);
article.setBodyId(articleBody.getId());
articleMapper.updateById(article);
Map<String,String> map = new HashMap<>();
map.put("id",article.getId().toString());
return Result.success(map);
}
private List<ArticleVo> copyList(List<Article> records, boolean isTag, boolean isAuthor) {
List<ArticleVo> articleVoList = new ArrayList<>();
for (Article record : records) {
articleVoList.add(copy(record,isTag,isAuthor,false,false));
}
return articleVoList;
}
private List<ArticleVo> copyList(List<Article> records, boolean isTag, boolean isAuthor, boolean isBody,boolean isCategory) {
List<ArticleVo> articleVoList = new ArrayList<>();
for (Article record : records) {
articleVoList.add(copy(record,isTag,isAuthor,isBody,isCategory));
}
return articleVoList;
}
@Autowired
private CategoryService categoryService;
private ArticleVo copy(Article article, boolean isTag, boolean isAuthor, boolean isBody,boolean isCategory){
ArticleVo articleVo = new ArticleVo();
articleVo.setId(String.valueOf(article.getId()));
BeanUtils.copyProperties(article,articleVo);
articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
//并不是所有的接口 都需要標簽 ,作者信息
if (isTag){
Long articleId = article.getId();
articleVo.setTags(tagService.findTagsByArticleId(articleId));
}
if (isAuthor){
Long authorId = article.getAuthorId();
articleVo.setAuthor(sysUserService.findUserById(authorId).getNickname());
}
if (isBody){
Long bodyId = article.getBodyId();
articleVo.setBody(findArticleBodyById(bodyId));
}
if (isCategory){
Long categoryId = article.getCategoryId();
articleVo.setCategory(categoryService.findCategoryById(categoryId));
}
return articleVo;
}
@Autowired
private ArticleBodyMapper articleBodyMapper;
private ArticleBodyVo findArticleBodyById(Long bodyId) {
ArticleBody articleBody = articleBodyMapper.selectById(bodyId);
ArticleBodyVo articleBodyVo = new ArticleBodyVo();
articleBodyVo.setContent(articleBody.getContent());
return articleBodyVo;
}
}五、項目總結
本項目使用技術新,采用最流行的springboot和vue前后端分離。適合大作業(yè)中使用。
以上就是SpringBoot前后端分離實現(xiàn)個人博客系統(tǒng)的詳細內容,更多關于SpringBoot個人博客系統(tǒng)的資料請關注腳本之家其它相關文章!
相關文章
@Autowired注解注入的xxxMapper報錯問題及解決
這篇文章主要介紹了@Autowired注解注入的xxxMapper報錯問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
如何用idea編寫并運行第一個spark scala處理程序
詳細介紹了如何使用IntelliJ IDEA創(chuàng)建Scala項目,包括配置JDK和Scala SDK,添加Maven支持,編輯pom.xml,并創(chuàng)建及運行Scala程序,這為Scala初學者提供了一個基礎的項目搭建和運行指南2024-09-09
JAVA中時間戳與LocalDateTime互相轉換代碼例子
最近在編碼過程中遇到將時間戳轉化為 LocalDateTime,所以這里給總結下,這篇文章主要給大家介紹了關于JAVA中時間戳與LocalDateTime互相轉換的相關資料,需要的朋友可以參考下2023-11-11
解決maven常見錯誤:Dependency is duplicated in
這篇文章主要介紹了解決maven常見錯誤:Dependency is duplicated in file(s):問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

