controller.java.ftl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package ${package.Controller};
  2. import ${package.Entity}.${entity};
  3. import ${package.Service}.${table.serviceName};
  4. <#if swagger>
  5. <#if swaggerVersion == "swagger2">
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import io.swagger.annotations.ApiParam;
  9. <#elseif swaggerVersion == "openapi3">
  10. import io.swagger.v3.oas.annotations.Operation;
  11. import io.swagger.v3.oas.annotations.Parameter;
  12. import io.swagger.v3.oas.annotations.tags.Tag;
  13. </#if>
  14. </#if>
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.web.bind.annotation.*;
  17. <#if restControllerStyle>
  18. import org.springframework.web.bind.annotation.RestController;
  19. <#else>
  20. import org.springframework.stereotype.Controller;
  21. </#if>
  22. <#if superControllerClassPackage??>
  23. import ${superControllerClassPackage};
  24. </#if>
  25. /**
  26. * <p>${table.comment}控制器</p>
  27. *
  28. * <p>提供${table.comment}相关的 REST API 接口,包括${table.comment}的创建、查询、更新和删除操作。
  29. * 本控制器遵循 RESTful 设计规范,使用标准的 HTTP 方法进行资源操作。</p>
  30. *
  31. * <p>主要功能:
  32. * <ul>
  33. * <li>创建${table.comment}</li>
  34. * <li>根据ID查询${table.comment}信息</li>
  35. * <li>更新${table.comment}信息</li>
  36. * <li>删除${table.comment}</li>
  37. <#if customMethods??>
  38. <#list customMethods as method>
  39. * <li>${method.description}</li>
  40. </#list>
  41. </#if>
  42. * </ul>
  43. * </p>
  44. *
  45. * @author ${author}
  46. * @since ${date}
  47. */
  48. <#if swagger>
  49. <#if swaggerVersion == "swagger2">
  50. @Api(value = "${table.comment}管理", tags = "${table.comment}管理接口")
  51. <#elseif swaggerVersion == "openapi3">
  52. @Tag(name = "${table.comment}管理", description = "${table.comment}管理接口")
  53. </#if>
  54. </#if>
  55. <#if restControllerStyle>
  56. @RestController
  57. <#else>
  58. @Controller
  59. </#if>
  60. @RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle>${table.entityPath}<#else>${table.entityPath}</#if>"<#if superControllerClass??>, produces = "application/json;charset=UTF-8"</#if>)
  61. <#if superControllerClass??>
  62. public class ${table.controllerName} extends ${superControllerClass} {
  63. <#else>
  64. public class ${table.controllerName} {
  65. </#if>
  66. @Autowired
  67. private ${table.serviceName} ${table.serviceName?substring(0,1)?lower_case}${table.serviceName?substring(1)};
  68. /**
  69. * <p>创建${table.comment}</p>
  70. *
  71. * <p>接收${table.comment}创建请求,验证数据后创建新${table.comment}并返回${table.comment}信息。</p>
  72. *
  73. * @param entity ${table.comment}实体对象
  74. * @return ${table.comment}实体对象
  75. */
  76. <#if swagger>
  77. <#if swaggerVersion == "swagger2">
  78. @ApiOperation(value = "创建${table.comment}", notes = "创建新的${table.comment}记录")
  79. <#elseif swaggerVersion == "openapi3">
  80. @Operation(summary = "创建${table.comment}", description = "创建新的${table.comment}记录")
  81. </#if>
  82. </#if>
  83. @PostMapping
  84. public ${entity} create(@RequestBody ${entity} entity) {
  85. return ${table.serviceName?substring(0,1)?lower_case}${table.serviceName?substring(1)}.save(entity);
  86. }
  87. /**
  88. * <p>根据ID查询${table.comment}</p>
  89. *
  90. * <p>根据提供的${table.comment}ID查询对应的${table.comment}详细信息。</p>
  91. *
  92. * @param id ${table.comment}唯一标识符
  93. * @return ${table.comment}实体对象
  94. */
  95. <#if swagger>
  96. <#if swaggerVersion == "swagger2">
  97. @ApiOperation(value = "根据ID查询${table.comment}", notes = "根据ID查询${table.comment}详细信息")
  98. @ApiParam(name = "id", value = "${table.comment}ID", required = true)
  99. <#elseif swaggerVersion == "openapi3">
  100. @Operation(summary = "根据ID查询${table.comment}", description = "根据ID查询${table.comment}详细信息")
  101. @Parameter(name = "id", description = "${table.comment}ID", required = true)
  102. </#if>
  103. </#if>
  104. @GetMapping("/{id}")
  105. public ${entity} getById(@PathVariable Long id) {
  106. return ${table.serviceName?substring(0,1)?lower_case}${table.serviceName?substring(1)}.getById(id);
  107. }
  108. /**
  109. * <p>更新${table.comment}</p>
  110. *
  111. * <p>根据${table.comment}ID和更新请求,更新${table.comment}的指定字段信息。</p>
  112. *
  113. * @param id ${table.comment}唯一标识符
  114. * @param entity ${table.comment}实体对象
  115. * @return 更新后的${table.comment}实体对象
  116. */
  117. <#if swagger>
  118. <#if swaggerVersion == "swagger2">
  119. @ApiOperation(value = "更新${table.comment}", notes = "更新${table.comment}信息")
  120. <#elseif swaggerVersion == "openapi3">
  121. @Operation(summary = "更新${table.comment}", description = "更新${table.comment}信息")
  122. </#if>
  123. </#if>
  124. @PutMapping("/{id}")
  125. public ${entity} update(@PathVariable Long id, @RequestBody ${entity} entity) {
  126. entity.setId(id);
  127. return ${table.serviceName?substring(0,1)?lower_case}${table.serviceName?substring(1)}.updateById(entity) ? entity : null;
  128. }
  129. /**
  130. * <p>删除${table.comment}</p>
  131. *
  132. * <p>根据${table.comment}ID删除指定的${table.comment}。删除操作会级联删除${table.comment}相关的数据。</p>
  133. *
  134. * @param id ${table.comment}唯一标识符
  135. * @return 操作结果
  136. */
  137. <#if swagger>
  138. <#if swaggerVersion == "swagger2">
  139. @ApiOperation(value = "删除${table.comment}", notes = "根据ID删除${table.comment}")
  140. <#elseif swaggerVersion == "openapi3">
  141. @Operation(summary = "删除${table.comment}", description = "根据ID删除${table.comment}")
  142. </#if>
  143. </#if>
  144. @DeleteMapping("/{id}")
  145. public boolean delete(@PathVariable Long id) {
  146. return ${table.serviceName?substring(0,1)?lower_case}${table.serviceName?substring(1)}.removeById(id);
  147. }
  148. <#if customMethods??>
  149. ## ---------- BEGIN 自定义接口 ----------
  150. <#list customMethods as method>
  151. /**
  152. * <p>${method.description}</p>
  153. *
  154. * <p>${method.detailDescription}</p>
  155. *
  156. <#list method.parameters as param>
  157. * @param ${param.name} ${param.type} ${param.description}
  158. </#list>
  159. * @return ${method.returnType} ${method.returnDescription}
  160. */
  161. <#if swagger>
  162. <#if swaggerVersion == "swagger2">
  163. @ApiOperation(value = "${method.description}", notes = "${method.detailDescription}")
  164. <#elseif swaggerVersion == "openapi3">
  165. @Operation(summary = "${method.description}", description = "${method.detailDescription}")
  166. </#if>
  167. </#if>
  168. @GetMapping("/${method.mappingPath}")
  169. public ${method.returnType} ${method.name}(<#list method.parameters as param>@RequestParam ${param.type} ${param.name}<#if param_has_next>, </#if></#list>) {
  170. return ${table.serviceName?substring(0,1)?lower_case}${table.serviceName?substring(1)}.${method.name}(<#list method.parameters as param>${param.name}<#if param_has_next>, </#if></#list>);
  171. }
  172. </#list>
  173. ## ---------- END 自定义接口 ----------
  174. </#if>
  175. }