【SpringBoot3+Vue3】二【实战篇】-后端

目录

一、环境搭建

1、数据库脚本

2、pom

3、yml

4、通过mybatis-X生成实体pojo等

4.1 Article

4.2 Category

4.3 User

5、 Mapper

5.1 ArticleMapper

5.2 CategoryMapper

5.3 UserMapper

6、service

6.1 ArticleService

6.2 CategoryService

6.3 UserService

7、serviceImpl

7.1 ArticleServiceImpl

7.2 CategoryServiceImpl

7.3 UserServiceImpl

8、统一返回类Result

9、工具类

9.1 MD5加密工具类Md5Util

9.2 jwt生成token工具类JwtUtils 

9.3 工具类ThreadLocalUtil

 9.4 AliOSSProperties阿里云参数实体

9.5 阿里云AliOSSUtils

10、全局异常处理类GlobalExceptionHandler

11、拦截器LoginCheckInterceptor

12、配置类

 12.1 配置类WebConfig注册拦截器

12.2   MybatisPlus分页配置类MybatisPlusConfig

13、自定义注解(新增文章)

13.1 新增文章自定义注解State接口

13.2 新增文章自定义注解实现类StateValidation

14、封装分页实体PageBean

二、用户

1、注册

1.1 UserController

1.2 service

1.3 serviceImpl

2、登录

2.1 UserController

2.2 登录认证 

3、获取用户详细信息

3.1 UserController

 3.2 servcie

3.3 servcieImpl

3.4 优化版本UserController(使用ThreadLocal)

4、更新用户基本信息

4.1 UserController

4.2 servcie

4.3 serviceImpl

5、更新用户头像

5.2 UserController 

5.2 servcie

5.3 servcieImpl

6、更新用户密码

6.1 UserController

6.2 service

6.3 serviceImpl

三、文章分类

1、新增文章分类

1.1 CategoryController

1.2 service

1.3 servcieImpl

2、文章分类列表

2.1 CategoryController

2.2 service

2.3 servcieImpl

3、 获取文章分类详情

3.1 CategoryController

3.2 service

3.3 servcieImpl

4、更新文章分类

4.1 CategoryController

4.2 service

4.3 servcieImpl

5、删除文章分类

5.1 CategoryController

5.2 service

5.3 servcieImpl

四、文章管理

1、新增文章

1.1 ArticleCtroller

1.2 service

1.3 serviceImpl

2、文章列表(条件分页)

2.1 ArticleCtroller

2.2 service

2.3 serviceImpl

3、获取文章详情

3.1 ArticleCtroller

3.2 service

3.3 serviceImpl

4、更新文章

4.1 ArticleCtroller

4.2 service

4.3 serviceImpl

5、删除文章

5.1 ArticleCtroller

5.2 service

5.3 serviceImpl

五、上传接口

1、FileUploadController


前言:SpringBoot3+Vue3项目实战-后端

一、环境搭建

1、数据库脚本

-- 用户表
create table user (id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用户名',password varchar(32)  comment '密码',nickname varchar(10)  default '' comment '昵称',email varchar(128) default '' comment '邮箱',user_pic varchar(128) default '' comment '头像',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间'
) comment '用户表';-- 分类表
create table category(id int unsigned primary key auto_increment comment 'ID',category_name varchar(32) not null comment '分类名称',category_alias varchar(32) not null comment '分类别名',create_user int unsigned not null comment '创建人ID',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间',constraint fk_category_user foreign key (create_user) references user(id) -- 外键约束
);-- 文章表
create table article(id int unsigned primary key auto_increment comment 'ID',title varchar(30) not null comment '文章标题',content varchar(10000) not null comment '文章内容',cover_img varchar(128) not null  comment '文章封面',state varchar(3) default '草稿' comment '文章状态: 只能是[已发布] 或者 [草稿]',category_id int unsigned comment '文章分类ID',create_user int unsigned not null comment '创建人ID',create_time datetime not null comment '创建时间',update_time datetime not null comment '修改时间',constraint fk_article_category foreign key (category_id) references category(id),-- 外键约束constraint fk_article_user foreign key (create_user) references user(id) -- 外键约束
)

