【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.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot_vue?serverTimezone=UTC
    username: root
    password: Miami
  main:
    banner-mode: off  # 关闭控制台springboot的logo


mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true # 在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # 控制台显示sql
    default-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)这里因为我是反的所以改成0
      logic-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.com
    accessKeyId: LTjfk332422slksPqy
    accessKeySecret: Pfslfksd2;lf2sALHfdsfsTHm6fdsfsdsR
    bucketName: 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;

    /**
     * 文章内容
     */
    @NotEmpty
    private String content;

    /**
     * 文章封面
     */
    @NotEmpty
    @URL
    private String coverImg;

    /**
     * 文章状态: 只能是[已发布] 或者 [草稿]
     */
    @State  //自定义注解
    private String state;

    /**
     * 文章分类ID
     */
    @NotNull
    private 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;
    @Autowired
    private 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("."));

        //上传文件到 OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, fileName, inputStream);

        //文件访问路径
        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        // 关闭ossClient
        ossClient.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 --------> 阿里巴巴fastJSON
            String 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 --------> 阿里巴巴fastJSON
            String 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 {

    @Autowired
    private LoginCheckInterceptor loginCheckInterceptor;

    /**
     * 注册拦截器, 拦截所有url,除登录
     * @param registry
     */
    @Override
    public 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 {

    @Bean
    public 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,则校验通过
     */
    @Override
    public 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 {

    @Autowired
    private 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{

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private 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{

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private 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{

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private 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 {

    @Autowired
    private 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{

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private 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{

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }

    /**
     * 更新头像
     * @param avatarUrl
     */
    @Override
    public 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 {

    @Autowired
    private 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 、调用userService
        userService.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{

    @Autowired
    private UserMapper userMapper;

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public void updateUser(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
    }

    /**
     * 更新头像
     * @param avatarUrl
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * @param category
     * @return
     */
    @PostMapping
    public 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{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     * @param category
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * @param category
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     * @param category
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * @param category
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     * @param category
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * @param category
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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
     */
    @PutMapping
    public 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{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     * @param category
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public Category queryCategoryById(Integer id) {

        return  categoryMapper.selectById(id);
    }

    /**
     * 根据id修改文章分类
     * @param category
     */
    @Override
    public 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 {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加文章分类
     * @param category
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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
     */
    @PutMapping
    public 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
     */
    @DeleteMapping
    public 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{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 新增文章分类
     * @param category
     */
    @Override
    public 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);
    }

    @Override
    public Category queryCategoryByCategoryName(String categoryName) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(categoryName != null, Category::getCategoryName,categoryName);
        Category category = categoryMapper.selectOne(lambdaQueryWrapper);
        return category;
    }

    /**
     * 列表查询当前登录用户创建的文章分类
     * @return
     */
    @Override
    public 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
     */
    @Override
    public Category queryCategoryById(Integer id) {

        return  categoryMapper.selectById(id);
    }

    /**
     * 根据id修改文章分类
     * @param category
     */
    @Override
    public void updateCategoryById(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateById(category);

    }

    /**
     * 根据id删除文章分类
     * @param id
     */
    @Override
    public 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 {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public 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{


    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 新增文章
     * @param article
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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{


    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 新增文章
     * @param article
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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{


    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 新增文章
     * @param article
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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 {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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
     */
    @PutMapping
    public 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{


    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 新增文章
     * @param article
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public Article detail(Integer id) {
        return articleMapper.selectById(id);
    }

    /**
     * 根据id更新文章
     * @param article
     */
    @Override
    public 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 {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public 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
     */
    @GetMapping
    public 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
     */
    @PutMapping
    public 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
     */
    @DeleteMapping
    public 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{


    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 新增文章
     * @param article
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public 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
     */
    @Override
    public Article detail(Integer id) {
        return articleMapper.selectById(id);
    }

    /**
     * 根据id更新文章
     * @param article
     */
    @Override
    public void updateArticleById(Article article) {
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.updateById(article);
    }

    /**
     * 根据id删除文章
     * @param id
     */
    @Override
    public 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 {

    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        log.info("上传的文件名:{}",file.getOriginalFilename());
        String url = aliOSSUtils.upload(file);

        return Result.success(url);
    }

}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

使用亚马逊鲲鹏系统有什么好处?

亚马逊鲲鹏系统是一款能绕过亚马逊智能检测&#xff0c;完全模拟人类真实行为&#xff0c;通过模拟真实的人流量来帮助你提升你的产品排名&#xff0c;让你的产品出现在搜索首页&#xff0c;从而快速帮助提高销售业绩的营销工具&#xff01; 好处1&#xff1a;自动化操作更节约…

Fabric多机部署启动节点与合约部署

这是我搭建的fabric的网络拓扑 3 个 orderer 节点&#xff1b;组织 org1 , org1 下有两个 peer 节点&#xff0c; peer0 和 peer1; 组织 org2 , org2 下有两个 peer 节点&#xff0c; peer0 和 peer1; 以上是我的多机环境的网络拓扑&#xff0c;使用的是docker搭建的。我的网络…

什么是数据泄露?泄露途径有哪些?企业如何免遭数据泄露?

数据泄露指将机密信息、私人信息或其他敏感信息发布到不安全的环境中。数据泄露可能由意外引起&#xff0c;也可能是蓄意攻击的结果。 每年都有数百万人卷入数据泄露&#xff0c;包括意外看错病人图表的医生&#xff0c;以及大规模尝试访问政府计算机以发现敏感信息。 因为敏…

向量矩阵范数pytorch

向量矩阵范数pytorch 矩阵按照某个维度求和&#xff08;dim就是shape数组的下标&#xff09;1. torch1.1 Tensors一些常用函数 一些安装问题cd进不去不去目录PyTorch里面_表示重写内容 在默认情况下&#xff0c;PyTorch会累积梯度&#xff0c;我们需要清除之前的值 范数是向量或…

企业级真实应用利用Mybatis-Plus进行分页查询处理

怎么导入依赖我在之前的文章里边有说过不理解的可以看看 你应该懂点Mybatis-plus&#xff0c;真的好用 1&#xff1a;了解Page<T>类的使用 首先我们需要使用到Page类 &#xff0c;建立一个Page类&#xff0c;泛式类型中放入我们需要输出的类&#xff0c;是列表的话就…

分享5款好用到爆的神仙软件

​ 最近陆陆续续收到好多小伙伴的咨询&#xff0c;这边也是抓紧时间整理出几个好用的软件&#xff0c;希望可以帮到大家。 1.全局鼠标手势——MouseInc ​ MouseInc是一款由shuax制作的全局鼠标手势软件&#xff0c;还支持很多增强辅助功能&#xff0c;如屏幕取色、窗口管理、…

前端学习笔记--Event-loop

定义 Event Loop&#xff1a;即事件循环&#xff0c;是指浏览器或Node的一种解决javaScript单线程运行时不会阻塞的一种机制&#xff0c;也就是我们经常使用异步的原理。 **进程&#xff1a;**进程是计算机中的程序关于某数据集合上的一次运行活动&#xff0c;是系统进行资源分…

vmware安装MacOS以及flutter遇到的问题

安装过程&#xff1a;参考下面的文章 链接&#xff1a; 虚拟机VMware安装苹果系统macOS&#xff0c;超级详细教程&#xff0c;附文件下载&#xff0c;真教程&#xff01;&#xff01; 无限重启情况&#xff1a; &#xff08;二&#xff09; 配置虚拟机找到你的虚拟机安装文件…

查询站点真实IP地址,绕过CDN

一.如何判断站点是否使用了CDN&#xff1f; 使用其他省市的电脑进行ping看返回的IP地址是否相同通过第三方网站查询 站长工具 3.nslookup命令 二. 如何绕过CDN获取真实IP 子域名查询&#xff0c;因为很多站点只对主域名进行了CDN加速网站邮件头信息微步在线DNS查询

[PyTorch][chapter 63][强化学习-QLearning]

前言&#xff1a; 这里结合走迷宫的例子,重点学习一下QLearning迭代更新算法 0,1,2,3,4 是房间&#xff0c;之间绿色的是代表可以走过去。 5为出口 可以用下图表示 目录&#xff1a; 策略评估 策略改进 迭代算法 走迷宫实现Python 一 策略评估 强化学习最终是为了…

算法通关村——数组中第K大的数字

数组中第K大的数字 1、题目描述 ​ LeetCode215. 数组中的第K个最大元素。给定整数数组nums和整数k&#xff0c;请返回数组中第k个最大的元素。请注意&#xff0c;你需要找的是数组排序后的第k个最大的元素&#xff0c;而不是第k个不同的元素。 示例1&#xff1a; 输入&#…

LLM prompt提示工程调试方法经验技巧汇总

现在接到一个LLM模型任务&#xff0c;第一反应就是能不能通过精调prompt来实现&#xff0c;因为使用prompt不需要训练模型&#xff0c;只需输入指令就可以实现和LLM的交互。按照以往经验&#xff0c;不同的prompt对模型输出影响非常大&#xff0c;如果能构造一个好的prompt&…

【23真题】厉害,这套竟有150分满分!

今天分享的是23年中国海洋大学946的信号与系统试题及解析。 本套试卷难度分析&#xff1a;22年中国海洋大学946考研真题&#xff0c;我也发布过&#xff0c;若有需要&#xff0c;戳这里自取!平均分为109-120分&#xff0c;最高分为150分满分&#xff01;本套试题内容难度中等&…

【vue】 实现 自定义 Calendar 日历

图例&#xff1a;自定义日历 一、标签自定义处理 <div class"date-box"><el-calendar v-model"state.currDate" ref"calendar"><template #header"{ date }"><div class"date-head flex"><div …

Golang获取月份的第一天和最后一天

package mainimport ("fmt""strconv""strings""time" )func main() {month : "2023-11"result : GetMonthStartAndEnd(month)fmt.Println(result["start"] " - " result["end"]) }// 获取月…

图形化探索:快速改造单实例为双主、MGR、读写分离等架

单机GreatSQL/MySQL调整架构为多副本复制的好处有哪些&#xff1f;为什么要调整&#xff1f; 性能优化&#xff1a;如果单个GreatSQL服务器的处理能力达到瓶颈&#xff0c;可能需要通过主从复制、双主复制或MGR&#xff0c;以及其他高可用方案等来提高整体性能。通过将读请求分…

zabbix的服务器端 server端安装部署

zabbix的服务器端 server 主机iplocalhost&#xff08;centos 7&#xff09;192.168.10.128 zabbix官网部署教程 但是不全&#xff0c;建议搭配这篇文章一起看 zabbixAgent部署 安装mysql 所有配置信息和Zabbix收集到的数据都被存储在数据库中。 下载对应的yum源 yum ins…

【Linux】非堵塞轮询

堵塞轮询&#xff1a; 堵塞轮询是我们最简单的一种等待方式也是最常应用的等待方式。 但是&#xff0c;一旦阻塞等待也就意味着我们当前在进行等待的时候&#xff0c;父进程什么都干不了。 非堵塞轮询&#xff1a; 其中非阻塞等待&#xff0c;是等待的一种模式&#xff0c; 在…

如何使用Imagewheel+内网穿透搭建私人图床实现公网访问

文章目录 1.前言2. Imagewheel网站搭建2.1. Imagewheel下载和安装2.2. Imagewheel网页测试2.3.cpolar的安装和注册 3.本地网页发布3.1.Cpolar临时数据隧道3.2.Cpolar稳定隧道&#xff08;云端设置&#xff09;3.3.Cpolar稳定隧道&#xff08;本地设置&#xff09; 4.公网访问测…

【淘宝API】商品详情+搜索商品列表接口

淘宝商品详情API接口可以使用淘宝开放平台提供的SDK或API来获取。这些接口可以用于获取商品的详细信息&#xff0c;如标题、价格、描述、图片等。 以下是使用淘宝开放平台API获取商品详情的步骤&#xff1a; 注册淘宝开放平台账号&#xff0c;并创建应用&#xff0c;获取应用…