controller2.java.btl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package ${package.Controller};
  2. import com.taotikee.common.api.CommonResult;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import io.swagger.annotations.Api;
  7. import io.swagger.annotations.ApiOperation;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import ${package.Service}.${table.serviceName};
  12. import ${package.Entity}.${entity};
  13. import java.io.Serializable;
  14. import java.util.List;
  15. <% if(restControllerStyle){ %>
  16. import org.springframework.web.bind.annotation.RestController;
  17. <% }else{ %>
  18. import org.springframework.stereotype.Controller;
  19. <% } %>
  20. <% if(isNotEmpty(superControllerClassPackage)){ %>
  21. import ${superControllerClassPackage};
  22. <% } %>
  23. /**
  24. * <p>
  25. * ${table.comment!} 前端控制器
  26. * </p>
  27. *
  28. * @author ${author}
  29. * @since ${date}
  30. */
  31. @Api(tags = "${table.controllerName}", description = "${table.controllerName}")
  32. @Slf4j
  33. <% if(restControllerStyle){ %>
  34. @RestController
  35. <% }else{ %>
  36. @Controller
  37. <% } %>
  38. @RequestMapping("<% if(isNotEmpty(package.ModuleName)){ %>/${package.ModuleName}<% } %>/<% if(isNotEmpty(controllerMappingHyphenStyle)){ %>${controllerMappingHyphen}<% }else{ %>${table.entityPath}<% } %>")
  39. <% if(kotlin){ %>
  40. class ${table.controllerName}<% if(isNotEmpty(superControllerClass)){ %> : ${superControllerClass}()<% } %>
  41. <% }else{ %>
  42. <% if(isNotEmpty(superControllerClass)){ %>
  43. public class ${table.controllerName} extends ${superControllerClass} {
  44. <% }else{ %>
  45. public class ${table.controllerName}{
  46. @Autowired
  47. public ${table.serviceName} i${entity}Service;
  48. /**
  49. * 分页查询所有数据
  50. *
  51. * @param pageNo 页码
  52. * @param pageSize 条数
  53. * @param ${table.entityPath} 查询实体
  54. * @return 所有数据
  55. */
  56. @ApiOperation("分页查询所有数据")
  57. @GetMapping
  58. public CommonResult selectAll(Integer pageNo, Integer pageSize,${entity} ${table.entityPath}) {
  59. IPage<${entity}> page = new Page<>(pageNo,pageSize);
  60. return CommonResult.success(this.i${entity}Service.page(page, new QueryWrapper<>(${table.entityPath})));
  61. }
  62. /**
  63. * 通过主键查询单条数据
  64. *
  65. * @param id 主键
  66. * @return 单条数据
  67. */
  68. @ApiOperation("通过主键查询单条数据")
  69. @GetMapping("{id}")
  70. public CommonResult selectOne(@PathVariable Serializable id) {
  71. return CommonResult.success(this.i${entity}Service.getById(id));
  72. }
  73. /**
  74. * 新增数据
  75. *
  76. * @param ${table.entityPath} 实体对象
  77. * @return 新增结果
  78. */
  79. @ApiOperation("新增数据")
  80. @PostMapping
  81. public CommonResult insert(@RequestBody ${entity} ${table.entityPath}) {
  82. try{
  83. return CommonResult.success(this.i${entity}Service.save( ${table.entityPath}),"添加成功");
  84. } catch (Exception e) {
  85. log.error("出现错误, {}",e.getMessage());
  86. return CommonResult.failed("添加失败");
  87. }
  88. }
  89. /**
  90. * 修改数据
  91. *
  92. * @param ${table.entityPath} 实体对象
  93. * @return 修改结果
  94. */
  95. @ApiOperation("修改数据")
  96. @PutMapping
  97. public CommonResult update(@RequestBody ${entity} ${table.entityPath}) {
  98. try {
  99. return CommonResult.success(this.i${entity}Service.updateById(${table.entityPath}),"修改成功");
  100. } catch (Exception e) {
  101. log.error("出现错误, {}",e.getMessage());
  102. return CommonResult.failed("修改失败");
  103. }
  104. }
  105. /**
  106. * 删除数据
  107. *
  108. * @param idList 主键结合
  109. * @return 删除结果
  110. */
  111. @ApiOperation("删除数据")
  112. @DeleteMapping
  113. public CommonResult delete(@RequestParam("idList") List<Long> idList) {
  114. try{
  115. return CommonResult.success(this.i${entity}Service.removeByIds(idList),"删除成功");
  116. } catch ( Exception e) {
  117. log.error("出现错误, {}",e.getMessage());
  118. return CommonResult.failed("删除失败");
  119. }
  120. }
  121. <% } %>
  122. }
  123. <% } %>