Controller.java.ftl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package ${package}.modules.${moduleName}.controller<#if subModuleName??>.${subModuleName}</#if>;
  2. import ${package}.common.annotation.LogOperation;
  3. import ${package}.common.constant.Constant;
  4. import ${package}.common.page.PageData;
  5. import ${package}.common.utils.ExcelUtils;
  6. import ${package}.common.utils.Result;
  7. import ${package}.common.validator.AssertUtils;
  8. import ${package}.common.validator.ValidatorUtils;
  9. import ${package}.common.validator.group.AddGroup;
  10. import ${package}.common.validator.group.DefaultGroup;
  11. import ${package}.common.validator.group.UpdateGroup;
  12. import ${package}.modules.${moduleName}.dto<#if subModuleName??>.${subModuleName}</#if>.${ClassName}DTO;
  13. import ${package}.modules.${moduleName}.excel<#if subModuleName??>.${subModuleName}</#if>.${ClassName}Excel;
  14. import ${package}.modules.${moduleName}.service<#if subModuleName??>.${subModuleName}</#if>.${ClassName}Service;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiImplicitParam;
  17. import io.swagger.annotations.ApiImplicitParams;
  18. import io.swagger.annotations.ApiOperation;
  19. import org.apache.shiro.authz.annotation.RequiresPermissions;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.web.bind.annotation.*;
  22. import springfox.documentation.annotations.ApiIgnore;
  23. import javax.servlet.http.HttpServletResponse;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * ${tableComment}
  28. *
  29. * @author ${author} ${email}
  30. * @since ${version} ${date}
  31. */
  32. @RestController
  33. @RequestMapping("${moduleName}/${classname}")
  34. @Api(tags="${tableComment}")
  35. public class ${ClassName}Controller {
  36. @Autowired
  37. private ${ClassName}Service ${className}Service;
  38. @GetMapping("page")
  39. @ApiOperation("分页")
  40. @ApiImplicitParams({
  41. @ApiImplicitParam(name = Constant.PAGE, value = "当前页码,从1开始", paramType = "query", required = true, dataType="int") ,
  42. @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
  43. @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
  44. @ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataType="String")
  45. })
  46. @RequiresPermissions("${moduleName}:${classname}:page")
  47. public Result<PageData<${ClassName}DTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
  48. PageData<${ClassName}DTO> page = ${className}Service.page(params);
  49. return new Result<PageData<${ClassName}DTO>>().ok(page);
  50. }
  51. @GetMapping("{id}")
  52. @ApiOperation("信息")
  53. @RequiresPermissions("${moduleName}:${classname}:info")
  54. public Result<${ClassName}DTO> get(@PathVariable("id") Long id){
  55. ${ClassName}DTO data = ${className}Service.get(id);
  56. return new Result<${ClassName}DTO>().ok(data);
  57. }
  58. @PostMapping
  59. @ApiOperation("保存")
  60. @LogOperation("保存")
  61. @RequiresPermissions("${moduleName}:${classname}:save")
  62. public Result save(@RequestBody ${ClassName}DTO dto){
  63. //效验数据
  64. ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
  65. ${className}Service.save(dto);
  66. return new Result();
  67. }
  68. @PutMapping
  69. @ApiOperation("修改")
  70. @LogOperation("修改")
  71. @RequiresPermissions("${moduleName}:${classname}:update")
  72. public Result update(@RequestBody ${ClassName}DTO dto){
  73. //效验数据
  74. ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
  75. ${className}Service.update(dto);
  76. return new Result();
  77. }
  78. @DeleteMapping
  79. @ApiOperation("删除")
  80. @LogOperation("删除")
  81. @RequiresPermissions("${moduleName}:${classname}:delete")
  82. public Result delete(@RequestBody Long[] ids){
  83. //效验数据
  84. AssertUtils.isArrayEmpty(ids, "id");
  85. ${className}Service.delete(ids);
  86. return new Result();
  87. }
  88. @GetMapping("export")
  89. @ApiOperation("导出")
  90. @LogOperation("导出")
  91. @RequiresPermissions("${moduleName}:${classname}:export")
  92. public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
  93. List<${ClassName}DTO> list = ${className}Service.list(params);
  94. ExcelUtils.exportExcelToTarget(response, null, "${tableComment}", list, ${ClassName}Excel.class);
  95. }
  96. }