EasyCode 模板代码重构

1、问题描述

距离之前写EasyCode+通用Mapper+封装Spring Boot+Swagger脚手架详细教程已经过去一段时间了,发现有人也转发了我的文章。最近呢发现模板代码可以更简单一点。也就进行了重构,过程就不分析了,可以参考之前的文章。重构之后的模板代码如下:

2、问题解决

entity.java

##引入宏定义
$!define

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

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

##使用全局变量实现默认包导入
$!autoImport
import java.io.Serializable;
import io.swagger.annotations.*;
import lombok.Data;
import javax.persistence.*;

##使用宏定义实现类注释信息
#tableComment("实体类")
@Data
@ApiModel("$tableInfo.comment")
public class $!{tableInfo.name} implements Serializable {
    private static final long serialVersionUID = $!tool.serial();

#foreach($column in $tableInfo.pkColumn )
    #if(${column.comment})/**
    * ${column.comment}
    */
    #end
@Id
    private $!{tool.getClsNameByFullName($column.type)} $!{column.name};
#end

#foreach($column in $tableInfo.otherColumn )
    #if(${column.comment})/**
    * ${column.comment}
    */
    #end
private $!{tool.getClsNameByFullName($column.type)} $!{column.name};

#end

}

IBaseMapper.java

##设置回调
$!callback.setFileName("IBaseMapper.java")
$!callback.setSavePath($tool.append($tableInfo.savePath, "/base/mapper"))
##使用宏定义设置回调(保存位置与文件后缀)
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}base.mapper;


import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;


/**
 * $!{tableInfo.comment}($!{tableInfo.name})抽象表数据库访问层
 *
 * @author $!author
 * @since $!time.currTime()
 */
public interface IBaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
}

dao.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 $!{tableInfo.savePackageName}.base.mapper.IBaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;


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

}

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;

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

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

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<$!{tableInfo.name}> queryAllByLimit(Integer offset, Integer limit);

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    List<$!{tableInfo.name}> queryAll();

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

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    /**
     * 修改数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    $!{tableInfo.name} update($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}));

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 是否成功
     */
    boolean deleteById($!pk.shortType $!pk.name);

}

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}.base.service.BaseService;
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;

/**
 * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类
 *
 * @author $!author
 * @since $!time.currTime()
 */
@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} queryById($!pk.shortType $!pk.name) {
        return BaseService.queryById($!pk.name, $!{tool.firstLowerCase($!{tableInfo.name})}Mapper);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<$!{tableInfo.name}> queryAllByLimit(Integer offset, Integer limit) {
        return BaseService.queryAllByLimit(offset, limit,new $!{tableInfo.name}(),$!{tool.firstLowerCase($!{tableInfo.name})}Mapper);
    }

    @Override
    public List<$!{tableInfo.name}> queryAll() {
        return BaseService.queryAll($!{tool.firstLowerCase($!{tableInfo.name})}Mapper);
    }

    @Override
    public List<$!{tableInfo.name}> queryAllByCondition($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return BaseService.queryAllByCondition($!tool.firstLowerCase($!{tableInfo.name}), $!{tool.firstLowerCase($!{tableInfo.name})}Mapper);
    }

    /**
     * 新增数据
     *
     * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象
     * @return 实例对象
     */
    @Override
    public $!{tableInfo.name} insert($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) {
        return BaseService.insert($!tool.firstLowerCase($!{tableInfo.name}),$!{tool.firstLowerCase($!{tableInfo.name})}Mapper);
    }

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

    /**
     * 通过主键删除数据
     *
     * @param $!pk.name 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById($!pk.shortType $!pk.name) {
        return BaseService.deleteById($!pk.name, $!{tool.firstLowerCase($!{tableInfo.name})}Mapper);
    }
}

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 $!{tableInfo.savePackageName}.http.HttpResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

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


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询 $!{tableInfo.comment}")
    @GetMapping("queryById")
    public HttpResult queryById($!pk.shortType id) {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.queryById(id));
    }

    @PostMapping(value = "/insert")
    public HttpResult insert(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.insert($!{tool.firstLowerCase($tableInfo.name)}));
    }

    @GetMapping(value = "/findWithPage")
    public HttpResult findWithPage(@RequestParam(value = "startPage", required = false) Integer startPage,
                               @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.queryAllByLimit(startPage, pageSize));
    }

    @GetMapping(value = "/findAll")
    public HttpResult findAll() {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.queryAll());
    }

    @GetMapping(value = "/findAllByCondition")
    public HttpResult findAllByCondition(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.queryAllByCondition($!{tool.firstLowerCase($tableInfo.name)}));
    }


    @PutMapping(value = "/update")
    public HttpResult update(@RequestBody $!{tableInfo.name} $!{tool.firstLowerCase($tableInfo.name)}) {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.update($!{tool.firstLowerCase($tableInfo.name)}));
    }

    @DeleteMapping(value = "/deleteById")
    public HttpResult deleteById(@RequestParam $!pk.shortType id) {
        return HttpResult.ok($!{tool.firstLowerCase($tableInfo.name)}Service.deleteById(id));
    }

}


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>

</mapper>

chenApplication.java

##设置回调
$!callback.setFileName("ChenApplication.java")
$!callback.setSavePath($tableInfo.savePath)

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

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * 启动类
 *
 * @author $!author
 * @since $!time.currTime()
 */
@SpringBootApplication
@EnableSwagger2
@MapperScan("$!{tableInfo.savePackageName}.#{end}mapper")
public class ChenApplication {

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

}

baseMapperOP.java

##设置回调
$!callback.setFileName("BaseMapperOP.java")
$!callback.setSavePath($tool.append($tableInfo.savePath, "/base/mapper"))
##使用宏定义设置回调(保存位置与文件后缀)
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}base.mapper;


import $!{tableInfo.savePackageName}.base.mapper.IBaseMapper;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 *  基类
 *
 * @author $!author
 * @since $!time.currTime()
 */
public class BaseMapperOP {
    public static final Integer DEFAULT_PAGE_SIZE = 10;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public static <T> T queryById(Object id, IBaseMapper<T> baseMapper) {
        return baseMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    public static <T> List<T> queryAllByLimit(Integer offset, Integer limit, T t, IBaseMapper<T> baseMapper) {
        //如果不传,则设置成默认值
        if (offset == null) {
            offset = 1;
        }
        if (limit == null) {
            limit = DEFAULT_PAGE_SIZE;
        }

	    if (offset < 1) {
            offset = 1;
        }

        PageHelper.startPage(offset, limit);
        // 告诉系统查询那个类
        Example example = new Example(t.getClass());
        return baseMapper.selectByExample(example);
    }


    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    public static <T> List<T> queryAll(IBaseMapper<T> baseMapper) {
        return baseMapper.selectAll();
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    public static <T> List<T> queryAllByCondition(T t, IBaseMapper<T> baseMapper) {
        return baseMapper.select(t);
    }

    /**
     * 新增数据
     *
     * @param t 实例对象
     * @return 影响行数
     */
    public static <T> int insert(T t, IBaseMapper<T> baseMapper) {
        return baseMapper.insertSelective(t);
    }

    /**
     * 修改数据 根据ID修改
     *
     * @param t 实例对象
     * @return 影响行数
     */
    public static <T> int update(T t, IBaseMapper<T> baseMapper) {
        return baseMapper.updateByPrimaryKeySelective(t);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    public static <T> int deleteById(Object id, IBaseMapper<T> baseMapper) {
        return baseMapper.deleteByPrimaryKey(id);
    }
}

baseService.java

##设置回调
$!callback.setFileName("BaseService.java")
$!callback.setSavePath($tool.append($tableInfo.savePath, "/base/service"))
##使用宏定义设置回调(保存位置与文件后缀)
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}base.service;


import $!{tableInfo.savePackageName}.base.mapper.BaseMapperOP;
import $!{tableInfo.savePackageName}.base.mapper.IBaseMapper;
import java.util.List;

/**
 * 基类服务
 *
 * @author $!author
 * @since $!time.currTime()
 */
public class BaseService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public static <T> T queryById(Object id, IBaseMapper<T> baseMapper) {
        return BaseMapperOP.queryById(id, baseMapper);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    public static <T> List<T> queryAllByLimit(Integer offset, Integer limit, T t, IBaseMapper<T> baseMapper) {
        return BaseMapperOP.queryAllByLimit(offset, limit, t, baseMapper);
    }

    /**
     * 根据条件查询多条数据
     *
     * @return 对象列表
     */
    public static <T> List<T> queryAll(IBaseMapper<T> baseMapper) {
        return BaseMapperOP.queryAll(baseMapper);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @return 对象列表
     */
    public static <T> List<T> queryAllByCondition(T t, IBaseMapper<T> baseMapper) {
        return BaseMapperOP.queryAllByCondition(t, baseMapper);
    }

    /**
     * 新增数据
     *
     * @param t 实例对象
     * @return 实例对象
     */
    public static <T> T insert(T t, IBaseMapper<T> baseMapper) {
        BaseMapperOP.insert(t, baseMapper);
        return t;
    }

    /**
     * 修改数据
     *
     * @param t 实例对象
     * @return 实例对象
     */
    public static <T> T update(T t, IBaseMapper<T> baseMapper) {
        BaseMapperOP.update(t, baseMapper);
        return t;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public static <T> boolean deleteById(Object id, IBaseMapper<T> baseMapper) {
        return BaseMapperOP.deleteById(id, baseMapper) > 0;
    }
}


httpStatus.java

##设置回调
$!callback.setFileName("HttpStatus.java")
$!callback.setSavePath($tool.append($tableInfo.savePath, "/http"))
##使用宏定义设置回调(保存位置与文件后缀)
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}http;



/**
 * Http 状态
 *
 * @author $!author
 * @since $!time.currTime()
 */
public interface HttpStatus {
    // --- 1xx Informational ---

    /** {@code 100 Continue} (HTTP/1.1 - RFC 2616) */
    public static final int SC_CONTINUE = 100;
    /** {@code 101 Switching Protocols} (HTTP/1.1 - RFC 2616)*/
    public static final int SC_SWITCHING_PROTOCOLS = 101;
    /** {@code 102 Processing} (WebDAV - RFC 2518) */
    public static final int SC_PROCESSING = 102;

    // --- 2xx Success ---

    /** {@code 200 OK} (HTTP/1.0 - RFC 1945) */
    public static final int SC_OK = 200;
    /** {@code 201 Created} (HTTP/1.0 - RFC 1945) */
    public static final int SC_CREATED = 201;
    /** {@code 202 Accepted} (HTTP/1.0 - RFC 1945) */
    public static final int SC_ACCEPTED = 202;
    /** {@code 203 Non Authoritative Information} (HTTP/1.1 - RFC 2616) */
    public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;
    /** {@code 204 No Content} (HTTP/1.0 - RFC 1945) */
    public static final int SC_NO_CONTENT = 204;
    /** {@code 205 Reset Content} (HTTP/1.1 - RFC 2616) */
    public static final int SC_RESET_CONTENT = 205;
    /** {@code 206 Partial Content} (HTTP/1.1 - RFC 2616) */
    public static final int SC_PARTIAL_CONTENT = 206;
    /**
     * {@code 207 Multi-Status} (WebDAV - RFC 2518)
     * or
     * {@code 207 Partial Update OK} (HTTP/1.1 - draft-ietf-http-v11-spec-rev-01?)
     */
    public static final int SC_MULTI_STATUS = 207;

    // --- 3xx Redirection ---

    /** {@code 300 Mutliple Choices} (HTTP/1.1 - RFC 2616) */
    public static final int SC_MULTIPLE_CHOICES = 300;
    /** {@code 301 Moved Permanently} (HTTP/1.0 - RFC 1945) */
    public static final int SC_MOVED_PERMANENTLY = 301;
    /** {@code 302 Moved Temporarily} (Sometimes {@code Found}) (HTTP/1.0 - RFC 1945) */
    public static final int SC_MOVED_TEMPORARILY = 302;
    /** {@code 303 See Other} (HTTP/1.1 - RFC 2616) */
    public static final int SC_SEE_OTHER = 303;
    /** {@code 304 Not Modified} (HTTP/1.0 - RFC 1945) */
    public static final int SC_NOT_MODIFIED = 304;
    /** {@code 305 Use Proxy} (HTTP/1.1 - RFC 2616) */
    public static final int SC_USE_PROXY = 305;
    /** {@code 307 Temporary Redirect} (HTTP/1.1 - RFC 2616) */
    public static final int SC_TEMPORARY_REDIRECT = 307;

    // --- 4xx Client Error ---

    /** {@code 400 Bad Request} (HTTP/1.1 - RFC 2616) */
    public static final int SC_BAD_REQUEST = 400;
    /** {@code 401 Unauthorized} (HTTP/1.0 - RFC 1945) */
    public static final int SC_UNAUTHORIZED = 401;
    /** {@code 402 Payment Required} (HTTP/1.1 - RFC 2616) */
    public static final int SC_PAYMENT_REQUIRED = 402;
    /** {@code 403 Forbidden} (HTTP/1.0 - RFC 1945) */
    public static final int SC_FORBIDDEN = 403;
    /** {@code 404 Not Found} (HTTP/1.0 - RFC 1945) */
    public static final int SC_NOT_FOUND = 404;
    /** {@code 405 Method Not Allowed} (HTTP/1.1 - RFC 2616) */
    public static final int SC_METHOD_NOT_ALLOWED = 405;
    /** {@code 406 Not Acceptable} (HTTP/1.1 - RFC 2616) */
    public static final int SC_NOT_ACCEPTABLE = 406;
    /** {@code 407 Proxy Authentication Required} (HTTP/1.1 - RFC 2616)*/
    public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;
    /** {@code 408 Request Timeout} (HTTP/1.1 - RFC 2616) */
    public static final int SC_REQUEST_TIMEOUT = 408;
    /** {@code 409 Conflict} (HTTP/1.1 - RFC 2616) */
    public static final int SC_CONFLICT = 409;
    /** {@code 410 Gone} (HTTP/1.1 - RFC 2616) */
    public static final int SC_GONE = 410;
    /** {@code 411 Length Required} (HTTP/1.1 - RFC 2616) */
    public static final int SC_LENGTH_REQUIRED = 411;
    /** {@code 412 Precondition Failed} (HTTP/1.1 - RFC 2616) */
    public static final int SC_PRECONDITION_FAILED = 412;
    /** {@code 413 Request Entity Too Large} (HTTP/1.1 - RFC 2616) */
    public static final int SC_REQUEST_TOO_LONG = 413;
    /** {@code 414 Request-URI Too Long} (HTTP/1.1 - RFC 2616) */
    public static final int SC_REQUEST_URI_TOO_LONG = 414;
    /** {@code 415 Unsupported Media Type} (HTTP/1.1 - RFC 2616) */
    public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;
    /** {@code 416 Requested Range Not Satisfiable} (HTTP/1.1 - RFC 2616) */
    public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
    /** {@code 417 Expectation Failed} (HTTP/1.1 - RFC 2616) */
    public static final int SC_EXPECTATION_FAILED = 417;

    /**
     * Static constant for a 418 error.
     * {@code 418 Unprocessable Entity} (WebDAV drafts?)
     * or {@code 418 Reauthentication Required} (HTTP/1.1 drafts?)
     */
    // not used
    // public static final int SC_UNPROCESSABLE_ENTITY = 418;

    /**
     * Static constant for a 419 error.
     * {@code 419 Insufficient Space on Resource}
     * (WebDAV - draft-ietf-webdav-protocol-05?)
     * or {@code 419 Proxy Reauthentication Required}
     * (HTTP/1.1 drafts?)
     */
    public static final int SC_INSUFFICIENT_SPACE_ON_RESOURCE = 419;
    /**
     * Static constant for a 420 error.
     * {@code 420 Method Failure}
     * (WebDAV - draft-ietf-webdav-protocol-05?)
     */
    public static final int SC_METHOD_FAILURE = 420;
    /** {@code 422 Unprocessable Entity} (WebDAV - RFC 2518) */
    public static final int SC_UNPROCESSABLE_ENTITY = 422;
    /** {@code 423 Locked} (WebDAV - RFC 2518) */
    public static final int SC_LOCKED = 423;
    /** {@code 424 Failed Dependency} (WebDAV - RFC 2518) */
    public static final int SC_FAILED_DEPENDENCY = 424;

    // --- 5xx Server Error ---

    /** {@code 500 Server Error} (HTTP/1.0 - RFC 1945) */
    public static final int SC_INTERNAL_SERVER_ERROR = 500;
    /** {@code 501 Not Implemented} (HTTP/1.0 - RFC 1945) */
    public static final int SC_NOT_IMPLEMENTED = 501;
    /** {@code 502 Bad Gateway} (HTTP/1.0 - RFC 1945) */
    public static final int SC_BAD_GATEWAY = 502;
    /** {@code 503 Service Unavailable} (HTTP/1.0 - RFC 1945) */
    public static final int SC_SERVICE_UNAVAILABLE = 503;
    /** {@code 504 Gateway Timeout} (HTTP/1.1 - RFC 2616) */
    public static final int SC_GATEWAY_TIMEOUT = 504;
    /** {@code 505 HTTP Version Not Supported} (HTTP/1.1 - RFC 2616) */
    public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;

    /** {@code 507 Insufficient Storage} (WebDAV - RFC 2518) */
    public static final int SC_INSUFFICIENT_STORAGE = 507;
}

httpResult.java

##设置回调
$!callback.setFileName("HttpResult.java")
$!callback.setSavePath($tool.append($tableInfo.savePath, "/http"))
##使用宏定义设置回调(保存位置与文件后缀)
#if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}http;



/**
 * HTTP结果封装
 *
 * @author $!author
 * @since $!time.currTime()
 */
public class HttpResult {
    private int code = 200;
    private String msg;
    private Object data;

    public static HttpResult error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
    }

    public static HttpResult error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static HttpResult error(int code, String msg) {
        HttpResult r = new HttpResult();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    public static HttpResult ok(String msg) {
        HttpResult r = new HttpResult();
        r.setMsg(msg);
        return r;
    }

    public static HttpResult ok(Object data) {
        HttpResult r = new HttpResult();
        r.setData(data);
        return r;
    }

    public static HttpResult ok() {
        return new HttpResult();
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

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

}

3、总结

  书上的代码直接运行绝大部分是对的,但是总有一些软件的更新使得作者无能为力。之前的API是对的,但是之后就废弃了或修改了是常有的事。所以我们需要跟踪源代码。这只是一个小小的问题,如果没有前辈的无私奉献,很难想象我们自己一天能学到多少内容。感谢各位前辈的辛勤付出,让我们少走了很多的弯路!

点个赞再走呗!欢迎留言哦!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章