2、pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.1.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.bocai</groupId><artifactId>mybatis-plus-module</artifactId><version>0.0.1-SNAPSHOT</version><name>mybatis-plus-module</name><description>mybatis-plus-module</description><properties><java.version>17</java.version><aliyun-sdk-oss.version>3.15.1</aliyun-sdk-oss.version><jaxb-api.version>2.3.1</jaxb-api.version><activation.version>1.1.1</activation.version><jaxb-runtime.version>2.3.3</jaxb-runtime.version><mybatis-plus.version>3.5.3</mybatis-plus.version><druid.version>1.1.23</druid.version><jjwt.version>0.9.0</jjwt.version><fastjson.version>1.2.76</fastjson.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--  校验 引入validation --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><!--  3、 引入mybatisplus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>${druid.version}</version></dependency><!--        阿里云OSS依赖--><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>${aliyun-sdk-oss.version}</version></dependency><!--  如果是java9 还需要下面几个依赖   本项目按理不需要--><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>${jaxb-api.version}</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>${activation.version}</version></dependency><!-- no more than 2.3.3--><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>${jaxb-runtime.version}</version></dependency><!--  上面都是阿里云的 依赖-  --><!--JWT令牌--><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>${jjwt.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><image><builder>paketobuildpacks/builder-jammy-base:latest</builder></image><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

3、yml

spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot_vue?serverTimezone=UTCusername: rootpassword: Miamimain:banner-mode: off  # 关闭控制台springboot的logomybatis-plus:configuration:map-underscore-to-camel-case: true # 在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 控制台显示sqldefault-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler  # 配置全局枚举处理器,好像还有说json的global-config:db-config:id-type: auto   # 数据库id生产规则全局 配置 # ASSIGN_ID雪花算法,数据库id建议使用Long类型logic-delete-field: deleted # 全局配置逻辑删除字段名logic-delete-value: 0 # 全局配置# 逻辑已删除值(默认为 1)这里因为我是反的所以改成0logic-not-delete-value: 1  # 逻辑未删除值(默认为 0)这里因为我是反的所以改成1# table-prefix: tbl_    # 数据库表前缀全局配置banner: false   # 关闭控制台mybatis-plus的logo
#  type-enums-package: com.bocai.enums   # 扫描通用枚举包 或者使用上面那个枚举全局配置# 阿里云OSS配置
aliyun:oss:endpoint: https://oss-cn-hanzhou.aliyuncs.comaccessKeyId: LTjfk332422slksPqyaccessKeySecret: Pfslfksd2;lf2sALHfdsfsTHm6fdsfsdsRbucketName: web-spring3bocai

4、通过mybatis-X生成实体pojo等

4.1 Article

package com.bocai.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;import com.bocai.anno.State;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.groups.Default;
import lombok.Data;
import org.hibernate.validator.constraints.URL;/*** * @TableName article*/
@TableName(value ="article")
@Data
public class Article implements Serializable {/*** ID*/@NotNull(groups = Article.Update.class)@TableId(type = IdType.AUTO)private Integer id;/*** 文章标题*/@NotEmpty@Pattern(regexp = "^\\S{1,10}$")private String title;/*** 文章内容*/@NotEmptyprivate String content;/*** 文章封面*/@NotEmpty@URLprivate String coverImg;/*** 文章状态: 只能是[已发布] 或者 [草稿]*/@State  //自定义注解private String state;/*** 文章分类ID*/@NotNullprivate Integer categoryId;/*** 创建人ID*/@JsonIgnore  // json返回不显示private Integer createUser;/*** 创建时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime createTime;/*** 修改时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime updateTime;@TableField(exist = false)private static final long serialVersionUID = 1L;// 如果说某个校验项没有指定分钟,默认属于Default分组//分组之间可以继承,A extend B, 那么A 就拥有B的所有校验项/*** 为了校验分组*/public interface Add extends Default {}/*** 为了校验分组*/public interface Update extends Default{}
}

4.2 Category

package com.bocai.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.groups.Default;
import lombok.Data;/*** * @TableName category*/
@TableName(value ="category")
@Data
public class Category implements Serializable {/*** ID*/@NotNull(groups = Update.class)@TableId(type = IdType.AUTO)private Integer id;/*** 分类名称*/@NotEmpty  //值不能为空,并且内容不能为空private String categoryName;/*** 分类别名*/@NotEmpty //值不能为空,并且内容不能为空
//    @NotEmpty(groups = {Update.class,Add.class})private String categoryAlias;/*** 创建人ID*/@JsonIgnore  // json返回不显示private Integer createUser;/*** 创建时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime createTime;/*** 修改时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime updateTime;@TableField(exist = false)private static final long serialVersionUID = 1L;// 如果说某个校验项没有指定分钟,默认属于Default分组//分组之间可以继承,A extend B, 那么A 就拥有B的所有校验项/*** 为了校验分组*/public interface Add extends Default {}/*** 为了校验分组*/public interface Update extends Default{}
}

4.3 User

package com.bocai.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;/*** 用户表* @TableName user*/
@TableName(value ="user")
@Data
public class User implements Serializable {/*** ID*/@NotNull //不能为空@TableId(type = IdType.AUTO)private Integer id;/*** 用户名*/private String username;/*** 密码*/@JsonIgnore //springboot把当前对象转换成json字符串的时候忽略这个字段private String password;/*** 昵称*/@NotEmpty  //值不能为空,并且内容不能为空@Pattern(regexp = "^\\S{1,10}$")  //正则private String nickname;/*** 邮箱*/@Email //满足邮箱格式private String email;/*** 头像*/private String userPic;/*** 创建时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime createTime;/*** 修改时间*/@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private LocalDateTime updateTime;@TableField(exist = false)private static final long serialVersionUID = 1L;
}

5、 Mapper

5.1 ArticleMapper

package com.bocai.mapper;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;/**
* @author cheng
* @description 针对表【article】的数据库操作Mapper
* @createDate 2023-11-13 19:55:11
* @Entity com.bocai.pojo.Article
*/
@Mapper
public interface ArticleMapper extends BaseMapper<Article> {}

5.2 CategoryMapper

package com.bocai.mapper;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;/**
* @author cheng
* @description 针对表【category】的数据库操作Mapper
* @createDate 2023-11-13 19:55:04
* @Entity com.bocai.pojo.Category
*/
@Mapper
public interface CategoryMapper extends BaseMapper<Category> {}

5.3 UserMapper

package com.bocai.mapper;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Mapper
* @createDate 2023-11-13 19:54:41
* @Entity com.bocai.pojo.User
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {}

6、service

6.1 ArticleService

package com.bocai.service;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【article】的数据库操作Service
* @createDate 2023-11-13 19:55:11
*/
public interface ArticleService extends IService<Article> {}

6.2 CategoryService

package com.bocai.service;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【category】的数据库操作Service
* @createDate 2023-11-13 19:55:04
*/
public interface CategoryService extends IService<Category> {}

6.3 UserService

package com.bocai.service;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2023-11-13 19:54:41
*/
public interface UserService extends IService<User> {}

7、serviceImpl

7.1 ArticleServiceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Article;
import com.bocai.service.ArticleService;
import com.bocai.mapper.ArticleMapper;
import org.springframework.stereotype.Service;/**
* @author cheng
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-11-13 19:55:11
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>implements ArticleService{}

7.2 CategoryServiceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Category;
import com.bocai.service.CategoryService;
import com.bocai.mapper.CategoryMapper;
import org.springframework.stereotype.Service;/**
* @author cheng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-11-13 19:55:04
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService{}

7.3 UserServiceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import org.springframework.stereotype.Service;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{}

8、统一返回类Result

package com.bocai.common;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {private Integer code;//响应码,1 代表成功; 0 代表失败private String msg;  //响应信息 描述字符串private Object data; //返回的数据//增删改 成功响应public static Result success(){return new Result(1,"success",null);}//查询 成功响应public static Result success(Object data){return new Result(1,"success",data);}//失败响应public static Result error(String msg){return new Result(0,msg,null);}
}

9、工具类

9.1 MD5加密工具类Md5Util

package com.bocai.utils;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class Md5Util {/*** 默认的密码字符串组合,用来将字节转换成 16 进制表示的字符,apache校验下载的文件的正确性用的就是默认的这个组合*/protected static char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};protected static MessageDigest messagedigest = null;static {try {messagedigest = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException nsaex) {System.err.println(Md5Util.class.getName() + "初始化失败,MessageDigest不支持MD5Util。");nsaex.printStackTrace();}}/*** 生成字符串的md5校验值** @param s* @return*/public static String getMD5String(String s) {return getMD5String(s.getBytes());}/*** 判断字符串的md5校验码是否与一个已知的md5码相匹配** @param password  要校验的字符串* @param md5PwdStr 已知的md5校验码* @return*/public static boolean checkPassword(String password, String md5PwdStr) {String s = getMD5String(password);return s.equals(md5PwdStr);}public static String getMD5String(byte[] bytes) {messagedigest.update(bytes);return bufferToHex(messagedigest.digest());}private static String bufferToHex(byte bytes[]) {return bufferToHex(bytes, 0, bytes.length);}private static String bufferToHex(byte bytes[], int m, int n) {StringBuffer stringbuffer = new StringBuffer(2 * n);int k = m + n;for (int l = m; l < k; l++) {appendHexPair(bytes[l], stringbuffer);}return stringbuffer.toString();}private static void appendHexPair(byte bt, StringBuffer stringbuffer) {char c0 = hexDigits[(bt & 0xf0) >> 4];// 取字节中高 4 位的数字转换, >>>// 为逻辑右移,将符号位一起右移,此处未发现两种符号有何不同char c1 = hexDigits[bt & 0xf];// 取字节中低 4 位的数字转换stringbuffer.append(c0);stringbuffer.append(c1);}}

9.2 jwt生成token工具类JwtUtils 

package com.bocai.utils;import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;import java.util.Date;
import java.util.Map;public class JwtUtils {private static String signKey = "bocai";private static Long expire = 43200000L;  // 12h/*** 生成JWT令牌* @param claims JWT第二部分负载 payload 中存储的内容* @return*/public static String generateJwt(Map<String, Object> claims){String jwt = Jwts.builder().addClaims(claims).signWith(SignatureAlgorithm.HS256, signKey).setExpiration(new Date(System.currentTimeMillis() + expire)).compact();return jwt;}/*** 解析JWT令牌* @param jwt JWT令牌* @return JWT第二部分负载 payload 中存储的内容*/public static Claims parseJWT(String jwt){Claims claims = Jwts.parser().setSigningKey(signKey).parseClaimsJws(jwt).getBody();return claims;}
}

9.3 工具类ThreadLocalUtil

package com.bocai.utils;import java.util.HashMap;
import java.util.Map;/*** ThreadLocal 工具类*/
@SuppressWarnings("all")
public class ThreadLocalUtil {//提供ThreadLocal对象,private static final ThreadLocal THREAD_LOCAL = new ThreadLocal();//根据键获取值public static <T> T get(){return (T) THREAD_LOCAL.get();}//存储键值对public static void set(Object value){THREAD_LOCAL.set(value);}//清除ThreadLocal 防止内存泄漏public static void remove(){THREAD_LOCAL.remove();}
}

 9.4 AliOSSProperties阿里云参数实体

package com.bocai.utils;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliOSSProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;
}

9.5 阿里云AliOSSUtils

package com.bocai.utils;import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;/*** 阿里云 OSS 工具类*/
@Component
public class AliOSSUtils {//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint;
//    @Value("${aliyun.oss.accessKeyId}")
//    private String accessKeyId;
//    @Value("${aliyun.oss.accessKeySecret}")
//    private String accessKeySecret;
//    @Value("${aliyun.oss.bucketName}")
//    private String bucketName;@Autowiredprivate AliOSSProperties aliOSSProperties;/*** 实现上传图片到OSS*/public String upload(MultipartFile file) throws IOException {// 获取阿里云OSS参数String endpoint = aliOSSProperties.getEndpoint();String accessKeyId = aliOSSProperties.getAccessKeyId();String accessKeySecret = aliOSSProperties.getAccessKeySecret();String bucketName = aliOSSProperties.getBucketName();// 获取上传的文件的输入流InputStream inputStream = file.getInputStream();// 避免文件覆盖String originalFilename = file.getOriginalFilename();String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));//上传文件到 OSSOSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);ossClient.putObject(bucketName, fileName, inputStream);//文件访问路径String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;// 关闭ossClientossClient.shutdown();return url;// 把上传到oss的路径返回}}

10、全局异常处理类GlobalExceptionHandler

package com.bocai.exception;import com.bocai.common.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;/*** =========================全局异常处理器========================*/
@RestControllerAdvice
public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)//捕获所有异常public Result ex(Exception ex){ex.printStackTrace();return Result.error("对不起,操作失败,请联系管理员");}}

11、拦截器LoginCheckInterceptor

package com.bocai.interceptor;import com.alibaba.fastjson.JSONObject;
import com.bocai.common.Result;
import com.bocai.utils.JwtUtils;
import com.bocai.utils.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import java.util.Map;/***   =========================LoginCheckInterceptor 拦截器 interceptor========================*/
@Slf4j
@Component
public class LoginCheckInterceptor implements HandlerInterceptor {@Override //目标资源方法运行前运行, 返回true: 放行, 放回false, 不放行public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {//1.获取请求url。String url = req.getRequestURL().toString();log.info("请求的url: {}",url);//2.判断请求url中是否包含login,如果包含,说明是登录操作,放行。if(url.contains("login")){log.info("登录操作, 放行...");return true;}//3.获取请求头中的令牌( Authorization)。String jwt = req.getHeader("Authorization");//4.判断令牌是否存在,如果不存在,返回错误结果(未登录)。if(!StringUtils.hasLength(jwt)){log.info("请求头Authorization为空,返回未登录的信息");Result error = Result.error("NOT_LOGIN");//手动转换 对象--json --------> 阿里巴巴fastJSONString notLogin = JSONObject.toJSONString(error);resp.getWriter().write(notLogin);return false;}//5.解析token,如果解析失败,返回错误结果(未登录)。try {Map<String, Object> claims = JwtUtils.parseJWT(jwt);//6.把业务数据存储到ThreadLocal中ThreadLocalUtil.set(claims);//7.放行。log.info("令牌合法, 放行");return true;} catch (Exception e) {//jwt解析失败e.printStackTrace();log.info("解析令牌失败, 返回未登录错误信息");Result error = Result.error("NOT_LOGIN");//手动转换 对象--json --------> 阿里巴巴fastJSONString notLogin = JSONObject.toJSONString(error);resp.getWriter().write(notLogin);return false;}}@Override //目标资源方法运行后运行public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle ...");}@Override //视图渲染完毕后运行, 最后运行public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {// 清空ThreadLocal中的数据,防止内存泄漏ThreadLocalUtil.remove();System.out.println("afterCompletion...");}
}

12、配置类

 12.1 配置类WebConfig注册拦截器

