ControllerTemplate.ftl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package ${model.basePackage}.web<#if model.genPackage??>.${model.genPackage}</#if>;
  2. <#if imports??>
  3. <#list imports as i>
  4. ${i}
  5. </#list>
  6. </#if>
  7. import ${model.basePackage}.domain.<#if model.genPackage??>${model.genPackage}.</#if>${model.className};
  8. import ${model.basePackage}.service.<#if model.genPackage??>${model.genPackage}.</#if>${model.className}Service;
  9. import ${model.basePackage}.dto.PageQuery;
  10. import ${model.basePackage}.exception.BusinessException;
  11. import ${model.basePackage}.repo.<#if model.genPackage??>${model.genPackage}.</#if>${model.className}Repo;
  12. import ${model.basePackage}.utils.ObjUtils;
  13. import ${model.basePackage}.utils.excel.ExcelUtils;
  14. <#if subPackage == true>
  15. </#if>
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.domain.Page;
  18. import org.springframework.security.access.prepost.PreAuthorize;
  19. import org.springframework.web.bind.annotation.*;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.IOException;
  22. import java.util.List;
  23. @RestController
  24. @RequestMapping("/${model.className?uncap_first}")
  25. public class ${model.className}Controller extends BaseController {
  26. /*generatedStart*/
  27. @Autowired
  28. private ${model.className}Service ${model.className?uncap_first}Service;
  29. @Autowired
  30. private ${model.className}Repo ${model.className?uncap_first}Repo;
  31. /*generatedEnd*/
  32. /*generatedStart*/
  33. @PreAuthorize("hasRole('ADMIN')")
  34. @PostMapping("/save")
  35. public ${model.className} save(@RequestBody ${model.className} record) {
  36. if (record.getId() != null) {
  37. ${model.className} orig = ${model.className?uncap_first}Repo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
  38. ObjUtils.merge(orig, record);
  39. return ${model.className?uncap_first}Repo.save(orig);
  40. }
  41. return ${model.className?uncap_first}Repo.save(record);
  42. }
  43. @PreAuthorize("hasRole('ADMIN')")
  44. @GetMapping("/all")
  45. public Page<${model.className}> all(PageQuery pageQuery) {
  46. return ${model.className?uncap_first}Repo.findAll(toSpecification(pageQuery,${model.className}.class), toPageRequest(pageQuery));
  47. }
  48. @GetMapping("/get/{id}")
  49. public ${model.className} get(@PathVariable Long id) {
  50. return ${model.className?uncap_first}Repo.findById(id).orElseThrow(new BusinessException("无记录"));
  51. }
  52. @PostMapping("/del/{id}")
  53. public void del(@PathVariable Long id) {
  54. ${model.className?uncap_first}Repo.deleteById(id);
  55. }
  56. @GetMapping("/excel")
  57. @ResponseBody
  58. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  59. List<${model.className}> data = all(pageQuery).getContent();
  60. ExcelUtils.export(response, data);
  61. }
  62. /*generatedEnd*/
  63. }