写在最前面的完结感言
目前整个流程已经走完,项目也已经成功部署上线。
项目地址:
raxcl.cn(已被重构)
-----欢迎访问
(后续域名备案成功后会替换成域名)
后端代码仓库:github.com/Raxcl/blog-parent
前端代码仓库:github.com/Raxcl/blog-app
恭喜自己成功走出新手村,但后面还有茫茫星辰大海要走,比如如何把博客管理系统部署上线,如何把其他项目也部署上去,如何优化等等等等…期待我们后续的相遇。
2022.03.15
目前博客已经重构,重构后代码仓库: RBlog
导航部分
由于篇幅过过过过过长,所以分成几篇来写
基于springboot + vue 的个人博客搭建过程(续)
基于springboot + vue 的个人博客搭建过程(上线)
基于springboot + vue 的个人博客搭建过程(新手村之外的世界依旧充满魅力)
目录
前言
码神之路网站所使用的博客,项目简单,需求明确,容易上手,非常适合作为练手几项目。
成品预览: blog.mszlu.com
项目搭建思路:
1. 提供前端工程,只需要实现后端接口即可
2. 项目以单体架构入手,先快速开发,不考虑项目优化,降低开发负担
3. 开发完成后,开始优化项目,提升编程思维能力(页面静态化,缓存,云存储,日志等等…)
4. docker部署上线
5. 云服务器购买,域名购买,域名备案等…
项目使用技术:springboot + mybatisplus + redis + mysql
工程搭建
1. 前端工程
用vscode打开,然后编译启动,具体命令如下:
npm install
npm run serve
BUT,我们在这里不用这种方式,而是采用另外一种方式去启动前端。
下面给出具体命令:
npm install yarn
yarn start
看在这里,我们可能会有一个大写的问号:npm和yarn有什么区别呢?
总的来说就是,yarn更快,更简洁(真是忘金油式的回答啊!)
总结一下为什么yarn比npm更好吧:
- 速度快: 采用并行安装和离线安装(如果之前安装过yarn,安装时就可以从缓存中离线安装)
- 安装版本统一: (确保每次拉取项目使用的依赖版本都是相同的)
- 更简洁的输出
- 多注册来源处理
- 更好的语义化(命令更简洁)
2. 新建maven工程(父框架)
2.1 新建blog-parent父框架, 删掉src,pom.xml引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.raxcl</groupId>
<artifactId>blog-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>blog-api</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--打包需要-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 maven依赖飘红问题
过程中可能会有一些依赖飘红,推测是由于版本号和父项目不一致导致的,先不用管(有强迫症的孩子可以尝试解决) 。 是由于maven版本过低导致的,maven官网推荐大家更新至3.8.3版本。飘红问题即可解决(如果还飘红,可以先输入版本号,刷新pom.xml文件后再删除版本号即可)
3. 新建子模块blog-api
3.1 pom.xml添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<!-- 排除 默认使用的logback -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
</dependencies>
3.2 配置信息
application.properties
#server
server.port=8888
spring.application.name=raxcl
# datasource
spring.datasource.url=jdbc:mysql://106.54.170.191:3306/blog?useUnicode=true&characterEncoding=UTF-8&serverTimeZone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.global-config.db-config.table-prefix=ms_
MybatisPlusConfig
package com.raxcl.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Configurable;
@Configuration
@MapperScan("com.raxcl.blog.mapper")
public class MybatisPlusConfig {
//分页插件
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
WebMVCConfig
package com.raxcl.config;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry){
//跨域配置
registry.addMapping("/**").allowedOrigins("http://localhost:8080");
}
}
3.3 启动类
package com.raxcl.blog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BlogApp {
public static void main(String[] args) {
SpringApplication.run(BlogApp.class,args);
}
}
功能开发
1. 首页-文章列表
1.1 接口说明
接口url:/articles
请求方式:POST
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
page | int | 当前页数 |
pageSize | int | 每页显示的数量 |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id": 1,
"title": "springboot介绍以及入门案例",
"summary": "通过Spring Boot实现的服务,只需要依靠一个Java类,把它打包成jar,并通过`java -jar`命令就可以运行起来。\r\n\r\n这一切相较于传统Spring应用来说,已经变得非常的轻便、简单。",
"commentCounts": 2,
"viewCounts": 54,
"weight": 1,
"createDate": "2609-06-26 15:58",
"author": "12",
"body": null,
"tags": [
{
"id": 5,
"avatar": null,
"tagName": "444"
},
{
"id": 7,
"avatar": null,
"tagName": "22"
},
{
"id": 8,
"avatar": null,
"tagName": "11"
}
],
"categorys": null
},
{
"id": 9,
"title": "Vue.js 是什么",
"summary": "Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架。",
"commentCounts": 0,
"viewCounts": 3,
"weight": 0,
"createDate": "2609-06-27 11:25",
"author": "12",
"body": null,
"tags": [
{
"id": 7,
"avatar": null,
"tagName": "22"
}
],
"categorys": null
},
{
"id": 10,
"title": "Element相关",
"summary": "本节将介绍如何在项目中使用 Element。",
"commentCounts": 0,
"viewCounts": 3,
"weight": 0,
"createDate": "2609-06-27 11:25",
"author": "12",
"body": null,
"tags": [
{
"id": 5,
"avatar": null,
"tagName": "444"
},
{
"id": 6,
"avatar": null,
"tagName": "33"
},
{
"id": 7,
"avatar": null,
"tagName": "22"
},
{
"id": 8,
"avatar": null,
"tagName": "11"
}
],
"categorys": null
}
]
}
1.2 编码
1.2.1 数据库表
CREATE TABLE `blog`.`ms_article` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`comment_counts` int(0) NULL DEFAULT NULL COMMENT '评论数量',
`create_date` bigint(0) NULL DEFAULT NULL COMMENT '创建时间',
`summary` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '简介',
`title` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '标题',
`view_counts` int(0) NULL DEFAULT NULL COMMENT '浏览数量',
`weight` int(0) NOT NULL COMMENT '是否置顶',
`author_id` bigint(0) NULL DEFAULT NULL COMMENT '作者id',
`body_id` bigint(0) NULL DEFAULT NULL COMMENT '内容id',
`category_id` int(0) NULL DEFAULT NULL COMMENT '类别id',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 25 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `blog`.`ms_tag` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`article_id` bigint(0) NOT NULL,
`tag_id` bigint(0) NOT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `article_id`(`article_id`) USING BTREE,
INDEX `tag_id`(`tag_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `blog`.`ms_sys_user` (
`id` bigint(0) NOT NULL AUTO_INCREMENT,
`account` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '账号',
`admin` bit(1) NULL DEFAULT NULL COMMENT '是否管理员',
`avatar` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '头像',
`create_date` bigint(0) NULL DEFAULT NULL COMMENT '注册时间',
`deleted` bit(1) NULL DEFAULT NULL COMMENT '是否删除',
`email` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '邮箱',
`last_login` bigint(0) NULL DEFAULT NULL COMMENT '最后登录时间',
`mobile_phone_number` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手机号',
`nickname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '昵称',
`password` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '密码',
`salt` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '加密盐',
`status` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '状态',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
1.2.2 dao层
1.2.2.1 pojo实体类
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class Article {
public static final int Article_TOP =1;
public static final int Article_Common = 0;
private Long id;
/**
*评论数量
*/
private int commentCounts;
/**
* 创建时间
*/
private Long createDate;
/**
* 简介
*/
private String summary;
/**
* 标题
*/
private String title;
/**
* 浏览数量
*/
private int viewCounts;
/**
* 是否置顶
*/
private int weight;
/**
* 作者id
*/
private Long authorId;
/**
* 内容id
*/
private Long bodyId;
/**
* 类别id
*/
private int category_id;
}
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class SysUser {
private Long id;
/**
* 账号
*/
private String account;
/**
* 是否管理员
*/
private Integer admin;
/**
* 头像
*/
private String avatar;
/**
* 注册时间
*/
private Long createDate;
/**
* 是否删除
*/
private Integer deleted;
/**
* 邮箱
*/
private String email;
/**
* 最后登录时间
*/
private Long lastLogin;
/**
* 手机号
*/
private String mobilePhoneNumber;
/**
* 昵称
*/
private String nickname;
/**
* 密码
*/
private String password;
/**
* 加密盐
*/
private String salt;
/**
* 状态
*/
private String status;
}
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class Tag {
private Long id;
private String avatar;
private String tagName;
}
1.2.2.2 mapper接口
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.pojo.Article;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
}
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.pojo.SysUser;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysUserMapper extends BaseMapper<SysUser> {
}
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.pojo.Tag;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface TagMapper extends BaseMapper<Tag> {
/**
* 根据文章标题查询标签列表
* @param articleId
* @return
*/
List<Tag> findTagByArticleId(Long articleId);
}
1.2.3 vo层
package com.raxcl.blog.vo.param;
import lombok.Data;
@Data
public class PageParams {
private int page = 1;
private int pageSize = 10;
}
package com.raxcl.blog.vo;
import lombok.Data;
import java.util.List;
@Data
public class ArticleVo {
private Long id;
/**
* 标题
*/
private String title;
/**
* 简介
*/
private String summary;
/**
*评论数量
*/
private int commentCounts;
/**
* 浏览数量
*/
private int viewCounts;
/**
* 是否置顶
*/
private int weight;
/**
* 创建时间
*/
private String createDate;
/**
* 作者
*/
private String author;
//private ArticleBodyVo body;
private List<TagVo> tags;
//private List<CategoryVo> categorys;
/**
* 作者id
*/
private Long authorId;
/**
* 内容id
*/
private Long bodyId;
/**
* 类别id
*/
private int category_id;
}
package com.raxcl.blog.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private boolean success;
private Integer code;
private String msg;
private Object data;
public static Result success(Object data) {
return new Result(true, 200, "success", data);
}
public static Result fail(Integer code, String msg){
return new Result(false,code, msg, null);
}
}
package com.raxcl.blog.vo;
import lombok.Data;
@Data
public class TagVo {
private Long id;
private String tagName;
}
1.2.4 controller层
package com.raxcl.blog.api;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("articles")
public class ArticleController {
private final ArticleService articleService;
public ArticleController(ArticleService articleService) {
this.articleService = articleService;
}
//Result是统一结果返回(VO类)
@PostMapping
public Result articles(@RequestBody PageParams pageParams){
return articleService.listArticle(pageParams);
}
}
1.2.5 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.springframework.stereotype.Service;
public interface ArticleService {
/**
* 分页查询,文章列表
* @param pageParams
* @return
*/
Result listArticle(PageParams pageParams);
}
package com.raxcl.blog.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.raxcl.blog.dao.mapper.ArticleMapper;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
private final ArticleMapper articleMapper;
private final TagService tagService;
private final SysUserService sysUserService;
public ArticleServiceImpl(ArticleMapper articleMapper, TagService tagService, SysUserService sysUserService) {
this.articleMapper = articleMapper;
this.tagService = tagService;
this.sysUserService = sysUserService;
}
@Override
public Result listArticle(PageParams pageParams) {
/**
* 1. 分页查询 article数据库表
*/
Page<Article> page = new Page<>(pageParams.getPage(),pageParams.getPageSize());
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
//是否置顶进行排序
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);
}
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));
}
return articleVoList;
}
private ArticleVo copy(Article article, boolean isTag, boolean isAuthor) {
ArticleVo articleVo = new ArticleVo();
BeanUtils.copyProperties(article, articleVo);
articleVo.setCreateDate(new DateTime(article.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
if (isTag){
articleVo.setTags(tagService.findTagByArticleId(article.getId()));
}
if (isAuthor){
articleVo.setAuthor(sysUserService.findUserById(article.getId()).getNickname());
}
return articleVo;
}
}
package com.raxcl.blog.service;
import com.raxcl.blog.dao.pojo.SysUser;
public interface SysUserService {
SysUser findUserById(Long id);
}
package com.raxcl.blog.service.impl;
import com.raxcl.blog.dao.mapper.SysUserMapper;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.SysUserService;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class SysUserServiceImpl implements SysUserService {
private final SysUserMapper sysUserMapper;
public SysUserServiceImpl(SysUserMapper sysUserMapper) {
this.sysUserMapper = sysUserMapper;
}
@Override
public SysUser findUserById(Long id) {
SysUser sysUser = sysUserMapper.selectById(id);
if (Objects.isNull(sysUser)){
sysUser = new SysUser();
sysUser.setNickname("raxcl学博客");
}
return sysUser;
}
}
package com.raxcl.blog.service;
import com.raxcl.blog.vo.TagVo;
import java.util.List;
public interface TagService {
List<TagVo> findTagByArticleId(Long articleId);
}
package com.raxcl.blog.service.impl;
import com.raxcl.blog.dao.mapper.TagMapper;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.dao.pojo.Tag;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.TagVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class TagServiceImpl implements TagService {
private final TagMapper tagMapper;
public TagServiceImpl(TagMapper tagMapper) {
this.tagMapper = tagMapper;
}
@Override
public List<TagVo> findTagByArticleId(Long articleId) {
//mybatisplus 无法进行多表查询?
List<Tag> tags = tagMapper.findTagByArticleId(articleId);
return copyList(tags);
}
private List<TagVo> copyList(List<Tag> records) {
List<TagVo> tagVoList = new ArrayList<>();
for (Tag record : records){
tagVoList.add(copy(record));
}
return tagVoList;
}
private TagVo copy(Tag tag) {
TagVo tagVo = new TagVo();
BeanUtils.copyProperties(tag, tagVo);
return tagVo;
}
}
1.2.6 mapper实现层
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis配置文件-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.raxcl.blog.dao.mapper.TagMapper">
<select id="findTagByArticleId" resultType="com.raxcl.blog.dao.pojo.Tag">
select id,avatar,tag_name
from ms_tag
where id in (
select tag_id from ms_article_tag where article_id=#{articleId}
)
</select>
</mapper>
2. 首页-最热标签
2.1 接口说明
接口url:/tags/hot
请求方式:GET
请求参数:无
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": [
{
"id":1,
"tagName":"4444"
}
]
}
2.2 编码
2.2.1 controller层
package com.raxcl.blog.api;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.TagVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("tags")
public class TagsController {
private final TagService tagService;
public TagsController(TagService tagService) {
this.tagService = tagService;
}
@GetMapping("/hot")
public Result listHotTags() {
int limit = 6;
List<TagVo> tagVoList = tagService.hot(limit);
return Result.success(tagVoList);
}
}
2.2.2 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.TagVo;
import java.util.List;
public interface TagService {
List<TagVo> hot(int limit);
}
package com.raxcl.blog.service.impl;
import com.raxcl.blog.dao.mapper.TagMapper;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.dao.pojo.Tag;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.TagVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class TagServiceImpl implements TagService {
@Override
public List<TagVo> hot(int limit) {
List<Long> hotsTagIds = tagMapper.findHotsTagIds(limit);
if (CollectionUtils.isEmpty(hotsTagIds)){
return Collections.emptyList();
}
List<Tag> tagList = tagMapper.findTagsByTagIds(hotsTagIds);
return copyList(tagList);
}
}
2.2.3 mapper层
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.pojo.Tag;
import com.raxcl.blog.vo.TagVo;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface TagMapper extends BaseMapper<Tag> {
/**
* 查询最热的标签,前n条
* @param limit
* @return
*/
List<Long> findHotsTagIds(int limit);
List<Tag> findTagsByTagIds(List<Long> tagIds);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis配置文件-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.raxcl.blog.dao.mapper.TagMapper">
<sql id ="all">
id,avatar,tag_name
</sql>
<select id="findHotsTagIds" resultType="java.lang.Long">
select tag_id
from ms_article_tag
group by tag_id
order by count(1) desc limit #{size}
</select>
<select id="findTagsByTagIds" resultType="com.raxcl.blog.dao.pojo.Tag">
select <include refid="all"></include>
from ms_tag
where id in
<foreach collection="tagIds" item="tagId" separator="," open="(" close=")">
#{tagId}
</foreach>
</select>
</mapper>
3. 统一处理异常
package com.raxcl.blog.handler;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
//对加了@Controller注解的方法进行拦截处理 AOP实现
@ControllerAdvice
public class AllExceptionHandler {
//进行异常处理,处理Exception.class的异常
@ExceptionHandler(Exception.class)
@ResponseBody
public Result doException(Exception e){
e.printStackTrace();
return Result.fail(-999,"系统异常");
}
}
4. 首页-最热文章&最新文章
4.1 接口说明
最热文章:
接口url:/articles/hot
请求方式:POST
请求参数:
最新文章:
接口url:/articles/new
请求方式:POST
请求参数:
4.2 controller层
package com.raxcl.blog.api;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("articles")
public class ArticleController {
/**
* 最热文章
* @return
*/
@PostMapping("hot")
public Result hotArticle(){
int limit = 5;
return articleService.hotArticle(limit);
}
/**
* 最新文章
* @return
*/
@PostMapping("new")
public Result newArticle(){
int limit = 5;
return articleService.newArticle(limit);
}
}
4.3 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
public interface ArticleService {
Result hotArticle(int limit);
Result newArticle(int limit);
}
package com.raxcl.blog.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.raxcl.blog.dao.mapper.ArticleMapper;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
@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);
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}
@Override
public Result newArticle(int limit) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(Article::getCreateDate);
queryWrapper.select(Article::getId,Article::getTitle);
queryWrapper.last("limit "+ limit);
List<Article> articles = articleMapper.selectList(queryWrapper);
return Result.success(copyList(articles,false,false));
}
}
5. 首页-文章归档
5.1 接口说明
接口url:/articles/listArchives
请求方式:POST
5.2 dto层
package com.raxcl.blog.dao.dto;
import lombok.Data;
@Data
public class Archives {
private Integer year;
private Integer month;
private Integer count;
}
5.3 controller层
package com.raxcl.blog.api;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("articles")
public class ArticleController {
/**
* 文章归档
* @return
*/
@PostMapping("listArchives")
public Result listArchives(){
return articleService.listArchives();
}
}
5.3 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
public interface ArticleService {
Result listArchives();
}
package com.raxcl.blog.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.raxcl.blog.dao.mapper.ArticleMapper;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
/**
* 文章归档
* @return
*/
@PostMapping("listArchives")
public Result listArchives(){
return articleService.listArchives();
}
}
5.4 mapper层
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.dto.Archives;
import com.raxcl.blog.dao.pojo.Article;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {
List<Archives> listArchives();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis配置文件-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.raxcl.blog.dao.mapper.ArticleMapper">
<select id="listArchives" resultType="com.raxcl.blog.dao.dto.Archives">
select year(create_date) year, month(create_date) month, count(*) count
from ms_article
group by year, month
</select>
</mapper>
6. 登录功能
6.1 接口说明
接口url:/login
请求方式:POST
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
account | string | 账号 |
password | string | 密码 |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": "token"
}
6.2 pom.xml
<!--密码加密-->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
6.3 vo层
package com.raxcl.blog.vo.param;
import lombok.Data;
@Data
public class LoginParam {
private String account;
private String password;
}
package com.raxcl.blog.vo;
public enum ErrorCode {
PARAMS_ERROR(10001,"参数有误"),
ACCOUNT_PWD_NOT_EXIST (10002,"用户名或密码不存在"),;
private int code;
private String msg;
ErrorCode(int code, String msg) {
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
6.4 utils层
package com.raxcl.blog.utils;
import io.jsonwebtoken.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class JWTUtils {
private static final String jwtToken = "123456Mszlu!@#$$";
public static String createToken(Long userId){
Map<String, Object> claims = new HashMap<>();
claims.put("userId",userId);
JwtBuilder jwtBuilder = Jwts.builder()
.signWith(SignatureAlgorithm.HS256, jwtToken) //签发算法,秘钥为jwtToken
.setClaims(claims) //body数据,要唯一,自行设置
.setIssuedAt(new Date()) // 设置签发时间
.setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 60 * 1000));//一天的有效时间
String token = jwtBuilder.compact();
return token;
}
public static Map<String, Object> checkToken(String token){
try {
Jwt parse = Jwts.parser().setSigningKey(jwtToken).parse(token);
return (Map<String, Object>) parse.getBody();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
6.5 controller层
package com.raxcl.blog.controller;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("login")
public class LoginController {
private final LoginService loginService;
public LoginController(LoginService loginService) {
this.loginService = loginService;
}
@PostMapping
public Result login(@RequestBody LoginParam loginParam){
return loginService.login(loginParam);
}
}
6.6 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
public interface LoginService {
/**
* 登录
* @param loginParam
* @return
*/
Result login(LoginParam loginParam);
}
package com.raxcl.blog.service.impl;
import com.alibaba.fastjson.JSON;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.utils.JWTUtils;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Service
public class LoginServiceImpl implements LoginService {
private static final String slat = "raxcl!@#";
private final SysUserService sysUserService;
private final RedisTemplate<String, String> redisTemplate;
public LoginServiceImpl(SysUserService sysUserService, RedisTemplate<String, String> redisTemplate) {
this.sysUserService = sysUserService;
this.redisTemplate = redisTemplate;
}
@Override
public Result login(LoginParam loginParam) {
String account = loginParam.getAccount();
String password = loginParam.getPassword();
if (StringUtils.isBlank(account) || StringUtils.isBlank(password)){
return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),ErrorCode.PARAMS_ERROR.getMsg());
}
String pwd = DigestUtils.md5Hex(password + slat);
SysUser sysUser = sysUserService.findUser(account,pwd);
if (Objects.isNull(sysUser)){
return Result.fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(),ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
}
//登录成功,使用JWT生成token,返回token和redis中
String token = JWTUtils.createToken(sysUser.getId());
redisTemplate.opsForValue().set("TOKEN_"+token, JSON.toJSONString(sysUser),1, TimeUnit.DAYS);
return Result.success(token);
}
}
package com.raxcl.blog.service;
import com.raxcl.blog.dao.pojo.SysUser;
public interface SysUserService {
SysUser findUser(String account, String pwd);
}
package com.raxcl.blog.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.raxcl.blog.dao.mapper.SysUserMapper;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.SysUserService;
import org.springframework.stereotype.Service;
import java.util.Objects;
@Service
public class SysUserServiceImpl implements SysUserService {
@Override
public SysUser findUser(String account, String pwd) {
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUser::getAccount,account);
queryWrapper.eq(SysUser::getPassword,pwd);
queryWrapper.select(SysUser::getId,SysUser::getAccount,SysUser::getAvatar,SysUser::getNickname);
queryWrapper.last("limit 1");
SysUser sysUser = sysUserMapper.selectOne(queryWrapper);
return sysUser;
}
}
7. 获取用户信息
7.1 接口说明
接口url:/users/currentUser
请求方式:GET
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
Authorization | string | 头部信息(TOKEN) |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": {
"id":1,
"account":"1",
"nickaname":"1",
"avatar":"ss"
}
}
7.2 controller层
package com.raxcl.blog.controller;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("users")
public class UserController {
private final SysUserService sysUserService;
public UserController(SysUserService sysUserService) {
this.sysUserService = sysUserService;
}
@GetMapping("currentUser")
public Result currentUser (@RequestHeader("Authorization") String token){
return sysUserService.getUserInfoByToken(token);
}
}
7.3 vo层
package com.raxcl.blog.vo;
import lombok.Data;
@Data
public class LoginUserVo {
private Long id;
private String account;
private String nickname;
private String avatar;
}
7.4 service层
package com.raxcl.blog.service;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.vo.Result;
public interface SysUserService {
Result getUserInfoByToken(String token);
}
package com.raxcl.blog.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.raxcl.blog.dao.mapper.SysUserMapper;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.utils.JWTUtils;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.LoginUserVo;
import com.raxcl.blog.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Objects;
@Service
public class SysUserServiceImpl implements SysUserService {
private final SysUserMapper sysUserMapper;
private final RedisTemplate<String, String> redisTemplate;
public SysUserServiceImpl(SysUserMapper sysUserMapper, RedisTemplate<String, String> redisTemplate) {
this.sysUserMapper = sysUserMapper;
this.redisTemplate = redisTemplate;
}
@Override
public Result getUserInfoByToken(String token) {
Map<String, Object> map = JWTUtils.checkToken((token));
if (Objects.isNull(map)){
return Result.fail(ErrorCode.NO_LOGIN.getCode(),ErrorCode.NO_LOGIN.getMsg());
}
String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
if (StringUtils.isBlank(userJson)){
return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
}
SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
LoginUserVo loginUserVo = new LoginUserVo();
loginUserVo.setAccount(sysUser.getAccount());
loginUserVo.setAvatar(sysUser.getAvatar());
loginUserVo.setId(sysUser.getId());
loginUserVo.setNickname(sysUser.getNickname());
return Result.success(loginUserVo);
}
}
8. 登出
接口url:/logout
请求方式:GET
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
Authorization | string | 头部信息(TOKEN) |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": null
}
8.1 controller层
package com.raxcl.blog.controller;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("logout")
public class LogoutController {
private final LoginService loginService;
public LogoutController(LoginService loginService) {
this.loginService = loginService;
}
@GetMapping
public Result logout(@RequestHeader("Authorization") String token){
return loginService.logout(token);
}
}
8.2 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
public interface LoginService {
Result logout(String token);
}
package com.raxcl.blog.service.impl;
import com.alibaba.fastjson.JSON;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.utils.JWTUtils;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Service
public class LoginServiceImpl implements LoginService {
@Override
public Result logout(String token) {
redisTemplate.delete("TOLEN_"+token);
return Result.success(null);
}
}
9. 注册
9.1 接口说明
接口url:/register
请求方式:POST
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
account | string | 账号 |
password | string | 密码 |
nickname | string | 昵称 |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": "token"
}
9.2 vo层
package com.raxcl.blog.vo.param;
import lombok.Data;
@Data
public class LoginParam {
private String account;
private String password;
private String nickname;
}
package com.raxcl.blog.vo;
public enum ErrorCode {
PARAMS_ERROR(10001,"参数有误"),
ACCOUNT_PWD_NOT_EXIST (10002,"用户名或密码不存在"),
NO_PERMISSION(70001,"无访问权限"),
SESSION_TIME_OUT(90001,"会话超时"),
NO_LOGIN(90002,"未登录"),
ACCOUNT_EXIST(10004,"账号已存在");
}
9.3 controller层
package com.raxcl.blog.controller;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("register")
public class RegisterController {
private final LoginService loginService;
public RegisterController(LoginService loginService) {
this.loginService = loginService;
}
@PostMapping
public Result register(@RequestBody LoginParam loginParam){
return loginService.register(loginParam);
}
}
9.4 service层
package com.raxcl.blog.service;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
public interface LoginService {
Result register(LoginParam loginParam);
}
package com.raxcl.blog.service.impl;
import com.alibaba.fastjson.JSON;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.utils.JWTUtils;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
private static final String slat = "raxcl!@#";
private final SysUserService sysUserService;
private final RedisTemplate<String, String> redisTemplate;
public LoginServiceImpl(SysUserService sysUserService, RedisTemplate<String, String> redisTemplate) {
this.sysUserService = sysUserService;
this.redisTemplate = redisTemplate;
}
@Override
public Result register(LoginParam loginParam) {
String account = loginParam.getAccount();
String password = loginParam.getPassword();
String nickname = loginParam.getNickname();
if (StringUtils.isBlank(account) || StringUtils.isBlank(password) || StringUtils.isBlank(nickname)){
return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
}
SysUser sysUser = this.sysUserService.findUserByAccount(account);
if (sysUser != null){
return Result.fail(ErrorCode.ACCOUNT_EXIST.getCode(),ErrorCode.ACCOUNT_EXIST.getMsg());
}
sysUser = new SysUser();
sysUser.setNickname(nickname);
sysUser.setAccount(account);
sysUser.setPassword(DigestUtils.md5Hex(password+slat));
sysUser.setCreateDate(System.currentTimeMillis());
sysUser.setLastLogin(System.currentTimeMillis());
sysUser.setAvatar("/static/img/logo.b3a48c0.png");
sysUser.setAdmin(1); // 1为true
sysUser.setDeleted(0); //0为false
sysUser.setSalt("");
sysUser.setStatus("");
sysUser.setEmail("");
this.sysUserService.save(sysUser);
//token
String token = JWTUtils.createToken(sysUser.getId());
redisTemplate.opsForValue().set("TOKEN_"+token,JSON.toJSONString(sysUser),1,TimeUnit.DAYS);
return Result.success(token);
}
}
package com.raxcl.blog.service;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.vo.Result;
public interface SysUserService {
SysUser findUserByAccount(String account);
void save(SysUser sysUser);
}
package com.raxcl.blog.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.raxcl.blog.dao.mapper.SysUserMapper;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.utils.JWTUtils;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.LoginUserVo;
import com.raxcl.blog.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Objects;
@Service
public class SysUserServiceImpl implements SysUserService {
private final SysUserMapper sysUserMapper;
private final RedisTemplate<String, String> redisTemplate;
public SysUserServiceImpl(SysUserMapper sysUserMapper, RedisTemplate<String, String> redisTemplate) {
this.sysUserMapper = sysUserMapper;
this.redisTemplate = redisTemplate;
}
@Override
public SysUser findUserByAccount(String account) {
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUser::getAccount,account);
queryWrapper.last("limit 1");
return sysUserMapper.selectOne(queryWrapper);
}
@Override
public void save(SysUser sysUser) {
//注意 默认生成的id是分布式id 采用了雪花算法
this.sysUserMapper.insert(sysUser);
}
}
10. 登录拦截器
每次访问需要登录的资源的时候,都需要在代码中进行判断,一旦登录的逻辑有所改变,代码都得进行变动,非常不合适。
那么可不可以统一进行登录判断呢?
可以,使用拦截器,进行登录拦截,如果遇到需要登录才能访问的接口,如果未登录,拦截器直接返回,并跳转登录页面。
10.1 hander层
package com.raxcl.blog.handler;
import com.alibaba.fastjson.JSON;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
private final LoginService loginService;
public LoginInterceptor(LoginService loginService) {
this.loginService = loginService;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(!(handler instanceof HandlerMethod)){
return true;
}
String token = request.getHeader("Authorization");
log.info("==============request start=================");
String requestURI = request.getRequestURI();
log.info("request uri:{}",requestURI);
log.info("request method:{}",request.getMethod());
log.info("token:{}",token);
log.info("==============request end===============");
if (token == null){
Result result = Result.fail(ErrorCode.NO_LOGIN.getCode(), "未登录");
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(JSON.toJSONString(result));
return false;
}
SysUser sysUser = loginService.checkToken(token);
if (sysUser == null){
Result result = Result.fail(ErrorCode.NO_LOGIN.getCode(), "未登录");
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(JSON.toJSONString(result));
return false;
}
//是登录状态,放行
return true;
}
}
10.2 service层
package com.raxcl.blog.service;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
public interface LoginService {
SysUser checkToken(String token);
}
package com.raxcl.blog.service.impl;
import com.alibaba.fastjson.JSON;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.utils.JWTUtils;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.Result;
import com.raxcl.blog.vo.param.LoginParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
@Override
public SysUser checkToken(String token) {
if (StringUtils.isBlank(token)){
return null;
}
Map<String,Object> stringObjectMap = JWTUtils.checkToken(token);
if (stringObjectMap == null){
return null;
}
String userJson = redisTemplate.opsForValue().get("TOKEN_"+ token);
if (StringUtils.isBlank(userJson)){
return null;
}
SysUser sysUser = JSON.parseObject(userJson, SysUser.class);
return sysUser;
}
}
10.3 config层(使拦截器生效)
package com.raxcl.blog.config;
import com.raxcl.blog.handler.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
private final LoginInterceptor loginInterceptor;
public WebMVCConfig(LoginInterceptor loginInterceptor) {
this.loginInterceptor = loginInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry){
//拦截test接口,后续实际遇到需要拦截的接口时,在配置为真正的拦截接口
registry.addInterceptor(loginInterceptor).addPathPatterns("/test");
}
}
10.4 测试层(controller)
package com.raxcl.blog.controller;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestController {
@RequestMapping
public Result test(){
return Result.success(null);
}
}
11. ThreadLocal保存用户信息
11.1 utils层
package com.raxcl.blog.utils;
import com.raxcl.blog.dao.pojo.SysUser;
public class UserThreadLocal {
private UserThreadLocal(){}
private static final ThreadLocal<SysUser> LOCAL = new ThreadLocal<>();
public static void put(SysUser sysUser){
LOCAL.set(sysUser);
}
public static SysUser get(){
return LOCAL.get();
}
public static void remove(){
LOCAL.remove();
}
}
11.2 handler层
package com.raxcl.blog.handler;
import com.alibaba.fastjson.JSON;
import com.raxcl.blog.dao.pojo.SysUser;
import com.raxcl.blog.service.LoginService;
import com.raxcl.blog.utils.UserThreadLocal;
import com.raxcl.blog.vo.ErrorCode;
import com.raxcl.blog.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
private final LoginService loginService;
public LoginInterceptor(LoginService loginService) {
this.loginService = loginService;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//在执行controller方法之前进行执行
//在执行controller方法(Handler)之前进行执行
/**
* 1. 需要判断 请求的接口路径 是否为 HandlerMethod (controller方法)
* 2. 判断 token是否为空,如果为空 未登录
* 3. 如果token 不为空,登录验证 loginService checkToken
* 4. 如果认证成功 放行即可
*/
if(!(handler instanceof HandlerMethod)){
return true;
}
String token = request.getHeader("Authorization");
log.info("==============request start=================");
String requestURI = request.getRequestURI();
log.info("request uri:{}",requestURI);
log.info("request method:{}",request.getMethod());
log.info("token:{}",token);
log.info("==============request end===============");
if (token == null){
Result result = Result.fail(ErrorCode.NO_LOGIN.getCode(), "未登录");
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(JSON.toJSONString(result));
return false;
}
SysUser sysUser = loginService.checkToken(token);
if (sysUser == null){
Result result = Result.fail(ErrorCode.NO_LOGIN.getCode(), "未登录");
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(JSON.toJSONString(result));
return false;
}
//是登录状态,放行
UserThreadLocal.put(sysUser);
return true;
}
/**
* 不移出可能会导致内存泄漏
* @param request
* @param response
* @param handler
* @param ex
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex){
UserThreadLocal.remove();
}
}
11.3 controller层
package com.raxcl.blog.controller;
import com.raxcl.blog.utils.UserThreadLocal;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestController {
@RequestMapping
public Result test(){
System.out.println("哈哈哈哈哈哈");
System.out.println(UserThreadLocal.get());
return Result.success(null);
}
}
12. ThreadLocal内存泄漏
实线代表强引用,虚线代表弱引用
每一个Thread维护一个ThreadLocalMap, key为使用弱引用的ThreadLocal实例,value为线程变量的副本。
强引用,使用最普遍的引用,一个对象具有强引用,不会被垃圾回收器回收。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不回收这种对象。
如果想取消强引用和某个对象之间的关联,可以显式地将引用赋值为null,这样可以使JVM在合适的时间就会回收该对象。
弱引用,JVM进行垃圾回收时,无论内存是否充足,都会回收被弱引用关联的对象。在java中,用java.lang.ref.WeakReference类来表示。
13. 文章详情
13.1 接口说明
接口url:/articles/view/{id}
请求方式:POST
请求参数:
参数名称 | 参数类型 | 说明 |
---|---|---|
id | long | 文章id(路径参数) |
返回数据:
{
"success": true,
"code": 200,
"msg": "success",
"data": "token"
}
13.2 dao层
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class Article {
public static final int Article_TOP =1;
public static final int Article_Common = 0;
private Long id;
/**
*评论数量
*/
private int commentCounts;
/**
* 创建时间
*/
private Long createDate;
/**
* 简介
*/
private String summary;
/**
* 标题
*/
private String title;
/**
* 浏览数量
*/
private int viewCounts;
/**
* 是否置顶
*/
private int weight;
/**
* 作者id
*/
private Long authorId;
/**
* 内容id
*/
private Long bodyId;
/**
* 类别id
*/
private Long categoryId;
}
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class ArticleBody {
private Long id;
private String content;
private String contentHtml;
private Long articleId;
}
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class Category {
private Long id;
private String avatar;
private String categoryName;
private String description;
}
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.pojo.ArticleBody;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ArticleBodyMapper extends BaseMapper<ArticleBody> {
}
package com.raxcl.blog.dao.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.raxcl.blog.dao.pojo.Category;
public interface CategoryMapper extends BaseMapper<Category> {
}
13.3 vo层
package com.raxcl.blog.vo;
import lombok.Data;
@Data
public class ArticleBodyVo {
/**
* 文章内容
*/
private String content;
}
package com.raxcl.blog.vo;
import lombok.Data;
import java.util.List;
@Data
public class ArticleVo {
private Long id;
/**
* 标题
*/
private String title;
/**
* 简介
*/
private String summary;
/**
*评论数量
*/
private int commentCounts;
/**
* 浏览数量
*/
private int viewCounts;
/**
* 是否置顶
*/
private int weight;
/**
* 创建时间
*/
private String createDate;
/**
* 作者
*/
private String author;
private ArticleBodyVo body;
private List<TagVo> tags;
private CategoryVo category;
/**
* 作者id
*/
private Long authorId;
/**
* 内容id
*/
private Long bodyId;
/**
* 类别id
*/
private int categoryId;
}
package com.raxcl.blog.vo;
import lombok.Data;
@Data
public class CategoryVo {
private Long id;
private String avatar;
private String categoryName;
}
13.4 具体实现(层级关系说明省略)
package com.raxcl.blog.controller;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.springframework.web.bind.annotation.*;
import java.sql.ResultSet;
@RestController
@RequestMapping("articles")
public class ArticleController {
private final ArticleService articleService;
public ArticleController(ArticleService articleService) {
this.articleService = articleService;
}
@PostMapping("view/{id}")
public Result findArticleById(@PathVariable("id") Long id){
ArticleVo articleVo = articleService.findArticleById(id);
return Result.success(articleVo);
}
}
package com.raxcl.blog.service;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
public interface ArticleService {
ArticleVo findArticleById(Long id);
}
package com.raxcl.blog.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.raxcl.blog.dao.dto.Archives;
import com.raxcl.blog.dao.mapper.ArticleBodyMapper;
import com.raxcl.blog.dao.mapper.ArticleMapper;
import com.raxcl.blog.dao.pojo.Article;
import com.raxcl.blog.dao.pojo.ArticleBody;
import com.raxcl.blog.service.ArticleService;
import com.raxcl.blog.service.CategoryService;
import com.raxcl.blog.service.SysUserService;
import com.raxcl.blog.service.TagService;
import com.raxcl.blog.vo.ArticleBodyVo;
import com.raxcl.blog.vo.ArticleVo;
import com.raxcl.blog.vo.param.PageParams;
import com.raxcl.blog.vo.Result;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class ArticleServiceImpl implements ArticleService {
private final ArticleMapper articleMapper;
private final TagService tagService;
private final SysUserService sysUserService;
private final ArticleBodyMapper articleBodyMapper;
private final CategoryService categoryService;
public ArticleServiceImpl(ArticleMapper articleMapper, TagService tagService, SysUserService sysUserService, ArticleBodyMapper articleBodyMapper, CategoryService categoryService) {
this.articleMapper = articleMapper;
this.tagService = tagService;
this.sysUserService = sysUserService;
this.articleBodyMapper = articleBodyMapper;
this.categoryService = categoryService;
}
@Override
public ArticleVo findArticleById(Long id) {
Article article = articleMapper.selectById(id);
return copy(article, true, true, true,true);
}
private ArticleBodyVo findArticleBodyById(Long bodyId) {
ArticleBody articleBody = articleBodyMapper.selectById(bodyId);
ArticleBodyVo articleBodyVo = new ArticleBodyVo();
articleBodyVo.setContent(articleBody.getContent());
return articleBodyVo;
}
}
package com.raxcl.blog.service;
import com.raxcl.blog.vo.CategoryVo;
import java.util.List;
public interface CategoryService {
CategoryVo findCategoryById(Long categoryId);
}
package com.raxcl.blog.service.impl;
import com.raxcl.blog.dao.mapper.CategoryMapper;
import com.raxcl.blog.dao.pojo.Category;
import com.raxcl.blog.service.CategoryService;
import com.raxcl.blog.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl implements CategoryService {
private final CategoryMapper categoryMapper;
public CategoryServiceImpl(CategoryMapper categoryMapper) {
this.categoryMapper = categoryMapper;
}
@Override
public CategoryVo findCategoryById(Long categoryId) {
Category category = categoryMapper.selectById(categoryId);
CategoryVo categoryVo = new CategoryVo();
BeanUtils.copyProperties(category, categoryVo);
return categoryVo;
}
}
14. 阅读数实现(线程池实现)
14.1实现过程
package com.raxcl.blog.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class ThreadPoolConfig {
@Bean("taskExecutor")
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//设置核心线程数
executor.setCorePoolSize(5);
//设置最大线程数
executor.setMaxPoolSize(20);
//配置队列大小
executor.setQueueCapacity(Integer.MAX_VALUE);
//设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
//设置默认线程名称
executor.setThreadNamePrefix("个人博客基础框架");
//等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
//执行初始化
executor.initialize();
return executor;
}
}
package com.raxcl.blog.service.impl;
@Service
public class ArticleServiceImpl implements ArticleService {
@Override
public ArticleVo findArticleById(Long id) {
Article article = articleMapper.selectById(id);
ArticleVo articleVo = copy(article, true, true, true, true);
//查询文章之后,更新阅读数
//采用线程池
threadService.updateArticleViewCount(articleMapper,article);
return articleVo;
}
}
package com.raxcl.blog.service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.raxcl.blog.dao.mapper.ArticleMapper;
import com.raxcl.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class ThreadService {
//期望此操作在线程池执行,不会影响原有的主线程
@Async("taskExecutor")
public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
int viewCounts = article.getViewCounts();
Article articleUpdate = new Article();
articleUpdate.setViewCounts(viewCounts+1);
LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(Article::getId, article.getId());
//查询之前阅读量,确保在多线程的环境下,线程安全
updateWrapper.eq(Article::getViewCounts,viewCounts);
// update article set view_count=100 where view_count=99 and id=11
articleMapper.update(articleUpdate,updateWrapper);
try {
Thread.sleep(5000);
System.out.println("更新完成了......");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
14.2 注意点
值得注意的是,articleMapper.update(articleUpdate,updateWrapper);
更新时会将对应实体类中有默认值的字段也作为参数,因此我们需要将对应实体类中的int类型改为Integer类型
package com.raxcl.blog.dao.pojo;
import lombok.Data;
@Data
public class Article {
public static final int Article_TOP =1;
public static final int Article_Common = 0;
private Long id;
/**
*评论数量
*/
private Integer commentCounts;
/**
* 创建时间
*/
private Long createDate;
/**
* 简介
*/
private String summary;
/**
* 标题
*/
private String title;
/**
* 浏览数量
*/
private Integer viewCounts;
/**
* 是否置顶
*/
private Integer weight;
/**
* 作者id
*/
private Long authorId;
/**
* 内容id
*/
private Long bodyId;
/**
* 类别id
*/
private Long categoryId;
}
package com.raxcl.blog.vo;
import lombok.Data;
import java.util.List;
@Data
public class ArticleVo {
private Long id;
/**
* 标题
*/
private String title;
/**
* 简介
*/
private String summary;
/**
*评论数量
*/
private Integer commentCounts;
/**
* 浏览数量
*/
private Integer viewCounts;
/**
* 是否置顶
*/
private Integer weight;
/**
* 创建时间
*/
private String createDate;
/**
* 作者
*/
private String author;
private ArticleBodyVo body;
private List<TagVo> tags;
private CategoryVo category;
/**
* 作者id
*/
private Long authorId;
/**
* 内容id
*/
private Long bodyId;
/**
* 类别id
*/
private Integer categoryId;
}
15 下一篇入口
由于字数太会导致卡顿,故而新开一章:
点这里跳转:基于springboot + vue 的个人博客搭建过程(续)