EasyCode template configuration

EasyCode template configuration

1. Template configuration

1.1 Configuration path

File --> Settings --> Other Settings --> EasyCode – > Template Settings

Insert picture description here

1.2 Configuration file

1. controller.java

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Controller"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/controller"))
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}controller;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.web.bind.annotation.*;
import $!{tableInfo.savePackageName}.entity.Response;
import java.util.List;

import javax.annotation.Resource;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})控制层
 *
 * @author $!author
 * @since $!time.currTime()
 */
@RestController
@RequestMapping("/$!tool.firstLowerCase($tableInfo.name)")
public class $!{tableName} {
    /**
     * 服务对象
     */
    @Resource
    private $!{tableInfo.name}Service $!tool.firstLowerCase($tableInfo.name)Service;

    /**
     * 通过主键查询单条数据
     *
     * @param $!tool.firstLowerCase($tableInfo.name) 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public Response<$tableInfo.name> selectOne($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
        $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.selectById($!{tool.firstLowerCase($tableInfo.name)}.getId());
        if(result != null){
           return Response.createSuccessResponse("查询成功", result);
        }
        return Response.createErrorResponse("查询失败");
    }
    
    /**
     * 新增一条数据
     *
     * @param $!tool.firstLowerCase($tableInfo.name) 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Response<$tableInfo.name> insert(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
        int result = $!{tool.firstLowerCase($tableInfo.name)}Service.insert($!tool.firstLowerCase($tableInfo.name));
        if (result > 0) {
           return Response.createSuccessResponse("新增成功", $!tool.firstLowerCase($tableInfo.name));
        }
        return Response.createErrorResponse("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param $!tool.firstLowerCase($tableInfo.name) 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public Response<$tableInfo.name> update(@RequestBody $tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
        $tableInfo.name result = $!{tool.firstLowerCase($tableInfo.name)}Service.update($!tool.firstLowerCase($tableInfo.name));
        if (result != null) {
           return Response.createSuccessResponse("修改成功", result);
        }
        return Response.createErrorResponse("修改失败");
    }

    /**
     * 删除一条数据
     *
     * @param $!tool.firstLowerCase($tableInfo.name) 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public Response<$tableInfo.name> delete($tableInfo.name $!tool.firstLowerCase($tableInfo.name)) {
        int result = $!{tool.firstLowerCase($tableInfo.name)}Service.deleteById($!{tool.firstLowerCase($tableInfo.name)}.getId());
        if (result > 0) {
           return Response.createSuccessResponse("删除成功", null);
        }
        return Response.createErrorResponse("删除失败");
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public Response<List<$tableInfo.name>> selectAll() {
        List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}Service.selectAll();
        if ($!tool.firstLowerCase($tableInfo.name)s != null) {
           return Response.createSuccessResponse("查询成功", $!tool.firstLowerCase($tableInfo.name)s);
        }
        return Response.createErrorResponse("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public Response<List<$tableInfo.name>> selectPage(Integer start, Integer limit) {
        List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}Service.selectPage(start, limit);
        if ($!tool.firstLowerCase($tableInfo.name)s != null) {
           return Response.createSuccessResponse("查询成功", $!tool.firstLowerCase($tableInfo.name)s);
        }
        return Response.createErrorResponse("查询失败");
    }
    
}

2. entity.java

##引入宏定义
$!define

##使用宏定义设置回调(保存位置与文件后缀)
#save("/entity", ".java")

##使用宏定义设置包后缀
#setPackageSuffix("entity")

##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;

##使用宏定义实现类注释信息
#tableComment("实体类")
public class $!{tableInfo.name} implements Serializable {
    private static final long serialVersionUID = $!tool.serial();
#foreach($column in $tableInfo.fullColumn)
    #if(${column.comment})/**
    * ${column.comment}
    */#end

    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

#foreach($column in $tableInfo.fullColumn)
##使用宏定义实现get,set方法
    #getSetMethod($column)
#end

    @Override
    public String toString(){
        return "$tableInfo.name {" +
        #foreach($column in $tableInfo.fullColumn)
    "$column.name : " + $column.name + ", " +
        #end        
'}';
    }
}

3. mapper.java

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Mapper"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/mapper"))

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}mapper;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表数据库访问层
 *
 * @author $!author
 * @since $!time.currTime()
 */
public interface $!{tableName} {

    /**
     * 通过ID查询单条数据
     *
     * @param $!pk.name 主键
     * @return 实例对象
     */
    $!{tableInfo.name} selectById($!pk.shortType $!pk.name);
	
    /**
     * 分页查询
     *
     * @param start 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<$!{tableInfo.name}> selectPage(@Param("start") int start, @Param("limit") int limit);

    /**
     * 查询全部
     *
     * @return 对象列表
     */
    List<$!{tableInfo.name}> selectAll();
    
    /**
     * 通过实体作为筛选条件查询
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 对象列表
     */
    List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
	
