controller.ftl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package ${basePackage}.controller;
  2. import com.txz.mall.core.Result;
  3. import ${basePackage}.model.${modelNameUpperCamel};
  4. import ${basePackage}.service.${modelNameUpperCamel}Service;
  5. import ${basePackage}.core.ResultCode;
  6. import com.github.pagehelper.PageHelper;
  7. import com.github.pagehelper.PageInfo;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestBody;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiOperation;
  17. import tk.mybatis.mapper.entity.Condition;
  18. import tk.mybatis.mapper.entity.Example.Criteria;
  19. import javax.annotation.Resource;
  20. import java.util.List;
  21. import java.util.Date;
  22. /**
  23. * Created by ${author} on ${date}.
  24. */
  25. @Api(tags = "[后台]${modelNameLowerCamel}管理")
  26. @RestController
  27. @RequestMapping("${baseRequestMapping}")
  28. public class ${modelNameUpperCamel}Controller {
  29. private static Logger log = LoggerFactory.getLogger(${modelNameUpperCamel}Controller.class);
  30. @Resource
  31. private ${modelNameUpperCamel}Service ${modelNameLowerCamel}Service;
  32. @PostMapping("/add")
  33. @ApiOperation(value = "${modelNameLowerCamel}新增",httpMethod = "POST")
  34. public Result add(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel},Long userId) {
  35. if(${modelNameLowerCamel} == null){
  36. return Result.fail(ResultCode.OBJECT_IS_NULL);
  37. }
  38. if(userId == null){
  39. return Result.fail(ResultCode.USERID_IS_NULL);
  40. }
  41. try {
  42. ${modelNameLowerCamel}.setCreateTime(new Date());
  43. ${modelNameLowerCamel}.setCreateUserId(userId);
  44. ${modelNameLowerCamel}Service.save(${modelNameLowerCamel});
  45. } catch (Exception e) {
  46. log.error("新增对象操作异常e:{}",e);
  47. return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);
  48. }
  49. return Result.success();
  50. }
  51. @PostMapping("/delete")
  52. @ApiOperation(value = "${modelNameLowerCamel}删除",httpMethod = "POST")
  53. public Result delete(@RequestParam Long id,Long userId) {
  54. if(id == null){
  55. return Result.fail(ResultCode.ID_IS_NULL);
  56. }
  57. if(userId == null){
  58. return Result.fail(ResultCode.USERID_IS_NULL);
  59. }
  60. try {
  61. ${modelNameUpperCamel} ${modelNameLowerCamel} = new ${modelNameUpperCamel}();
  62. ${modelNameLowerCamel}.setId(id);
  63. ${modelNameLowerCamel}.setIsDelete(1);
  64. ${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
  65. } catch (Exception e) {
  66. log.error("删除对象操作异常e:{}",e);
  67. return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);
  68. }
  69. return Result.success();
  70. }
  71. @PostMapping("/update")
  72. @ApiOperation(value = "${modelNameLowerCamel}更新",httpMethod = "POST")
  73. public Result update(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel},Long userId) {
  74. if(${modelNameLowerCamel} == null){
  75. return Result.fail(ResultCode.OBJECT_IS_NULL);
  76. }
  77. if(${modelNameLowerCamel}.getId() == null){
  78. return Result.fail(ResultCode.ID_IS_NULL);
  79. }
  80. if(userId == null){
  81. return Result.fail(ResultCode.USERID_IS_NULL);
  82. }
  83. try {
  84. ${modelNameLowerCamel}.setUpdateTime(new Date());
  85. ${modelNameLowerCamel}.setUpdateUserId(userId);
  86. ${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
  87. } catch (Exception e) {
  88. log.error("更新对象操作异常e:{}",e);
  89. return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);
  90. }
  91. return Result.success();
  92. }
  93. @PostMapping("/detail")
  94. @ApiOperation(value = "${modelNameLowerCamel}获取详情",httpMethod = "POST")
  95. public Result<${modelNameUpperCamel}> detail(@RequestParam Long id,Long userId) {
  96. if(id == null){
  97. return Result.fail(ResultCode.ID_IS_NULL);
  98. }
  99. if(userId == null){
  100. return Result.fail(ResultCode.USERID_IS_NULL);
  101. }
  102. ${modelNameUpperCamel} ${modelNameLowerCamel} = null;
  103. try {
  104. ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id);
  105. } catch (Exception e) {
  106. log.error("查询对象操作异常e:{}",e);
  107. return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);
  108. }
  109. return Result.success(${modelNameLowerCamel});
  110. }
  111. @PostMapping("/list")
  112. @ApiOperation(value = "${modelNameLowerCamel}获取列表",httpMethod = "POST")
  113. public Result<List<${modelNameUpperCamel}>> list(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size,Long userId) {
  114. if(userId == null){
  115. return Result.fail(ResultCode.USERID_IS_NULL);
  116. }
  117. PageHelper.startPage(page, size);
  118. Condition condition = new Condition(${modelNameLowerCamel}.getClass());
  119. // Criteria criteria = condition.createCriteria();
  120. // criteria.andEqualTo("name", city.getName());
  121. PageInfo pageInfo = null;
  122. try {
  123. List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findByCondition(condition);
  124. pageInfo = new PageInfo(list);
  125. } catch (Exception e) {
  126. log.error("查询对象操作异常e:{}",e);
  127. return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);
  128. }
  129. return Result.success(pageInfo);
  130. }
  131. }