package com.bocai.config;import com.bocai.interceptor.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/***   =========================LoginCheckInterceptor 的配置类 ========================*/
@Configuration //配置类
public class WebConfig implements WebMvcConfigurer {@Autowiredprivate LoginCheckInterceptor loginCheckInterceptor;/*** 注册拦截器, 拦截所有url,除登录* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginCheckInterceptor).addPathPatterns("/**").excludePathPatterns("/user/login","/user/register");}
}

12.2   MybatisPlus分页配置类MybatisPlusConfig

package com.bocai.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 配置MP的分页插件*/
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){// 1、定义MybatisPlus拦截器MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();// 2、添加分页的拦截器mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));// 3、添加乐观锁的拦截器mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());return mybatisPlusInterceptor;}
}

13、自定义注解(新增文章)

13.1 新增文章自定义注解State接口

package com.bocai.anno;import com.bocai.validation.StateValidation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import jakarta.validation.constraints.NotEmpty;import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;@Documented//元注解
@Target({ FIELD})//元注解
@Retention(RUNTIME)//元注解
@Constraint(validatedBy = { StateValidation.class})//指定提供校验规则的类
public @interface State {//提供校验失败后的提示信息String message() default "state参数的值只能是已发布或者草稿";//指定分组Class<?>[] groups() default { };//负载  获取到State注解的附加信息Class<? extends Payload>[] payload() default { };
}

13.2 新增文章自定义注解实现类StateValidation

