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

文章分類

標(biāo)簽

登錄
發(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ù)進(jìn)行交互
@RestController
@RequestMapping("articles")
public class ArticleController {
@Autowired
private ArticleService articleService;
/**
* 首頁(yè) 文章列表
* @param pageParams
* @return
*/
@PostMapping
//加上此注解 代表要對(duì)此接口記錄日志
@LogAnnotation(module="文章",operator="獲取文章列表")
@Cache(expire = 5 * 60 * 1000,name = "listArticle")
public Result listArticle(@RequestBody PageParams pageParams){
// int i = 10/0;
return articleService.listArticle(pageParams);
}
/**
* 首頁(yè) 最熱文章
* @return
*/
@PostMapping("hot")
@Cache(expire = 5 * 60 * 1000,name = "hot_article")
public Result hotArticle(){
int limit = 5;
return articleService.hotArticle(limit);
}
/**
* 首頁(yè) 最新文章
* @return
*/
@PostMapping("new")
@Cache(expire = 5 * 60 * 1000,name = "news_article")
public Result newArticles(){
int limit = 5;
return articleService.newArticles(limit);
}
/**
* 首頁(yè) 最新文章
* @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
//
//請(qǐng)求方式: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. 分頁(yè)查詢 article數(shù)據(jù)庫(kù)表
// */
// 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){
// //加入標(biāo)簽 條件查詢
// //article表中 并沒(méi)有tag字段 一篇文章 有多個(gè)標(biāo)簽
// //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);
// }
// }
// //是否置頂進(jìn)行排序
// //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 去做關(guān)聯(lián)查詢
*/
Article article = this.articleMapper.selectById(articleId);
ArticleVo articleVo = copy(article, true, true,true,true);
//查看完文章了,新增閱讀數(shù),有沒(méi)有問(wèn)題呢?
//查看完文章之后,本應(yīng)該直接返回?cái)?shù)據(jù)了,這時(shí)候做了一個(gè)更新操作,更新時(shí)加寫鎖,阻塞其他的讀操作,性能就會(huì)比較低
// 更新 增加了此次接口的 耗時(shí) 如果一旦更新出問(wèn)題,不能影響 查看文章的操作
//線程池 可以把更新操作 扔到線程池中去執(zhí)行,和主線程就不相關(guān)了
threadService.updateArticleViewCount(articleMapper,article);
return Result.success(articleVo);
}
@Override
public Result publish(ArticleParam articleParam) {
//此接口 要加入到登錄攔截當(dāng)中
SysUser sysUser = UserThreadLocal.get();
/**
* 1. 發(fā)布文章 目的 構(gòu)建Article對(duì)象
* 2. 作者id 當(dāng)前的登錄用戶
* 3. 標(biāo)簽 要將標(biāo)簽加入到 關(guān)聯(lián)列表當(dāng)中
* 4. body 內(nèi)容存儲(chǔ) 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()));
//插入之后 會(huì)生成一個(gè)文章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"));
//并不是所有的接口 都需要標(biāo)簽 ,作者信息
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;
}
}五、項(xiàng)目總結(jié)
本項(xiàng)目使用技術(shù)新,采用最流行的springboot和vue前后端分離。適合大作業(yè)中使用。
以上就是SpringBoot前后端分離實(shí)現(xiàn)個(gè)人博客系統(tǒng)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot個(gè)人博客系統(tǒng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java 常見(jiàn)的并發(fā)問(wèn)題處理方法總結(jié)
這篇文章主要介紹了Java 常見(jiàn)的并發(fā)問(wèn)題處理方法總結(jié),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-02-02
Spring?Cloud?OpenFeign?遠(yuǎn)程調(diào)用
這篇文章主要介紹了Spring?Cloud?OpenFeign?遠(yuǎn)程調(diào)用,本文通過(guò)遠(yuǎn)程調(diào)用的GitHub開放API用到的OpenFeign作為示例代碼作為入口進(jìn)行講解。然后以圖解+解讀源碼的方式深入剖析了OpenFeign的運(yùn)行機(jī)制和架構(gòu)設(shè)計(jì),需要的朋友可以參考一下2022-08-08
MybatisPlus實(shí)現(xiàn)邏輯刪除的示例代碼
本文主要介紹了Mybatis?Plus實(shí)現(xiàn)邏輯刪除的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
讀取Java文件到byte數(shù)組的三種方法(總結(jié))
下面小編就為大家?guī)?lái)一篇讀取Java文件到byte數(shù)組的三種方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08
Java多種經(jīng)典排序算法(含動(dòng)態(tài)圖)
排序算法是老生常談的了,但是在面試中也有會(huì)被問(wèn)到,例如有時(shí)候,在考察算法能力的時(shí)候,不讓你寫算法,就讓你描述一下,某個(gè)排序算法的思想以及時(shí)間復(fù)雜度或空間復(fù)雜度。我就遇到過(guò),直接問(wèn)快排的,所以這次我就總結(jié)梳理一下經(jīng)典的十大排序算法以及它們的模板代碼2021-04-04
詳解SpringCloud LoadBalancer 新一代負(fù)載均衡器
這篇文章主要為大家介紹了SpringCloud LoadBalancer新一代負(fù)載均衡器詳解使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
spring boot aop 記錄方法執(zhí)行時(shí)間代碼示例
這篇文章主要介紹了spring boot aop 記錄方法執(zhí)行時(shí)間代碼示例,分享了相關(guān)代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
基于springboot+vue實(shí)現(xiàn)垃圾分類管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于springboot+vue實(shí)現(xiàn)垃圾分類管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07

