idea插件代码生成工具EasyCode

idea插件代码生成工具EasyCode

  • 1. EasyCode下载安装
  • 2. EasyCode配置模板
    • 2.1 配置路径
    • 2.2 配置文件
      • 2.2.1. controller.java
      • 2.2.2. entity.java
      • 2.2.3. mapper.java
      • 2.2.4. service.java
      • 2.2.5. serviceImpl.java
      • 2.2.6. mapper.xml
  • 3. 构建SpringBoot项目
    • 3.1 初始化数据库
    • 3.2 创建项目
    • 3.3 生成代码
    • 3.4 修改配置
      • 3.4.1. 修改application.yml
      • 3.4.2. application-dev.yml
      • 3.4.3. 修改MapperScan路径
      • 3.4.4. 修改pom文件
      • 3.4.5. 添加结果对象
  • 4. 测试验证
    • 4.1 目录结构
    • 4.2. 测试结果

1. EasyCode下载安装

EasyCode是一款根据idea数据库连接自动生成Java代码,从controller,service,dao,mapper一整套的代码的idea插件。

首先下载安装easycode插件,idea分为在线安装与离线安装。
1.在线安装:setting-plugin,搜索easycode,安装重启。

2.离线安装
下载地址:https://gitee.com/makejava/EasyCode/releases
下载压缩包:EasyCode-1.2.3-RELEASE.zip
通过idea离线安装该压缩包。

2. EasyCode配置模板

2.1 配置路径

配置路径:File --> Settings --> Other Settings --> EasyCode – > Template Settings
在这里插入图片描述

2.2 配置文件

2.2.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})s 实例对象的集合
     * @return 影响行数
     */
    @RequestMapping(value = "batchInsert", method = RequestMethod.POST)
    public Response<Integer> batchInsert(@RequestBody List<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})s) {
        int result = $!{tool.firstLowerCase($tableInfo.name)}Service.batchInsert($!tool.firstLowerCase($tableInfo.name)s);
        if (result > 0) {
           return Response.createSuccessResponse("新增成功", result);
        }
        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("查询失败");
    }
    
    /**
     * 通过实体作为筛选条件查询
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectList", method = RequestMethod.GET)
    public Response<List<$tableInfo.name>> selectList($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        List<$tableInfo.name> $!tool.firstLowerCase($tableInfo.name)s = $!{tool.firstLowerCase($tableInfo.name)}Service.selectList($!tool.firstLowerCase($!{tableInfo.name}));
        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.2.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        
'}';
    }
}

2.2.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();
}

2.2.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();
}

2.2.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();
     }
}

2.2.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>

3. 构建SpringBoot项目

3.1 初始化数据库

-- 用户测试
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;

3.2 创建项目

在这里插入图片描述在这里插入图片描述在这里插入图片描述

3.3 生成代码

在这里插入图片描述

3.4 修改配置

3.4.1. 修改application.yml

spring:
  profiles:
    active: dev

3.4.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.4.3. 修改MapperScan路径

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 );
    }

}

3.4.4. 修改pom文件


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.0version>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>categoryartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>categoryname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.4version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

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

project>

3.4.5. 添加结果对象

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. 测试验证

4.1 目录结构

在这里插入图片描述

4.2. 测试结果

idea插件代码生成工具EasyCode_第1张图片

你可能感兴趣的:(基础框架,intellij-idea,java,数据库)