	/**
     * 批量新增
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合
     * @return 影响行数
     */
	int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s);
	
    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 影响行数
     */
    int deleteById($!pk.shortType $!pk.name);

    /**
     * 查询总数据数
     *
     * @return 数据总数
     */
    int count();
}

4. service.java

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "Service"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service"))

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import java.util.List;
import java.util.Map;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表服务接口
 *
 * @author $!author
 * @since $!time.currTime()
 */
public interface $!{tableName} {

    /**
     * 通过ID查询单条数据
     *
     * @param $!pk.name 主键
     * @return 实例对象
     */
    $!{tableInfo.name} selectById($!pk.shortType $!pk.name);

    /**
     * 分页查询
     *
     * @param start 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<$!{tableInfo.name}> selectPage(int start, int limit);

    /**
     * 查询全部
     *
     * @return 对象列表
     */
    List<$!{tableInfo.name}> selectAll();
    
    /**
     * 通过实体作为筛选条件查询
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 对象列表
     */
    List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 影响行数
     */
    int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));
	
	/**
     * 批量新增
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合
     * @return 影响行数
     */
	int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s);
	
    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 修改
     */
    $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 影响行数
     */
    int deleteById($!pk.shortType $!pk.name);
    
    /**
     * 查询总数据数
     *
     * @return 数据总数
     */
    int count();
}

5. serviceImpl.java

##定义初始变量
#set($tableName = $tool.append($tableInfo.name, "ServiceImpl"))
##设置回调
$!callback.setFileName($tool.append($tableName, ".java"))
$!callback.setSavePath($tool.append($tableInfo.savePath, "/service/impl"))

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service.impl;

import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name};
import $!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper;
import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * $!{tableInfo.comment}($!{tableInfo.name}表)服务实现类
 *
 * @author $!author
 * @since $!time.currTime()
 */
@Service("$!tool.firstLowerCase($!{tableInfo.name})Service")
public class $!{tableName} implements $!{tableInfo.name}Service {
    @Resource
    private $!{tableInfo.name}Mapper $!tool.firstLowerCase($!{tableInfo.name})Mapper;

    /**
     * 通过ID查询单条数据
     *
     * @param $!pk.name 主键
     * @return 实例对象
     */
    @Override
    public $!{tableInfo.name} selectById($!pk.shortType $!pk.name) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectById($!pk.name);
    }

    /**
     * 分页查询
     *
     * @param start 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<$!{tableInfo.name}> selectPage(int start, int limit) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectPage(start, limit);
    }

    /**
     * 查询所有
     *
     * @return 实例对象的集合
     */
     @Override
     public List<$!{tableInfo.name}> selectAll() {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectAll();
     }
     
    /**
     * 根据条件查询
     *
     * @return 实例对象的集合
     */
    @Override
    public List<$!{tableInfo.name}> selectList($!{tableInfo.name} $!{tool.firstLowerCase($!{tableInfo.name})}) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.selectList($!{tool.firstLowerCase($!{tableInfo.name})});
    }
    
    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    @Override
    public int insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.insert($!tool.firstLowerCase($!{tableInfo.name}));
    }

    /**
     * 批量新增
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name})s 实例对象的集合
     * @return 生效的条数
     */
    @Override
    public int batchInsert(List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.batchInsert($!tool.firstLowerCase($!{tableInfo.name})s);
    }

    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    @Override
    public $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.update($!tool.firstLowerCase($!{tableInfo.name}));
        return this.selectById($!{tool.firstLowerCase($!{tableInfo.name})}.get$!tool.firstUpperCase($pk.name)());
    }

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 是否成功
     */
    @Override
    public int deleteById($!pk.shortType $!pk.name) {
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.deleteById($!pk.name);
    }
    
    /**
     * 查询总数据数
     *
     * @return 数据总数
     */
     @Override
     public int count(){
        return this.$!{tool.firstLowerCase($!{tableInfo.name})}Mapper.count();
     }
}

6. mapper.xml

##引入mybatis支持
$!mybatisSupport

##设置保存名称与保存位置
$!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml"))
$!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper"))

##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
    #set($pk = $tableInfo.pkColumn.get(0))
#end

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper">
    <!-- 结果集 -->
    <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}Map">
#foreach($column in $tableInfo.fullColumn)
        <result property="$!column.name" column="$!column.obj.name" jdbcType="$!column.ext.jdbcType"/>
