创建模板

Intellij定位

在这里插入图片描述

具体模板代码:

#set($NameNoController = $NAME.length() - 10)
#set($NameLowerFirst = ${NAME.substring(0,1).toLowerCase()} + $NAME.substring(1,$NameNoController))
#set($NameUpperFirst = $NAME.substring(0,$NameNoController))

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end

import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.io.Serializable;
import java.util.List;

#parse("File Header.java")
@Slf4j
@RestController
@RequestMapping("/${NameLowerFirst}")
public class ${NAME} {

    @Resource
    private ${NameUpperFirst}Service ${NameLowerFirst}Service;

    @PostMapping
    public Result<Null> create(@RequestBody ${NameUpperFirst} ${NameLowerFirst}) {
        boolean res = ${NameLowerFirst}Service.save(${NameLowerFirst});
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result<Null> updateById(@RequestBody ${NameUpperFirst} ${NameLowerFirst}) {
        boolean res = ${NameLowerFirst}Service.updateById(${NameLowerFirst});
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result<Null> removeById(@PathVariable Serializable id) {
        boolean res = ${NameLowerFirst}Service.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result<${NameUpperFirst}> getById(@PathVariable Serializable id) {
        ${NameUpperFirst} res = ${NameLowerFirst}Service.getById(id);
        return ResultUtil.<${NameUpperFirst}>success("查询成功").setData(res);
    }

    @GetMapping
    public Result<List<${NameUpperFirst}>> getByCondition(@RequestBody  ${NameUpperFirst}Query condition) {
        List<${NameUpperFirst}> list = ${NameLowerFirst}Service.queryByCondition(condition);
        return ResultUtil.<${NameUpperFirst}>success("查询成功").setData(list);
    }
}

相关代码

结果类:Result.java

@Getter
@ToString
public class Result<T> {
    /**
     * 请求响应状态码
     */
    private int code;
    /**
     * 请求结果描述信息
     */
    private String msg;
    /**
     * 请求结果数据
     */
    private T data;

    public Result<T> setCode(int code) {
        this.code = code;
        return this;
    }

    public Result<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    protected Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
}

结果工具类:ResultUtil.java

public class ResultUtil<T> {

    /**
     * 操作成功,返回具体的数据、结果码和提示信息
     *
     * @return 结果
     */
    public static <T> Result<T> success() {
        final Result<T> result = new Result(ResultEnum.OK.getCode(), ResultEnum.OK.getMsg(), null);
        return result;
    }

    public static <T> Result<T> success(String msg) {
        final Result<T> result = new Result(ResultEnum.OK.getCode(), msg, null);
        return result;
    }

    /**
     * 操作失败,返回具体的数据、结果码和提示信息
     *
     * @return 结果
     */
    public static <T> Result<T> fail() {
        final Result<T> result = new Result(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
        return result;
    }

    public static <T> Result<T> fail(String msg) {
        final Result<T> result = new Result(ResultEnum.ERROR.getCode(), msg, null);
        return result;
    }

    /**
     * 操作成功,返回具体的数据、结果码和提示信息
     *
     * @param resultEnum 枚举
     * @return 结果
     */
    public static <T> Result<T> success(ResultEnum resultEnum) {
        final Result<T> result = new Result(resultEnum.getCode(), resultEnum.getMsg(), null);
        return result;
    }

    /**
     * 操作失败,返回具体的数据、结果码和提示信息
     *
     * @param resultEnum 枚举
     * @return 结果
     */
    public static <T> Result<T> fail(ResultEnum resultEnum) {
        final Result<T> result = new Result(resultEnum.getCode(), resultEnum.getMsg(), null);
        return result;
    }

    public static void main(String[] args) {
        Result<Null> r1 = ResultUtil.<Null>success().setMsg("haha");
        System.out.println(r1);
        System.out.println(r1.getMsg() == null);
        System.out.println(r1.getMsg() instanceof String);

        final Result r2 = ResultUtil.success(ResultEnum.OK);
        System.out.println(r2);
        final Result r3 = ResultUtil.success().setData(new Date());
        final Result r4 = ResultUtil.success(ResultEnum.OK);

    }
}

结果枚举类:ResultEnum.java

public enum ResultEnum {
    /**
     * 成功
     */
    OK(200, "Success"),
    /**
     * 成功
     */
    SUCCESS(200,"成功"),
    /**
     * 找不到
     */
    NOT_FOUND(404, "Not Found"),

    /**
     * 服务器错误
     */
    ERROR(500, "Server Error"),

    /**
     * 参数不正确
     */
    PARAM_ERROR(501,"Parameter Error");

    /**
     * 操作代码
     */
    int code;

    /**
     * 提示信息
     */
    String msg;

    ResultEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

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

示例

在这里插入图片描述
结果:

package com.resume;

import com.resume.bean.Result;
import com.resume.bean.ResultUtil;
import com.resume.domain.User;
import com.resume.domain.UserQuery;
import com.resume.service.UserService;
import lombok.extern.slf4j.Slf4j;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author 梁云亮
 * @version v1.0
 * @date 2021/8/16 21:16
 * @describe
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping
    public Result<Void> create(@RequestBody User user) {
        boolean res = userService.save(user);
        return res ? ResultUtil.success() : ResultUtil.failure();
    }

    @PutMapping
    public Result<Void> updateById(@RequestBody User user) {
        boolean res = userService.updateById(user);
        return res ? ResultUtil.success() : ResultUtil.failure();
    }

    @DeleteMapping("{id}")
    public Result<Void> removeById(@PathVariable Serializable id) {
        boolean res = userService.removeById(id);
        return res ? ResultUtil.success() : ResultUtil.failure();
    }

    @GetMapping("{id}")
    public Result<User> getById(@PathVariable Serializable id) {
        User res = userService.getById(id);
        return ResultUtil.success(200, "查询成功", res);
    }

    @GetMapping
    public Result<List<User>> getByCondition(UserQuery condition) {
        List<User> list = userService.queryByCondition(condition);
        return ResultUtil.success(200, "查询成功", list);
    }
}