package com.bocai.validation;import com.bocai.anno.State;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;public class StateValidation implements ConstraintValidator<State,String> {/**** @param value 将来要校验的数据* @param context context in which the constraint is evaluated** @return 如果返回false,则校验不通过,如果返回true,则校验通过*/@Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {//提供校验规则if (value == null){return false;}if (value.equals("已发布") || value.equals("草稿")){return true;}return false;}
}

14、封装分页实体PageBean

package com.bocai.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;/*** 分页查询结果封装类*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {private Long total;//总记录数private List items;//数据列表}

二、用户

1、注册

1.1 UserController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username,password);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}}

1.2 service

package com.bocai.service;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2023-11-13 19:54:41
*/
public interface UserService extends IService<User> {/*** 用户注册* @param username* @param password*/void register(String username, String password);/*** 根据用户名 --查询用户* @param username  * @return*/User queryUserByUsername(String username);
}

1.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import com.bocai.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{@Autowiredprivate UserMapper userMapper;/*** 注册* @param username* @param password*/@Overridepublic void register(String username, String password) {//密码加密String md5String = Md5Util.getMD5String(password);User user = new User();user.setUsername(username);user.setPassword(md5String);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}/*** 查询用户* @param username    * @return*/@Overridepublic User queryUserByUsername(String username) {//查询用户LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(username != null, User::getUsername,username);User user = userMapper.selectOne(lambdaQueryWrapper);return user;}
}

2、登录

2.1 UserController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {log.info("登录用户名:{},密码为:{}", username, password);// 查询用户User loginUser = userService.queryUserByUsername(username);// 判断用户是否存在if (loginUser == null) {return Result.error("用户名不存在");}// 判断密码是否正确,loginUser对象中的password是密文if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){// 登录成功return Result.success("JWT");}return Result.error("密码错误!");}
}

2.2 登录认证 

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import com.bocai.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{@Autowiredprivate UserMapper userMapper;/*** 注册* @param username* @param password*/@Overridepublic void register(String username, String password) {//密码加密String md5String = Md5Util.getMD5String(password);User user = new User();user.setUsername(username);user.setPassword(md5String);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}/*** 查询用户* @param username* @return*/@Overridepublic User queryUserByUsername(String username) {//查询用户LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(username != null, User::getUsername,username);User user = userMapper.selectOne(lambdaQueryWrapper);return user;}
}

3、获取用户详细信息

3.1 UserController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.utils.JwtUtils;
import com.bocai.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {log.info("登录用户名:{},密码为:{}", username, password);// 查询用户User loginUser = userService.queryUserByUsername(username);// 判断用户是否存在if (loginUser == null) {return Result.error("用户名不存在");}// 判断密码是否正确,loginUser对象中的password是密文if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){// 登录成功Map<String, Object> claims = new HashMap<>();claims.put("id", loginUser.getId());claims.put("username",loginUser.getUsername());String jwt = JwtUtils.generateJwt(claims); //让jwt包含了当前登录的员工信息return Result.success(jwt);}return Result.error("密码错误!");}/*** 查询用户信息(从token获取)* @param token* @return*/@GetMapping("/userInfo")public Result userInfo(@RequestHeader(name = "Authorization")String token){log.info("查询用户全部信息");//根据用户名查询用户Map<String, Object> claims = JwtUtils.parseJWT(token);String username = (String) claims.get("username");User user = userService.queryUserByUsername(username);return Result.success(user);}
}

 3.2 servcie

package com.bocai.service;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2023-11-13 19:54:41
*/
public interface UserService extends IService<User> {/*** 用户注册* @param username* @param password*/void register(String username, String password);/*** 根据用户名 --查询用户* @param username* @return*/User queryUserByUsername(String username);/*** 更新用户* @param user*/void updateUser(User user);}

3.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{@Autowiredprivate UserMapper userMapper;/*** 注册* @param username* @param password*/@Overridepublic void register(String username, String password) {//密码加密String md5String = Md5Util.getMD5String(password);User user = new User();user.setUsername(username);user.setPassword(md5String);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}/*** 查询用户* @param username* @return*/@Overridepublic User queryUserByUsername(String username) {//查询用户LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(username != null, User::getUsername,username);User user = userMapper.selectOne(lambdaQueryWrapper);return user;}/*** 更新用户* @param user*/@Overridepublic void updateUser(User user) {user.setUpdateTime(LocalDateTime.now());userMapper.updateById(user);}}

3.4 优化版本UserController(使用ThreadLocal)

 

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.utils.JwtUtils;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {log.info("登录用户名:{},密码为:{}", username, password);// 查询用户User loginUser = userService.queryUserByUsername(username);// 判断用户是否存在if (loginUser == null) {return Result.error("用户名不存在");}// 判断密码是否正确,loginUser对象中的password是密文if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){// 登录成功Map<String, Object> claims = new HashMap<>();claims.put("id", loginUser.getId());claims.put("username",loginUser.getUsername());String jwt = JwtUtils.generateJwt(claims); //让jwt包含了当前登录的员工信息return Result.success(jwt);}return Result.error("密码错误!");}/*** 查询用户信息(从线程获取)* @return*/@GetMapping("/userInfo")public Result userInfo(){// 从线程获取存储的jwt信息Map<String, Object> map = ThreadLocalUtil.get();String username = (String) map.get("username");log.info("查询用户全部信息,从token获取信息为:{}",username);//根据用户名查询用户User user = userService.queryUserByUsername(username);return Result.success(user);}//使用上面的的优化版本
//    /**
//     * 查询用户信息(从token获取)
//     * @param token
//     * @return
//     */
//    @GetMapping("/userInfo")
//    public Result userInfo(@RequestHeader(name = "Authorization")String token){
//        log.info("查询用户全部信息,从token获取信息为:{}",token);
//        //根据用户名查询用户
//        Map<String, Object> claims = JwtUtils.parseJWT(token);
//        String username = (String) claims.get("username");
//        User user = userService.queryUserByUsername(username);
//        return Result.success(user);
//    }
}

4、更新用户基本信息

4.1 UserController

@Validated

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.utils.JwtUtils;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {log.info("登录用户名:{},密码为:{}", username, password);// 查询用户User loginUser = userService.queryUserByUsername(username);// 判断用户是否存在if (loginUser == null) {return Result.error("用户名不存在");}// 判断密码是否正确,loginUser对象中的password是密文if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){// 登录成功Map<String, Object> claims = new HashMap<>();claims.put("id", loginUser.getId());claims.put("username",loginUser.getUsername());String jwt = JwtUtils.generateJwt(claims); //让jwt包含了当前登录的员工信息return Result.success(jwt);}return Result.error("密码错误!");}/*** 查询用户信息(从线程获取)* @return*/@GetMapping("/userInfo")public Result userInfo(){// 从线程获取存储的jwt信息Map<String, Object> map = ThreadLocalUtil.get();String username = (String) map.get("username");log.info("查询用户全部信息,从token获取信息为:{}",username);//根据用户名查询用户User user = userService.queryUserByUsername(username);return Result.success(user);}//使用上面的的优化版本
//    /**
//     * 查询用户信息(从token获取)
//     * @param token
//     * @return
//     */
//    @GetMapping("/userInfo")
//    public Result userInfo(@RequestHeader(name = "Authorization")String token){
//        log.info("查询用户全部信息,从token获取信息为:{}",token);
//        //根据用户名查询用户
//        Map<String, Object> claims = JwtUtils.parseJWT(token);
//        String username = (String) claims.get("username");
//        User user = userService.queryUserByUsername(username);
//        return Result.success(user);
//    }/*** 更新用户* @param user* @return*/@PutMapping("/update")public Result update(@RequestBody @Validated  User user){log.info("修改的用户为:{}",user);userService.updateUser(user);return Result.success();}
}

4.2 servcie

package com.bocai.service;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2023-11-13 19:54:41
*/
public interface UserService extends IService<User> {/*** 用户注册* @param username* @param password*/void register(String username, String password);/*** 根据用户名 --查询用户* @param username* @return*/User queryUserByUsername(String username);/*** 更新用户* @param user*/void updateUser(User user);}

4.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{@Autowiredprivate UserMapper userMapper;/*** 注册* @param username* @param password*/@Overridepublic void register(String username, String password) {//密码加密String md5String = Md5Util.getMD5String(password);User user = new User();user.setUsername(username);user.setPassword(md5String);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}/*** 查询用户* @param username* @return*/@Overridepublic User queryUserByUsername(String username) {//查询用户LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(username != null, User::getUsername,username);User user = userMapper.selectOne(lambdaQueryWrapper);return user;}/*** 更新用户* @param user*/@Overridepublic void updateUser(User user) {user.setUpdateTime(LocalDateTime.now());userMapper.updateById(user);}}

5、更新用户头像

5.2 UserController 

本内容2个知识点:

1、@URL 判断传参是不是url

2、@PatchMapping

@PutMapping和@PatchMapping是Spring MVC中用于处理HTTP请求的注解,它们在功能和使用上有一些区别。

  1. 请求方式:@PutMapping用于处理PUT请求,而@PatchMapping用于处理PATCH请求。
  2. 请求范围:@PutMapping通常用于执行大规模的替换操作,而不是更新操作。如果省略了订单上的某个属性,那么该属性的值应该被NULL所替代,甚至订单中的某个物品的实体数据也应一起重新设置,否则它们将从订单中移除。而@PatchMapping则用于对资源的部分更新,即只更新请求中指定的属性或关系。
  3. 参数绑定:@PutMapping可以使用@RequestBody注解将请求体中的JSON数据映射到Java对象中,而@PatchMapping则无法直接使用@RequestBody注解,需要使用其他方式来解析请求体中的JSON数据。
package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.utils.JwtUtils;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {log.info("登录用户名:{},密码为:{}", username, password);// 查询用户User loginUser = userService.queryUserByUsername(username);// 判断用户是否存在if (loginUser == null) {return Result.error("用户名不存在");}// 判断密码是否正确,loginUser对象中的password是密文if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){// 登录成功Map<String, Object> claims = new HashMap<>();claims.put("id", loginUser.getId());claims.put("username",loginUser.getUsername());String jwt = JwtUtils.generateJwt(claims); //让jwt包含了当前登录的员工信息return Result.success(jwt);}return Result.error("密码错误!");}/*** 查询用户信息(从线程获取)* @return*/@GetMapping("/userInfo")public Result userInfo(){// 从线程获取存储的jwt信息Map<String, Object> map = ThreadLocalUtil.get();String username = (String) map.get("username");log.info("查询用户全部信息,从token获取信息为:{}",username);//根据用户名查询用户User user = userService.queryUserByUsername(username);return Result.success(user);}//使用上面的的优化版本
//    /**
//     * 查询用户信息(从token获取)
//     * @param token
//     * @return
//     */
//    @GetMapping("/userInfo")
//    public Result userInfo(@RequestHeader(name = "Authorization")String token){
//        log.info("查询用户全部信息,从token获取信息为:{}",token);
//        //根据用户名查询用户
//        Map<String, Object> claims = JwtUtils.parseJWT(token);
//        String username = (String) claims.get("username");
//        User user = userService.queryUserByUsername(username);
//        return Result.success(user);
//    }/*** 更新用户* @param user* @return*/@PutMapping("/update")public Result update(@RequestBody @Validated  User user){log.info("修改的用户为:{}",user);userService.updateUser(user);return Result.success();}/*** 更新头像* @param avatarUrl* @return*/@PatchMapping("/updateAvatar")public Result updateAvatar(@RequestParam @URL String avatarUrl){log.info("头像地址是{}",avatarUrl);userService.updateAvatar(avatarUrl);return Result.success();}
}

5.2 servcie

package com.bocai.service;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2023-11-13 19:54:41
*/
public interface UserService extends IService<User> {/*** 用户注册* @param username* @param password*/void register(String username, String password);/*** 根据用户名 --查询用户* @param username* @return*/User queryUserByUsername(String username);/*** 更新用户* @param user*/void updateUser(User user);/*** 更新头像* @param avatarUrl*/void updateAvatar(String avatarUrl);}

5.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{@Autowiredprivate UserMapper userMapper;/*** 注册* @param username* @param password*/@Overridepublic void register(String username, String password) {//密码加密String md5String = Md5Util.getMD5String(password);User user = new User();user.setUsername(username);user.setPassword(md5String);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}/*** 查询用户* @param username* @return*/@Overridepublic User queryUserByUsername(String username) {//查询用户LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(username != null, User::getUsername,username);User user = userMapper.selectOne(lambdaQueryWrapper);return user;}/*** 更新用户* @param user*/@Overridepublic void updateUser(User user) {user.setUpdateTime(LocalDateTime.now());userMapper.updateById(user);}/*** 更新头像* @param avatarUrl*/@Overridepublic void updateAvatar(String avatarUrl) {Map<String, Object> map = ThreadLocalUtil.get();Integer id = (Integer) map.get("id");User user = new User();user.setId(id);user.setUpdateTime(LocalDateTime.now());user.setUserPic(avatarUrl);userMapper.updateById(user);}}

6、更新用户密码

6.1 UserController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.utils.JwtUtils;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@Slf4j
@Validated
@RequestMapping("/user")
public class UserController {@Autowiredprivate UserService userService;/*** 用户注册* @param username* @param password* @return*/@PostMapping("/register")public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password){log.info("注册用户名:{},密码为:{}",username,password);// 查询用户User user = userService.queryUserByUsername(username);if (user == null){//没有占用,可以注册//注册用户userService.register(username,password);return Result.success();}else{return Result.error("用户名被占用");}}@PostMapping("/login")public Result login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$")String password) {log.info("登录用户名:{},密码为:{}", username, password);// 查询用户User loginUser = userService.queryUserByUsername(username);// 判断用户是否存在if (loginUser == null) {return Result.error("用户名不存在");}// 判断密码是否正确,loginUser对象中的password是密文if(Md5Util.getMD5String(password).equals(loginUser.getPassword())){// 登录成功Map<String, Object> claims = new HashMap<>();claims.put("id", loginUser.getId());claims.put("username",loginUser.getUsername());String jwt = JwtUtils.generateJwt(claims); //让jwt包含了当前登录的员工信息return Result.success(jwt);}return Result.error("密码错误!");}/*** 查询用户信息(从线程获取)* @return*/@GetMapping("/userInfo")public Result userInfo(){// 从线程获取存储的jwt信息Map<String, Object> map = ThreadLocalUtil.get();String username = (String) map.get("username");log.info("查询用户全部信息,从token获取信息为:{}",username);//根据用户名查询用户User user = userService.queryUserByUsername(username);return Result.success(user);}//使用上面的的优化版本
//    /**
//     * 查询用户信息(从token获取)
//     * @param token
//     * @return
//     */
//    @GetMapping("/userInfo")
//    public Result userInfo(@RequestHeader(name = "Authorization")String token){
//        log.info("查询用户全部信息,从token获取信息为:{}",token);
//        //根据用户名查询用户
//        Map<String, Object> claims = JwtUtils.parseJWT(token);
//        String username = (String) claims.get("username");
//        User user = userService.queryUserByUsername(username);
//        return Result.success(user);
//    }/*** 更新用户* @param user* @return*/@PutMapping("/update")public Result update(@RequestBody @Validated  User user){log.info("修改的用户为:{}",user);userService.updateUser(user);return Result.success();}/*** 更新头像* @param avatarUrl* @return*/@PatchMapping("/updateAvatar")public Result updateAvatar(@RequestParam @URL String avatarUrl){log.info("头像地址是{}",avatarUrl);userService.updateAvatar(avatarUrl);return Result.success();}/*** 更新密码* @param params json数据包含old_pwd,new_pwd,re_pwd* @return*/@PatchMapping("/updatePwd")public Result updatePwd(@RequestBody Map<String, String> params){log.info("修改密码传过来数据是:{}",params);// 1、校验参数String old_pwd = params.get("old_pwd");String new_pwd = params.get("new_pwd");String re_pwd = params.get("re_pwd");if(!StringUtils.hasLength(old_pwd) || !StringUtils.hasLength(new_pwd) || !StringUtils.hasLength(re_pwd)){return Result.error("缺少必要参数!");}//原密码是否正确Map<String, Object> map = ThreadLocalUtil.get();String username = (String) map.get("username");User loginUser = userService.queryUserByUsername(username);if(!loginUser.getPassword().equals(Md5Util.getMD5String(old_pwd))){return Result.error("原密码不正确!");}//新、老密码是否一致if(old_pwd.equals(new_pwd)){return Result.error("新、老密码一样!!");}//新密码和确认密码不一致!if(!re_pwd.equals(new_pwd)){return Result.error("新密码和确认密码不一致!!");}// 2 、调用userServiceuserService.updatePwd(new_pwd);return Result.success();}
}

6.2 service

package com.bocai.service;import com.bocai.pojo.User;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service
* @createDate 2023-11-13 19:54:41
*/
public interface UserService extends IService<User> {/*** 用户注册* @param username* @param password*/void register(String username, String password);/*** 根据用户名 --查询用户* @param username* @return*/User queryUserByUsername(String username);/*** 更新用户* @param user*/void updateUser(User user);/*** 更新头像* @param avatarUrl*/void updateAvatar(String avatarUrl);/*** 更新密码* @param newPwd*/void updatePwd(String newPwd);
}

6.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.User;
import com.bocai.service.UserService;
import com.bocai.mapper.UserMapper;
import com.bocai.utils.Md5Util;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;/**
* @author cheng
* @description 针对表【user(用户表)】的数据库操作Service实现
* @createDate 2023-11-13 19:54:41
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>implements UserService{@Autowiredprivate UserMapper userMapper;/*** 注册* @param username* @param password*/@Overridepublic void register(String username, String password) {//密码加密String md5String = Md5Util.getMD5String(password);User user = new User();user.setUsername(username);user.setPassword(md5String);user.setCreateTime(LocalDateTime.now());user.setUpdateTime(LocalDateTime.now());userMapper.insert(user);}/*** 查询用户* @param username* @return*/@Overridepublic User queryUserByUsername(String username) {//查询用户LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(username != null, User::getUsername,username);User user = userMapper.selectOne(lambdaQueryWrapper);return user;}/*** 更新用户* @param user*/@Overridepublic void updateUser(User user) {user.setUpdateTime(LocalDateTime.now());userMapper.updateById(user);}/*** 更新头像* @param avatarUrl*/@Overridepublic void updateAvatar(String avatarUrl) {Map<String, Object> map = ThreadLocalUtil.get();Integer id = (Integer) map.get("id");User user = new User();user.setId(id);user.setUpdateTime(LocalDateTime.now());user.setUserPic(avatarUrl);userMapper.updateById(user);}/*** 更新密码* @param newPwd*/@Overridepublic void updatePwd(String newPwd) {Map<String, Object> map = ThreadLocalUtil.get();Integer id = (Integer) map.get("id");User user = new User();user.setId(id);user.setUpdateTime(LocalDateTime.now());user.setPassword(Md5Util.getMD5String(newPwd));userMapper.updateById(user);}
}

三、文章分类

1、新增文章分类

1.1 CategoryController

@Validated

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.Category;
import com.bocai.pojo.User;
import com.bocai.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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
@Slf4j
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;/*** 添加文章分类* @param category* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Category.Add.class) Category category){log.info("新增的文章为:{}",category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有被创建categoryService.add(category);return Result.success();}else{return Result.error("文章分类被占用,不能创建重复");}}}

1.2 service

package com.bocai.service;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【category】的数据库操作Service
* @createDate 2023-11-13 19:55:04
*/
public interface CategoryService extends IService<Category> {/*** 新增文章分类* @param category*/void add(Category category);/*** 根据分类名称查询文章分类* @param category* @return*/Category queryCategoryByCategoryName(Category category);
}

1.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Category;
import com.bocai.service.CategoryService;
import com.bocai.mapper.CategoryMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;/**
* @author cheng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-11-13 19:55:04
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService{@Autowiredprivate CategoryMapper categoryMapper;/*** 新增文章分类* @param category*/@Overridepublic void add(Category category) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());category.setCreateUser(createUserId);categoryMapper.insert(category);}/*** 根据传入参数判断当前对象是否在对象中存在* @param category* @return*/@Overridepublic Category queryCategoryByCategoryName(Category category) {LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(category.getCategoryName() != null, Category::getCategoryName,category.getCategoryName()).ne(category.getId() != null, Category::getId,category.getId()) ;Category queryCategory = categoryMapper.selectOne(lambdaQueryWrapper);return queryCategory;}
}

2、文章分类列表

2.1 CategoryController

1、实体类Category指定日期返回格式   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

2、实体类Category   @JsonIgnore // json返回不显示

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.Category;
import com.bocai.pojo.User;
import com.bocai.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;/*** 添加文章分类* @param category* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Category.Add.class) Category category){log.info("新增的文章为:{}",category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有被创建categoryService.add(category);return Result.success();}else{return Result.error("文章分类被占用,不能创建重复");}}/*** 列表查询当前登录用户创建的文章分类* @return*/@GetMappingpublic Result list(){log.info("列表查询当前登录用户创建的文章分类");List list = categoryService.categorylist();return Result.success(list);}}

2.2 service

package com.bocai.service;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/**
* @author cheng
* @description 针对表【category】的数据库操作Service
* @createDate 2023-11-13 19:55:04
*/
public interface CategoryService extends IService<Category> {/*** 新增文章分类* @param category*/void add(Category category);/*** 根据分类名称查询文章分类* @param category* @return*/Category queryCategoryByCategoryName(Category category);/*** 列表查询当前登录用户创建的文章分类* @return*/List categorylist();
}

2.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Category;
import com.bocai.service.CategoryService;
import com.bocai.mapper.CategoryMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;/**
* @author cheng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-11-13 19:55:04
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService{@Autowiredprivate CategoryMapper categoryMapper;/*** 新增文章分类* @param category*/@Overridepublic void add(Category category) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());category.setCreateUser(createUserId);categoryMapper.insert(category);}/*** 根据传入参数判断当前对象是否在对象中存在* @param category* @return*/@Overridepublic Category queryCategoryByCategoryName(Category category) {LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(category.getCategoryName() != null, Category::getCategoryName,category.getCategoryName()).ne(category.getId() != null, Category::getId,category.getId()) ;Category queryCategory = categoryMapper.selectOne(lambdaQueryWrapper);return queryCategory;}/*** 列表查询当前登录用户创建的文章分类* @return*/@Overridepublic List categorylist() {Map<String, Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(userId != null,Category::getCreateUser,userId);return categoryMapper.selectList(lambdaQueryWrapper);}
}

3、 获取文章分类详情

3.1 CategoryController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.Category;
import com.bocai.pojo.User;
import com.bocai.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;/*** 添加文章分类* @param category* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Category.Add.class) Category category){log.info("新增的文章为:{}",category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有被创建categoryService.add(category);return Result.success();}else{return Result.error("文章分类被占用,不能创建重复");}}/*** 列表查询当前登录用户创建的文章分类* @return*/@GetMappingpublic Result list(){log.info("列表查询当前登录用户创建的文章分类");List list = categoryService.categorylist();return Result.success(list);}/*** 根据id查询文章分类* @param id* @return*/@GetMapping("/detail")public Result detail(Integer id){log.info("查询ID为:{}文章分类详情",id);Category category = categoryService.queryCategoryById(id);return Result.success(category);}}

3.2 service

package com.bocai.service;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/**
* @author cheng
* @description 针对表【category】的数据库操作Service
* @createDate 2023-11-13 19:55:04
*/
public interface CategoryService extends IService<Category> {/*** 新增文章分类* @param category*/void add(Category category);/*** 根据分类名称查询文章分类* @param category* @return*/Category queryCategoryByCategoryName(Category category);/*** 列表查询当前登录用户创建的文章分类* @return*/List categorylist();/*** 根据id查询文章分类* @param id* @return*/Category queryCategoryById(Integer id);
}

3.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Category;
import com.bocai.service.CategoryService;
import com.bocai.mapper.CategoryMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;/**
* @author cheng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-11-13 19:55:04
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService{@Autowiredprivate CategoryMapper categoryMapper;/*** 新增文章分类* @param category*/@Overridepublic void add(Category category) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());category.setCreateUser(createUserId);categoryMapper.insert(category);}/*** 根据传入参数判断当前对象是否在对象中存在* @param category* @return*/@Overridepublic Category queryCategoryByCategoryName(Category category) {LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(category.getCategoryName() != null, Category::getCategoryName,category.getCategoryName()).ne(category.getId() != null, Category::getId,category.getId()) ;Category queryCategory = categoryMapper.selectOne(lambdaQueryWrapper);return queryCategory;}/*** 列表查询当前登录用户创建的文章分类* @return*/@Overridepublic List categorylist() {Map<String, Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(userId != null,Category::getCreateUser,userId);return categoryMapper.selectList(lambdaQueryWrapper);}/*** 根据id查询文章分类* @param id* @return*/@Overridepublic Category queryCategoryById(Integer id) {return  categoryMapper.selectById(id);}
}

