|
| 1 | +<#if isWithPackage?exists && isWithPackage==true>package ${packageName}.controller;</#if> |
| 2 | +<#if isAutoImport?exists && isAutoImport==true> |
| 3 | +import ${packageName}.entity.${classInfo.className}; |
| 4 | +import ${packageName}.repository.${classInfo.className}Repository; |
| 5 | +import org.springframework.data.domain.Example; |
| 6 | +import org.springframework.data.domain.Pageable; |
| 7 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 8 | +import org.springframework.web.bind.annotation.RequestParam; |
| 9 | +import org.springframework.data.domain.ExampleMatcher; |
| 10 | +import org.springframework.data.domain.PageRequest; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.web.bind.annotation.PostMapping; |
| 13 | +import org.springframework.web.bind.annotation.RestController; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +</#if> |
| 19 | +/** |
| 20 | + * @description ${classInfo.classComment} |
| 21 | + * @author ${authorName} |
| 22 | + * @date ${.now?string('yyyy-MM-dd')} |
| 23 | + */ |
| 24 | + |
| 25 | +@Slf4j |
| 26 | +@Api(tags = "${classInfo.className?uncap_first}") |
| 27 | +@CrossOrigin |
| 28 | +@RestController |
| 29 | +@RequestMapping("/${classInfo.className?uncap_first}") |
| 30 | +public class ${classInfo.className}Controller { |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private ${classInfo.className}Repository ${classInfo.className?uncap_first}Repository; |
| 34 | + |
| 35 | + /** |
| 36 | + * 新增或编辑 |
| 37 | + */ |
| 38 | + @PostMapping("/save") |
| 39 | + @ApiOperation(value = "save ${classInfo.className}", notes = "save ${classInfo.className}") |
| 40 | + public Object save(@RequestBody ${classInfo.className} ${classInfo.className?uncap_first}){ |
| 41 | + try { |
| 42 | + return ReturnT.success(${classInfo.className?uncap_first}Repository.save(${classInfo.className?uncap_first})); |
| 43 | + } catch (Exception e) { |
| 44 | + e.printStackTrace(); |
| 45 | + return ReturnT.error("保存失败"); |
| 46 | + } |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * 删除 |
| 52 | + */ |
| 53 | + @PostMapping("/delete") |
| 54 | + @ApiOperation(value = "delete ${classInfo.className}", notes = "delete ${classInfo.className}") |
| 55 | + public Object delete(int id){ |
| 56 | + Optional<${classInfo.className}> ${classInfo.className?uncap_first}=${classInfo.className?uncap_first}Repository.findById(id); |
| 57 | + if(${classInfo.className?uncap_first}.isPresent()){ |
| 58 | + ${classInfo.className?uncap_first}Repository.deleteById(id); |
| 59 | + return ${returnUtilSuccess}("删除成功"); |
| 60 | + }else{ |
| 61 | + return ${returnUtilFailure}("没有找到该对象"); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * 查询 |
| 67 | + */ |
| 68 | + @PostMapping("/find") |
| 69 | + @ApiOperation(value = "find ${classInfo.className} by id", notes = "find ${classInfo.className} by id") |
| 70 | + public Object find(int id){ |
| 71 | + Optional<${classInfo.className}> ${classInfo.className?uncap_first}=${classInfo.className?uncap_first}Repository.findById(id); |
| 72 | + if(${classInfo.className?uncap_first}.isPresent()){ |
| 73 | + return ${returnUtilSuccess}(${classInfo.className?uncap_first}.get()); |
| 74 | + }else{ |
| 75 | + return ${returnUtilFailure}("没有找到该对象"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 分页查询 |
| 81 | + */ |
| 82 | + @PostMapping("/list") |
| 83 | + @ApiOperation(value = "list ${classInfo.className}", notes = "list ${classInfo.className}") |
| 84 | + public Object list(@RequestBody ${classInfo.className} ${classInfo.className?uncap_first}, |
| 85 | + @RequestParam(required = false, defaultValue = "0") int pageNumber, |
| 86 | + @RequestParam(required = false, defaultValue = "10") int pageSize) { |
| 87 | + |
| 88 | + try { |
| 89 | + //创建匹配器,需要查询条件请修改此处代码 |
| 90 | + ExampleMatcher matcher = ExampleMatcher.matchingAll(); |
| 91 | + |
| 92 | + //创建实例 |
| 93 | + Example<${classInfo.className}> example = Example.of(${classInfo.className?uncap_first}, matcher); |
| 94 | + //分页构造 |
| 95 | + Pageable pageable = PageRequest.of(pageNumber,pageSize); |
| 96 | + |
| 97 | + return ReturnT.success(${classInfo.className?uncap_first}Repository.findAll(example, pageable)); |
| 98 | + |
| 99 | + } catch (Exception e) { |
| 100 | + e.printStackTrace(); |
| 101 | + return ReturnT.error(e.getMessage()); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments