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是對的,但是之後就廢棄了或修改了是常有的事。所以我們需要跟蹤源代碼。這只是一個小小的問題,如果沒有前輩的無私奉獻,很難想象我們自己一天能學到多少內容。感謝各位前輩的辛勤付出,讓我們少走了很多的彎路!

點個贊再走唄!歡迎留言哦!

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