4、更新文章分类

4.1 CategoryController

分组校验,这里主要原因是实体id字段新增不需要验证,修改需要验证。

在实体类进行修改,然后在CategoryController指定使用哪个分组

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.Category;
import com.bocai.pojo.User;
import com.bocai.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;/*** 添加文章分类* @param category* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Category.Add.class) Category category){log.info("新增的文章为:{}",category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有被创建categoryService.add(category);return Result.success();}else{return Result.error("文章分类被占用,不能创建重复");}}/*** 列表查询当前登录用户创建的文章分类* @return*/@GetMappingpublic Result list(){log.info("列表查询当前登录用户创建的文章分类");List list = categoryService.categorylist();return Result.success(list);}/*** 根据id查询文章分类* @param id* @return*/@GetMapping("/detail")public Result detail(Integer id){log.info("查询ID为:{}文章分类详情",id);Category category = categoryService.queryCategoryById(id);return Result.success(category);}/*** 根据id修改文章分类* @param category* @return*/@PutMappingpublic Result update(@RequestBody @Validated(Category.Update.class) Category category) {log.info("修改的文章分类为:{}", category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有重复categoryService.updateCategoryById(category);return Result.success();}else{return Result.error("文章分类被占用,不能修改为重复");}}}

4.2 service

package com.bocai.service;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/**
* @author cheng
* @description 针对表【category】的数据库操作Service
* @createDate 2023-11-13 19:55:04
*/
public interface CategoryService extends IService<Category> {/*** 新增文章分类* @param category*/void add(Category category);/*** 根据分类名称查询文章分类* @param category* @return*/Category queryCategoryByCategoryName(Category category);/*** 列表查询当前登录用户创建的文章分类* @return*/List categorylist();/*** 根据id查询文章分类* @param id* @return*/Category queryCategoryById(Integer id);/*** 根据id修改文章分类* @param category*/void updateCategoryById(Category category);
}

