情况一:非前后分离
模板
#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 com.hc.bean.QueryBean;
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.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.validation.constraints.Null;
import java.io.Serializable;
import java.util.List;
#parse("File Header.java")
@Slf4j
@Controller
@RequestMapping("/${NameLowerFirst}")
public class ${NAME} {
@Resource
private ${NameUpperFirst}Service ${NameLowerFirst}Service;
@PostMapping
public ModelAndView create(@RequestBody ${NameUpperFirst} ${NameLowerFirst}, ModelAndView mav) {
${NameLowerFirst}Service.insert(${NameLowerFirst});
mav.setViewName("${NameLowerFirst}_list");
return mav;
}
@PutMapping
public ModelAndView updateById(@RequestBody ${NameUpperFirst} ${NameLowerFirst}, ModelAndView mav) {
${NameLowerFirst}Service.updateByPrimaryKeySelective(${NameLowerFirst});
mav.setViewName("${NameLowerFirst}_list");
return mav;
}
@DeleteMapping("/{id}")
public ModelAndView removeById(@PathVariable Serializable id, ModelAndView mav) {
${NameLowerFirst}Service.deleteByPrimaryKey(id);
mav.setViewName("${NameLowerFirst}_list");
return mav;
}
@GetMapping("/get/{id}")
public ModelAndView getById(@PathVariable Serializable id, ModelAndView mav) {
${NameUpperFirst} ${NameLowerFirst} = ${NameLowerFirst}Service.selectByPrimaryKey(id);
mav.addObject("${NameLowerFirst}", ${NameLowerFirst});
mav.setViewName("${NameLowerFirst}_details");
return mav;
}
//@GetMapping("/list")
//public ModelAndView list(QueryBean queryBean,
// @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
// @RequestParam(name = "pageSize", defaultValue = WeGoConst.PAGE_SIZE) Integer pageSize,
// ModelAndView mav) {
// //TODO .....queryBean转 ${NameLowerFirst},
// ${NameUpperFirst}Query ${NameLowerFirst}Query = null;
// PageBean pageBean = ${NameLowerFirst}Service.getPage( ${NameLowerFirst}Query,pageNum,pageSize);
// pageBean.setUrl("${NameLowerFirst}/list?");
//
// mav.addObject("pageBean", pageBean);
//
// mav.setViewName("${NameLowerFirst}_list");
// return mav;
// }
}
相关代码
QueryBean
@Getter
@Setter
@ToString
public class QueryBean {
/**
* 查询条件
*/
private String key;
/**
* 查询的具体值
*/
private String value;
}
PageBean
@Getter
@Setter
@NoArgsConstructor
public class PageBean {
/**
* 每页显示的条数
*/
private Integer pageSize;
/**
* 当前的页码
*/
private Integer pageNum;
/**
* 一共有多少条记录
*/
private Long total;
/**
* 一共有多少页
*/
private Integer pages;
/**
* 每一页所显示的数据
*/
private List records;
/**
* 分页请求路径
*/
private String url;
/**
* 将MyBatis的Page对象转换成我们自定义的PageBean对象
*
* @param page
*/
public PageBean(Page page) {
this.pageSize = page.getPageSize();
this.pageNum = page.getPageNum();
this.total = page.getTotal();
//将Long转换成Integer类型
long tmp = this.total % this.pageSize == 0 ? this.total / this.pageSize : this.total / this.pageSize + 1;
this.pages = Long.valueOf(tmp).intValue();
this.records = page.getResult();
}
@Override
public String toString() {
//返回当前对象的JSON字符串
return JsonUtil.obj2String(this);
}
}
JsonUtil
请参看:https://blog.csdn.net/lianghecai52171314/article/details/105893158
情况二:前后分离 创建模板 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 create(@RequestBody ${NameUpperFirst} ${NameLowerFirst}) {
boolean res = ${NameLowerFirst}Service.save(${NameLowerFirst});
return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
}
@PutMapping
public Result updateById(@RequestBody ${NameUpperFirst} ${NameLowerFirst}) {
boolean res = ${NameLowerFirst}Service.updateById(${NameLowerFirst});
return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
}
@DeleteMapping("{id}")
public Result removeById(@PathVariable Serializable id) {
boolean res = ${NameLowerFirst}Service.removeById(id);
return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
}
@GetMapping("{id}")
public Result getById(@PathVariable Serializable id) {
${NameUpperFirst} res = ${NameLowerFirst}Service.getById(id);
return ResultUtil.success("查询成功").setData(res);
}
@PostMapping
public Result getByCondition(@RequestBody ${NameUpperFirst}Condition condition) {
List list = ${NameLowerFirst}Service.queryByCondition(condition);
return ResultUtil.success("查询成功").setData(list);
}
}
相关代码
结果类:Result.java
@Getter
@ToString
public class Result {
/**
* 请求响应状态码
*/
private int code;
/**
* 请求结果描述信息
*/
private String msg;
/**
* 请求结果数据
*/
private T data;
public Result setCode(int code) {
this.code = code;
return this;
}
public Result setMsg(String msg) {
this.msg = msg;
return this;
}
public Result 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 {
/**
* 操作成功,返回具体的数据、结果码和提示信息
*
* @return 结果
*/
public static Result success() {
final Result result = new Result(ResultEnum.OK.getCode(), ResultEnum.OK.getMsg(), null);
return result;
}
public static Result success(String msg) {
final Result result = new Result(ResultEnum.OK.getCode(), msg, null);
return result;
}
/**
* 操作失败,返回具体的数据、结果码和提示信息
*
* @return 结果
*/
public static Result fail() {
final Result result = new Result(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), null);
return result;
}
public static Result fail(String msg) {
final Result result = new Result(ResultEnum.ERROR.getCode(), msg, null);
return result;
}
/**
* 操作成功,返回具体的数据、结果码和提示信息
*
* @param resultEnum 枚举
* @return 结果
*/
public static Result success(ResultEnum resultEnum) {
final Result result = new Result(resultEnum.getCode(), resultEnum.getMsg(), null);
return result;
}
/**
* 操作失败,返回具体的数据、结果码和提示信息
*
* @param resultEnum 枚举
* @return 结果
*/
public static Result fail(ResultEnum resultEnum) {
final Result result = new Result(resultEnum.getCode(), resultEnum.getMsg(), null);
return result;
}
public static void main(String[] args) {
Result r1 = ResultUtil.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 create(@RequestBody User user) {
boolean res = userService.save(user);
return res ? ResultUtil.success() : ResultUtil.failure();
}
@PutMapping
public Result updateById(@RequestBody User user) {
boolean res = userService.updateById(user);
return res ? ResultUtil.success() : ResultUtil.failure();
}
@DeleteMapping("{id}")
public Result removeById(@PathVariable Serializable id) {
boolean res = userService.removeById(id);
return res ? ResultUtil.success() : ResultUtil.failure();
}
@GetMapping("{id}")
public Result getById(@PathVariable Serializable id) {
User res = userService.getById(id);
return ResultUtil.success(200, "查询成功", res);
}
@GetMapping
public Result getByCondition(UserQuery condition) {
List list = userService.queryByCondition(condition);
return ResultUtil.success(200, "查询成功", list);
}
}