#end
    </resultMap>
    
    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        #allSqlColumn()
    </sql>
    
    <!-- 查询单个 -->
    <select id="selectById" resultMap="$!{tableInfo.name}Map">
        select
          <include refid="Base_Column_List" />
        from $!tableInfo.obj.name
        where $!pk.obj.name = #{$!pk.name}
    </select>

    <!-- 分页查询 -->
    <select id="selectPage" resultMap="$!{tableInfo.name}Map">
        select
        <include refid="Base_Column_List" />
        from $!tableInfo.obj.name
        limit #{start},#{limit}
    </select>

    <!-- 查询全部 -->
    <select id="selectAll" resultMap="$!{tableInfo.name}Map">
        select
        <include refid="Base_Column_List" />
        from $!tableInfo.obj.name
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultMap="$!{tableInfo.name}Map">
        select
        <include refid="Base_Column_List" />
        from $!tableInfo.obj.name
        <where>
        #foreach($column in $tableInfo.fullColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                and $!column.obj.name = #{$!column.name}
            </if>
        #end
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="$!pk.name" useGeneratedKeys="true">
        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
        values ( #foreach($column in $tableInfo.fullColumn)#{$!{column.name}}#if($velocityHasNext), #end#end)
    </insert>
    
    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into $!{tableInfo.obj.name}(#foreach($column in $tableInfo.fullColumn)$!column.obj.name#if($velocityHasNext), #end#end)
        values 
        <foreach collection="$!tool.firstLowerCase($!{tableInfo.name})s" item="item" index="index" separator=",">
        (
            #foreach($column in $tableInfo.fullColumn)
            #{item.$!{column.name}}#if($velocityHasNext), #end
#end
         )
         </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update $!{tableInfo.obj.parent.name}.$!{tableInfo.obj.name}
        <set>
        #foreach($column in $tableInfo.otherColumn)
            <if test="$!column.name != null#if($column.type.equals("java.lang.String")) and $!column.name != ''#end">
                $!column.obj.name = #{$!column.name},
            </if>
        #end
        </set>
        where $!pk.obj.name = #{$!pk.name}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from $!{tableInfo.obj.name} where $!pk.obj.name = #{$!pk.name}
    </delete>
    
    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*) from $!{tableInfo.obj.name}
    </select>
</mapper>

2. Build the project

2.1 Initialize the database

-- 用户测试
CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `passWord` varchar(50) NOT NULL,
  `realName` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `category`.`user`(`id`, `userName`, `passWord`, `realName`) VALUES (3, '朱荣靖', '123', 'Jess');
INSERT INTO `category`.`user`(`id`, `userName`, `passWord`, `realName`) VALUES (4, '王小二', '456', 'wang');

select * from user;

2.2 Create a project

Insert picture description here
Insert picture description here
Insert picture description here

2.3 Generate code

Insert picture description here

3. Configuration file

1. application.yml

spring:
  profiles:
    active: dev

2. application-dev.yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/category?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.example.category

#showSql
logging:
  level:
    com:
      example:
        mapper : debug

3. CategoryApplication

package com.example.category;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.category.mapper")
public class CategoryApplication {

    public static void main(String[] args) {
        SpringApplication.run( CategoryApplication.class, args );
    }

}

4. pom file

<?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>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>category</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>category</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5. Result Object

Response

package com.example.category.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zrj
 * @date 2021/6/2
 * @since V1.0
 **/
@Component
public class Response<T> {

    private static ResponseCode responseCode;
    /**
     * 提示消息
     */
    private String message;

    /**
     * 具体返回的数据
     */
    private T data;

    /**
     * 状态码
     */
    private String code;

    private Response(String code, String message, T data) {
        this.message = message;
        this.code = code;
        this.data = data;
    }

    private Response(String code, String msg) {
        this.message = msg;
        this.code = code;
    }

    @Autowired
    public Response(ResponseCode responseCode) {
        Response.responseCode = responseCode;
    }

    /**
     * 返回成功Response对象
     *
     * @param successMessage 成功提示信息
     * @param data           需要返回的数据
     * @return 成功信息
     */
    public static <T> Response<T> createSuccessResponse(String successMessage, T data) {
        return new Response<>( responseCode.getSuccessCode(), successMessage, data );
    }


    /**
     * 返回错误Response对象
     *
     * @param errorMessage 错误信息
     * @return 错误信息
     */
    public static <T> Response<T> createErrorResponse(String errorMessage) {
        return new Response<>( responseCode.getErrorCode(), errorMessage );
    }

    public Response() {
    }

    /**
     * 返回未登录状态码
     *
     * @param message 提示信息
     * @return Response
     */
    public static <T> Response<T> createUnLoginResponse(String message) {
        return new Response<>( responseCode.getAuthErrorCode(), message );
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

}

ResponseCode

package com.example.category.entity;

import org.springframework.stereotype.Component;

/**
 * @author zrj
 * @date 2021/6/2
 * @since V1.0
 **/
@Component
public class ResponseCode {
    private String successCode = "200";

    private String errorCode = "500";

    private String authErrorCode = "300";

    public String getSuccessCode() {
        return successCode;
    }

    public void setSuccessCode(String successCode) {
        this.successCode = successCode;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getAuthErrorCode() {
        return authErrorCode;
    }

    public void setAuthErrorCode(String authErrorCode) {
        this.authErrorCode = authErrorCode;
    }
}

4. Directory structure

Insert picture description here

5. Test results

Insert picture description here