4.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Category;
import com.bocai.service.CategoryService;
import com.bocai.mapper.CategoryMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;/**
* @author cheng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-11-13 19:55:04
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService{@Autowiredprivate CategoryMapper categoryMapper;/*** 新增文章分类* @param category*/@Overridepublic void add(Category category) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());category.setCreateUser(createUserId);categoryMapper.insert(category);}/*** 根据传入参数判断当前对象是否在对象中存在* @param category* @return*/@Overridepublic Category queryCategoryByCategoryName(Category category) {LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(category.getCategoryName() != null, Category::getCategoryName,category.getCategoryName()).ne(category.getId() != null, Category::getId,category.getId()) ;Category queryCategory = categoryMapper.selectOne(lambdaQueryWrapper);return queryCategory;}/*** 列表查询当前登录用户创建的文章分类* @return*/@Overridepublic List categorylist() {Map<String, Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(userId != null,Category::getCreateUser,userId);return categoryMapper.selectList(lambdaQueryWrapper);}/*** 根据id查询文章分类* @param id* @return*/@Overridepublic Category queryCategoryById(Integer id) {return  categoryMapper.selectById(id);}/*** 根据id修改文章分类* @param category*/@Overridepublic void updateCategoryById(Category category) {category.setUpdateTime(LocalDateTime.now());categoryMapper.updateById(category);}
}

5、删除文章分类

5.1 CategoryController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.Category;
import com.bocai.pojo.User;
import com.bocai.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {@Autowiredprivate CategoryService categoryService;/*** 添加文章分类* @param category* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Category.Add.class) Category category){log.info("新增的文章为:{}",category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有被创建categoryService.add(category);return Result.success();}else{return Result.error("文章分类被占用,不能创建重复");}}/*** 列表查询当前登录用户创建的文章分类* @return*/@GetMappingpublic Result list(){log.info("列表查询当前登录用户创建的文章分类");List list = categoryService.categorylist();return Result.success(list);}/*** 根据id查询文章分类* @param id* @return*/@GetMapping("/detail")public Result detail(Integer id){log.info("查询ID为:{}文章分类详情",id);Category category = categoryService.queryCategoryById(id);return Result.success(category);}/*** 根据id修改文章分类* @param category* @return*/@PutMappingpublic Result update(@RequestBody @Validated(Category.Update.class) Category category) {log.info("修改的文章分类为:{}", category);// 查询文章分类是否已被创建Category queryCategory = categoryService.queryCategoryByCategoryName(category);if (queryCategory == null){//文章分类没有重复categoryService.updateCategoryById(category);return Result.success();}else{return Result.error("文章分类被占用,不能修改为重复");}}/*** 根据id删除文章分类* @param id* @return*/@DeleteMappingpublic Result delete(Integer id){log.info("根据id删除文章分类{}",id);categoryService.deleteCategoryById(id);return Result.success();}}

5.2 service

package com.bocai.service;import com.bocai.pojo.Category;
import com.baomidou.mybatisplus.extension.service.IService;import java.util.List;/**
* @author cheng
* @description 针对表【category】的数据库操作Service
* @createDate 2023-11-13 19:55:04
*/
public interface CategoryService extends IService<Category> {/*** 新增文章分类* @param category*/void add(Category category);/*** 根据分类名称查询文章分类* @param category* @return*/Category queryCategoryByCategoryName(Category category);/*** 列表查询当前登录用户创建的文章分类* @return*/List categorylist();/*** 根据id查询文章分类* @param id* @return*/Category queryCategoryById(Integer id);/*** 根据id修改文章分类* @param category*/void updateCategoryById(Category category);/*** 根据id删除文章分类* @param id*/void deleteCategoryById(Integer id);
}

5.3 servcieImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Category;
import com.bocai.service.CategoryService;
import com.bocai.mapper.CategoryMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;/**
* @author cheng
* @description 针对表【category】的数据库操作Service实现
* @createDate 2023-11-13 19:55:04
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService{@Autowiredprivate CategoryMapper categoryMapper;/*** 新增文章分类* @param category*/@Overridepublic void add(Category category) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");category.setCreateTime(LocalDateTime.now());category.setUpdateTime(LocalDateTime.now());category.setCreateUser(createUserId);categoryMapper.insert(category);}@Overridepublic Category queryCategoryByCategoryName(String categoryName) {LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(categoryName != null, Category::getCategoryName,categoryName);Category category = categoryMapper.selectOne(lambdaQueryWrapper);return category;}/*** 列表查询当前登录用户创建的文章分类* @return*/@Overridepublic List categorylist() {Map<String, Object> map = ThreadLocalUtil.get();Integer userId = (Integer) map.get("id");LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(userId != null,Category::getCreateUser,userId);return categoryMapper.selectList(lambdaQueryWrapper);}/*** 根据id查询文章分类* @param id* @return*/@Overridepublic Category queryCategoryById(Integer id) {return  categoryMapper.selectById(id);}/*** 根据id修改文章分类* @param category*/@Overridepublic void updateCategoryById(Category category) {category.setUpdateTime(LocalDateTime.now());categoryMapper.updateById(category);}/*** 根据id删除文章分类* @param id*/@Overridepublic void deleteCategoryById(Integer id) {categoryMapper.deleteById(id);}
}

四、文章管理

1、新增文章

1.1 ArticleCtroller

自定义校验

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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
@Slf4j
@RequestMapping("/article")
public class ArticleCtroller {@Autowiredprivate ArticleService articleService;/*** 添加文章* @param article* @return*/@PostMappingpublic Result add(@RequestBody @Validated Article article){log.info("新增文章为:{}",article);//查询文章标题是否被创建Article queryArticle = articleService.queryArticleByTitle(article);if(queryArticle == null){//没有重复的文章标题articleService.add(article);return Result.success();}else {// 存在重复文章标题return Result.error("文章标题重复!");}}
}

1.2 service

package com.bocai.service;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.extension.service.IService;/**
* @author cheng
* @description 针对表【article】的数据库操作Service
* @createDate 2023-11-13 19:55:11
*/
public interface ArticleService extends IService<Article> {/*** 新增文章* @param article*/void add(Article article);/*** 根据文章标题查询文章* @param article* @return*/Article queryArticleByTitle(Article article);
}

1.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.service.ArticleService;
import com.bocai.mapper.ArticleMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;/**
* @author cheng
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-11-13 19:55:11
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>implements ArticleService{@Autowiredprivate ArticleMapper articleMapper;/*** 新增文章* @param article*/@Overridepublic void add(Article article) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");article.setCreateUser(createUserId);article.setCreateTime(LocalDateTime.now());article.setUpdateTime(LocalDateTime.now());articleMapper.insert(article);}/*** 根据文章标题查询文章* @param article* @return*/@Overridepublic Article queryArticleByTitle(Article article) {LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(article.getTitle() != null, Article::getTitle,article.getTitle()).ne(article.getId() != null,Article::getId,article.getId());return articleMapper.selectOne(lambdaQueryWrapper);}
}

2、文章列表(条件分页)

2.1 ArticleCtroller

package com.bocai.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocai.common.Result;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.Emp;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.time.LocalDate;@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleCtroller {@Autowiredprivate ArticleService articleService;/*** 添加文章* @param article* @return*/@PostMappingpublic Result add(@RequestBody @Validated Article article){log.info("新增文章为:{}",article);//查询文章标题是否被创建Article queryArticle = articleService.queryArticleByTitle(article);if(queryArticle == null){//没有重复的文章标题articleService.add(article);return Result.success();}else {// 存在重复文章标题return Result.error("文章标题重复!");}}/*** 文章根据条件查询,并分页* @param pageNum* @param pageSize* @param categoryId* @param state* @return*/@GetMappingpublic Result page(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,String categoryId, String state){log.info("分页查询参数:当前页:{} 每页条数: {} 文章分类ID:{}  状态:{} ",pageNum,pageSize,categoryId,state);PageBean pageBean = articleService.pageList(pageNum,pageSize,categoryId,state);return Result.success(pageBean);}
}

2.2 service

package com.bocai.service;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bocai.pojo.PageBean;/**
* @author cheng
* @description 针对表【article】的数据库操作Service
* @createDate 2023-11-13 19:55:11
*/
public interface ArticleService extends IService<Article> {/*** 新增文章* @param article*/void add(Article article);/*** 根据文章标题查询文章* @param article* @return*/Article queryArticleByTitle(Article article);/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state);
}

2.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import com.bocai.mapper.ArticleMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;/**
* @author cheng
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-11-13 19:55:11
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>implements ArticleService{@Autowiredprivate ArticleMapper articleMapper;/*** 新增文章* @param article*/@Overridepublic void add(Article article) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");article.setCreateUser(createUserId);article.setCreateTime(LocalDateTime.now());article.setUpdateTime(LocalDateTime.now());articleMapper.insert(article);}/*** 根据文章标题查询文章* @param article* @return*/@Overridepublic Article queryArticleByTitle(Article article) {LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(article.getTitle() != null, Article::getTitle,article.getTitle()).ne(article.getId() != null,Article::getId,article.getId());return articleMapper.selectOne(lambdaQueryWrapper);}/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/@Overridepublic PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state) {Map<String,Object> map = ThreadLocalUtil.get();String userId = (String) map.get("id");LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(categoryId != null,Article::getCategoryId,categoryId).eq(state != null,Article::getState,state).eq(userId != null,Article::getCreateUser,userId);Page<Article> pageArticle = new Page<>(pageNum,pageSize);articleMapper.selectPage(pageArticle,lambdaQueryWrapper);PageBean pageBean = new PageBean(pageArticle.getTotal(),pageArticle.getRecords());return pageBean;}
}

3、获取文章详情

3.1 ArticleCtroller

package com.bocai.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocai.common.Result;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.Emp;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.time.LocalDate;@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleCtroller {@Autowiredprivate ArticleService articleService;/*** 添加文章* @param article* @return*/@PostMappingpublic Result add(@RequestBody @Validated Article article){log.info("新增文章为:{}",article);//查询文章标题是否被创建String articleTitle = article.getTitle();Article queryArticle = articleService.queryArticleByTitle(articleTitle);if(queryArticle == null){//没有重复的文章标题articleService.add(article);return Result.success();}else {// 存在重复文章标题return Result.error("文章标题重复!");}}/*** 文章根据条件查询,并分页* @param pageNum* @param pageSize* @param categoryId* @param state* @return*/@GetMappingpublic Result page(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,String categoryId, String state){log.info("分页查询参数:当前页:{} 每页条数: {} 文章分类ID:{}  状态:{} ",pageNum,pageSize,categoryId,state);PageBean pageBean = articleService.pageList(pageNum,pageSize,categoryId,state);return Result.success(pageBean);}/*** 文章详情* @param id* @return*/@GetMapping("/detail")public Result detail(Integer id){log.info("文章id{}详情",id);Article article = articleService.detail(id);return Result.success(article);}
}

3.2 service

package com.bocai.service;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bocai.pojo.PageBean;/**
* @author cheng
* @description 针对表【article】的数据库操作Service
* @createDate 2023-11-13 19:55:11
*/
public interface ArticleService extends IService<Article> {/*** 新增文章* @param article*/void add(Article article);/*** 根据文章标题查询文章* @param articleTitle* @return*/Article queryArticleByTitle(String articleTitle);/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state);/*** 文章详情* @param id* @return*/Article detail(Integer id);
}

3.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import com.bocai.mapper.ArticleMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;/**
* @author cheng
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-11-13 19:55:11
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>implements ArticleService{@Autowiredprivate ArticleMapper articleMapper;/*** 新增文章* @param article*/@Overridepublic void add(Article article) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");article.setCreateUser(createUserId);article.setCreateTime(LocalDateTime.now());article.setUpdateTime(LocalDateTime.now());articleMapper.insert(article);}/*** 根据文章标题查询文章* @param articleTitle* @return*/@Overridepublic Article queryArticleByTitle(String articleTitle) {LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(articleTitle != null, Article::getTitle,articleTitle);return articleMapper.selectOne(lambdaQueryWrapper);}/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/@Overridepublic PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state) {Map<String,Object> map = ThreadLocalUtil.get();String userId = (String) map.get("id");LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(categoryId != null,Article::getCategoryId,categoryId).eq(state != null,Article::getState,state).eq(userId != null,Article::getCreateUser,userId);Page<Article> pageArticle = new Page<>(pageNum,pageSize);articleMapper.selectPage(pageArticle,lambdaQueryWrapper);PageBean pageBean = new PageBean(pageArticle.getTotal(),pageArticle.getRecords());return pageBean;}/*** 文章详情* @param id* @return*/@Overridepublic Article detail(Integer id) {return articleMapper.selectById(id);}
}

4、更新文章

4.1 ArticleCtroller

package com.bocai.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocai.common.Result;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.Emp;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.time.LocalDate;@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleCtroller {@Autowiredprivate ArticleService articleService;/*** 添加文章* @param article* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Article.Add.class) Article article){log.info("新增文章为:{}",article);//查询文章标题是否被创建Article queryArticle = articleService.queryArticleByTitle(article);if(queryArticle == null){//没有重复的文章标题articleService.add(article);return Result.success();}else {// 存在重复文章标题return Result.error("文章标题重复!");}}/*** 文章根据条件查询,并分页* @param pageNum* @param pageSize* @param categoryId* @param state* @return*/@GetMappingpublic Result page(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,String categoryId, String state){log.info("分页查询参数:当前页:{} 每页条数: {} 文章分类ID:{}  状态:{} ",pageNum,pageSize,categoryId,state);PageBean pageBean = articleService.pageList(pageNum,pageSize,categoryId,state);return Result.success(pageBean);}/*** 文章详情* @param id* @return*/@GetMapping("/detail")public Result detail(Integer id){log.info("文章id{}详情",id);Article article = articleService.detail(id);return Result.success(article);}/*** 根据id更新文章* @param article* @return*/@PutMappingpublic Result update(@RequestBody @Validated(Article.Update.class) Article article){log.info("根据id更新文章这些属性{}",article);Article queryArticle = articleService.queryArticleByTitle(article);if(queryArticle == null){articleService.updateArticleById(article);return Result.success();}else {return Result.error("不能修改为文章标题重复名称");}}
}

4.2 service

package com.bocai.service;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bocai.pojo.PageBean;/**
* @author cheng
* @description 针对表【article】的数据库操作Service
* @createDate 2023-11-13 19:55:11
*/
public interface ArticleService extends IService<Article> {/*** 新增文章* @param article*/void add(Article article);/*** 根据文章标题查询文章* @param article* @return*/Article queryArticleByTitle(Article article);/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state);/*** 文章详情* @param id* @return*/Article detail(Integer id);/***     根据id更新文章* @param article*/void updateArticleById(Article article);
}

4.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import com.bocai.mapper.ArticleMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;/**
* @author cheng
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-11-13 19:55:11
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>implements ArticleService{@Autowiredprivate ArticleMapper articleMapper;/*** 新增文章* @param article*/@Overridepublic void add(Article article) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");article.setCreateUser(createUserId);article.setCreateTime(LocalDateTime.now());article.setUpdateTime(LocalDateTime.now());articleMapper.insert(article);}/*** 根据文章标题查询文章* @param article* @return*/@Overridepublic Article queryArticleByTitle(Article article) {LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(article.getTitle() != null, Article::getTitle,article.getTitle()).ne(article.getId() != null,Article::getId,article.getId());return articleMapper.selectOne(lambdaQueryWrapper);}/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/@Overridepublic PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state) {Map<String,Object> map = ThreadLocalUtil.get();String userId = (String) map.get("id");LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(categoryId != null,Article::getCategoryId,categoryId).eq(state != null,Article::getState,state).eq(userId != null,Article::getCreateUser,userId);Page<Article> pageArticle = new Page<>(pageNum,pageSize);articleMapper.selectPage(pageArticle,lambdaQueryWrapper);PageBean pageBean = new PageBean(pageArticle.getTotal(),pageArticle.getRecords());return pageBean;}/*** 文章详情* @param id* @return*/@Overridepublic Article detail(Integer id) {return articleMapper.selectById(id);}/*** 根据id更新文章* @param article*/@Overridepublic void updateArticleById(Article article) {article.setUpdateTime(LocalDateTime.now());articleMapper.updateById(article);}
}

5、删除文章

5.1 ArticleCtroller

package com.bocai.controller;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bocai.common.Result;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.Emp;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;import java.time.LocalDate;@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleCtroller {@Autowiredprivate ArticleService articleService;/*** 添加文章* @param article* @return*/@PostMappingpublic Result add(@RequestBody @Validated(Article.Add.class) Article article){log.info("新增文章为:{}",article);//查询文章标题是否被创建Article queryArticle = articleService.queryArticleByTitle(article);if(queryArticle == null){//没有重复的文章标题articleService.add(article);return Result.success();}else {// 存在重复文章标题return Result.error("文章标题重复!");}}/*** 文章根据条件查询,并分页* @param pageNum* @param pageSize* @param categoryId* @param state* @return*/@GetMappingpublic Result page(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,String categoryId, String state){log.info("分页查询参数:当前页:{} 每页条数: {} 文章分类ID:{}  状态:{} ",pageNum,pageSize,categoryId,state);PageBean pageBean = articleService.pageList(pageNum,pageSize,categoryId,state);return Result.success(pageBean);}/*** 文章详情* @param id* @return*/@GetMapping("/detail")public Result detail(Integer id){log.info("文章id{}详情",id);Article article = articleService.detail(id);return Result.success(article);}/*** 根据id更新文章* @param article* @return*/@PutMappingpublic Result update(@RequestBody @Validated(Article.Update.class) Article article){log.info("根据id更新文章这些属性{}",article);Article queryArticle = articleService.queryArticleByTitle(article);if(queryArticle == null){articleService.updateArticleById(article);return Result.success();}else {return Result.error("不能修改为文章标题重复名称");}}/*** 根据id删除文章* @param id* @return*/@DeleteMappingpublic Result delete(Integer id){log.info("根据id{},删除文章",id);articleService.deleteArticleById(id);return Result.success();}
}

5.2 service

package com.bocai.service;import com.bocai.pojo.Article;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bocai.pojo.PageBean;/**
* @author cheng
* @description 针对表【article】的数据库操作Service
* @createDate 2023-11-13 19:55:11
*/
public interface ArticleService extends IService<Article> {/*** 新增文章* @param article*/void add(Article article);/*** 根据文章标题查询文章* @param article* @return*/Article queryArticleByTitle(Article article);/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state);/*** 文章详情* @param id* @return*/Article detail(Integer id);/***     根据id更新文章* @param article*/void updateArticleById(Article article);/*** 根据id删除文章* @param id*/void deleteArticleById(Integer id);
}

5.3 serviceImpl

package com.bocai.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Article;
import com.bocai.pojo.Category;
import com.bocai.pojo.PageBean;
import com.bocai.service.ArticleService;
import com.bocai.mapper.ArticleMapper;
import com.bocai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDateTime;
import java.util.Map;/**
* @author cheng
* @description 针对表【article】的数据库操作Service实现
* @createDate 2023-11-13 19:55:11
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>implements ArticleService{@Autowiredprivate ArticleMapper articleMapper;/*** 新增文章* @param article*/@Overridepublic void add(Article article) {Map<String, Object> map = ThreadLocalUtil.get();Integer createUserId = (Integer) map.get("id");article.setCreateUser(createUserId);article.setCreateTime(LocalDateTime.now());article.setUpdateTime(LocalDateTime.now());articleMapper.insert(article);}/*** 根据文章标题查询文章* @param article* @return*/@Overridepublic Article queryArticleByTitle(Article article) {LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(article.getTitle() != null, Article::getTitle,article.getTitle()).ne(article.getId() != null,Article::getId,article.getId());return articleMapper.selectOne(lambdaQueryWrapper);}/*** 文章根据条件查询,并分页* @param pageNum 当前页码* @param pageSize 每页条数* @param categoryId 文章分类ID* @param state 状态* @return*/@Overridepublic PageBean pageList(Integer pageNum, Integer pageSize, String categoryId, String state) {Map<String,Object> map = ThreadLocalUtil.get();String userId = (String) map.get("id");LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();lambdaQueryWrapper.eq(categoryId != null,Article::getCategoryId,categoryId).eq(state != null,Article::getState,state).eq(userId != null,Article::getCreateUser,userId);Page<Article> pageArticle = new Page<>(pageNum,pageSize);articleMapper.selectPage(pageArticle,lambdaQueryWrapper);PageBean pageBean = new PageBean(pageArticle.getTotal(),pageArticle.getRecords());return pageBean;}/*** 文章详情* @param id* @return*/@Overridepublic Article detail(Integer id) {return articleMapper.selectById(id);}/*** 根据id更新文章* @param article*/@Overridepublic void updateArticleById(Article article) {article.setUpdateTime(LocalDateTime.now());articleMapper.updateById(article);}/*** 根据id删除文章* @param id*/@Overridepublic void deleteArticleById(Integer id) {articleMapper.deleteById(id);}
}

五、上传接口

1、FileUploadController

package com.bocai.controller;import com.bocai.common.Result;
import com.bocai.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;@RestController
@Slf4j
public class FileUploadController {@Autowiredprivate AliOSSUtils aliOSSUtils;@PostMapping("/upload")public Result upload(MultipartFile file) throws IOException {log.info("上传的文件名:{}",file.getOriginalFilename());String url = aliOSSUtils.upload(file);return Result.success(url);}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/148466.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

workman使用手册1.0

workman官网地址&#xff1a;高性能PHP应用容器 workerman 1&#xff1a;把workman项目放到linux服务器后&#xff0c;需要启动你的php文件&#xff0c;才可以使用 定位到项目根目录&#xff1a;例&#xff1a;cd /mnt/workman 启动代码&#xff1a;php outin.php start -d 停…

关于DBMS_STATS.GATHER_DATABASE_STATS_JOB_PROC的一些发现

任务在哪 这个是11g以后的自动收集统计信息的后台任务&#xff0c;10g之前是在dba_scheduler_jobs里查看 SQL> SELECT CLIENT_NAME ,STATUS ,MEAN_INCOMING_TASKS_7_DAYS,MEAN_INCOMING_TASKS_30_DAYS FROM DBA_AUTOTASK_CLIENT WHERE…

计算机视觉:使用opencv实现车牌识别

1 引言 汽车车牌识别&#xff08;License Plate Recognition&#xff09;是一个日常生活中的普遍应用&#xff0c;特别是在智能交通系统中&#xff0c;汽车牌照识别发挥了巨大的作用。汽车牌照的自动识别技术是把处理图像的方法与计算机的软件技术相连接在一起&#xff0c;以准…

Flutter 中在单个屏幕上实现多个列表

今天&#xff0c;我将提供一个实际的示例&#xff0c;演示如何在单个页面上实现多个列表&#xff0c;这些列表可以水平排列、网格格式、垂直排列&#xff0c;甚至是这些常用布局的组合。 下面是要做的&#xff1a; 实现 让我们从创建一个包含产品所有属性的产品模型开始。 …

ZJU Beamer学习手册(二)

ZJU Beamer学习手册基于 Overleaf 的 ZJU Beamer模板 进行解读&#xff0c;本文则基于该模版进行进一步修改。 参考文献 首先在frame文件夹中增加reference.tex文件&#xff0c;文件内容如下。这段代码对参考文献的引用进行了预处理。 \usepackage[backendbiber]{biblatex} \…

【机器学习】划分训练集和测试集的方法

在机器学习中&#xff0c;我们的模型建立完成后&#xff0c;通常要根据评估指标来对模型进行评估&#xff0c;以此来判断模型的可用性。而评估指标主要的目的是让模型在未知数据上的预测能力最好。因此&#xff0c;我们在模型训练之前&#xff0c;要对训练集和测试集进行划分。…

【Python数据结构与算法】——(线性结构)精选好题分享,不挂科必看系列

&#x1f308;个人主页: Aileen_0v0&#x1f525;系列专栏:<<Python数据结构与算法专栏>>&#x1f4ab;个人格言:"没有罗马,那就自己创造罗马~" 时间复杂度大小比较 1.time complexity of algorithm A is O(n^3) while algorithm B is O(2^n). Which o…

股东入股可用的出资形式主要有哪些

股东入股&#xff0c;可用的出资形式主要包括货币以及实物、知识产权、土地使用权等可以用货币估价并可以依法转让的非货币财产。 第一&#xff0c;货币。设立公司必然需要一定数量的流动资金。以支付创建公司时的开支和启动公司运营。因此&#xff0c;股东可以用货币出资。 第…

自学嵌入式,已经会用stm32做各种小东西了

自学嵌入式&#xff0c;已经会用stm32做各种小东西了 1、stm32 工程中&#xff0c;定义一个变量&#xff0c;记录复位次数&#xff0c;即复位一次变量加一。要求不许用备份寄存器和 flash 保存信息。本题只讨论不断电热启动情况&#xff0c;至于冷启动&#xff0c;不在此讨论。…

【MATLAB源码-第80期】基于蚯蚓优化算法(EOA)的无人机三维路径规划,输出做短路径图和适应度曲线

操作环境&#xff1a; MATLAB 2022a 1、算法描述 蚯蚓优化算法&#xff08;Earthworm Optimisation Algorithm, EOA&#xff09;是一种启发式算法&#xff0c;灵感来源于蚯蚓在自然界中的行为模式。蚯蚓优化算法主要模仿了蚯蚓在寻找食物和逃避天敌时的行为策略。以下是蚯蚓…

【论文阅读】基于隐蔽带宽的汽车控制网络鲁棒认证(二)

文章目录 第三章 识别CAN中的隐藏带宽信道3.1 隐蔽带宽vs.隐藏带宽3.1.1 隐蔽通道3.1.2 隐藏带宽通道 3.2 通道属性3.3 CAN隐藏带宽信道3.3.1 CAN帧ID字段3.3.2 CAN帧数据字段3.3.3 帧错误检测领域3.3.4 时间通道3.3.5 混合通道 3.4 构建信道带宽公式3.5通道矩阵3.6 结论 第四章…

视频集中存储/云存储平台EasyCVR级联下级平台的详细步骤

安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快&#xff0c;可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等&#xff0c;以及支持厂家私有协议与SDK接入&#xff0c;包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安…

前台页面从数据库中获取下拉框值

后端&#xff1a;查询所有信息 前台&#xff1a;elementUI <el-select v-model"searchData.stationName" clearable> <el-option :label"item.stationName" :value"item.stationName" v-for"item in stationNameList&quo…

GoLong的学习之路,进阶,标准库之并发(context)补充并发三部曲,你真的明白context吗?

其实对于&#xff0c;context来说&#xff0c;如果只是用来做并发处理就有些不太合适。因为对于golang来说&#xff0c;context应用场景不仅在并发有用&#xff0c;并且在网络链接&#xff0c;http处理&#xff0c;gorm中都有体现。但是其实&#xff0c;本质来说。以上这些场景…

【Java 进阶篇】JQuery 事件绑定:`on` 与 `off` 的奇妙舞曲

在前端开发的舞台上&#xff0c;用户与页面的互动是一场精彩的表演。而 JQuery&#xff0c;作为 JavaScript 的一种封装库&#xff0c;为这场表演提供了更为便捷和优雅的事件绑定方式。其中&#xff0c;on 和 off 两位主角&#xff0c;正是这场奇妙舞曲中的核心演员。在这篇博客…

Flask 接口

目录 前言 代码实现 简单接口实现 执行其它程序接口 携带参数访问接口 前言 有时候会想着开个一个接口来访问试试&#xff0c;这里就给出一个基础接口代码示例 代码实现 导入Flask模块&#xff0c;没安装Flask 模块需要进行 安装&#xff1a;pip install flask 使用镜…

Redis数据的持久化

Redis的持久化有两种方式&#xff1a; RDB&#xff08;Redis Database&#xff09;和AOF&#xff08;Append Only File&#xff09; 目录 一、RDB 保存方式 2、rdb在redis.conf文件中的配置 二、AOF 1、保存方式 2、aof方式持久化在redis.conf文件中的配置 三、持久化建…

【迅搜01】安装运行并测试XunSearch

安装运行并测试XunSearch 这回的新系列&#xff0c;我们将学习到的是一个搜索引擎 迅搜 XunSearch 的使用。这个搜索引擎在 PHP 圈可能还是有一点名气的&#xff0c;而且也是一直在更新的&#xff0c;虽说现在 ElasticSearch 已经是实际上的搜索引擎霸主了&#xff0c;而且还有…

Vue3 shallowRef 和 shallowReactive

一、shallowRef 使用shallowRef之前需要进行引入&#xff1a; import { shallowRef } from vue; 使用方法和ref 的使用方法一致&#xff0c;以下是二者的区别&#xff1a; 1. 如果ref 和 shallowRef 都传入的是普通数据类型的数据&#xff0c;那么他们的效果是一样的&#x…

【计算机基础】优雅的PPT就应该这样设计

&#x1f4e2;&#xff1a;如果你也对机器人、人工智能感兴趣&#xff0c;看来我们志同道合✨ &#x1f4e2;&#xff1a;不妨浏览一下我的博客主页【https://blog.csdn.net/weixin_51244852】 &#x1f4e2;&#xff1a;文章若有幸对你有帮助&#xff0c;可点赞 &#x1f44d;…