|
| 1 | +package ${package.Controller}; |
| 2 | + |
| 3 | + |
| 4 | +import io.github.talelin.autoconfigure.exception.NotFoundException; |
| 5 | +<#if package.Controller?split(".")?last == "cms"> |
| 6 | +import io.github.talelin.core.annotation.LoginRequired; |
| 7 | +</#if> |
| 8 | +import io.github.talelin.latticy.common.mybatis.Page; |
| 9 | +import io.github.talelin.latticy.common.util.PageUtil; |
| 10 | +import ${package.Service}.${table.serviceName}; |
| 11 | +import org.springframework.beans.BeanUtils; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.web.bind.annotation.PostMapping; |
| 14 | +import org.springframework.web.bind.annotation.PutMapping; |
| 15 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 18 | +import org.springframework.web.bind.annotation.PathVariable; |
| 19 | +import org.springframework.web.bind.annotation.RequestParam; |
| 20 | +import org.springframework.web.bind.annotation.RequestBody; |
| 21 | +import ${package.Entity}.${entity}; |
| 22 | +import io.github.talelin.latticy.vo.CreatedVO; |
| 23 | +import io.github.talelin.latticy.vo.DeletedVO; |
| 24 | +import io.github.talelin.latticy.vo.PageResponseVO; |
| 25 | +import io.github.talelin.latticy.vo.UpdatedVO; |
| 26 | + |
| 27 | +import javax.validation.constraints.Min; |
| 28 | +import javax.validation.constraints.Max; |
| 29 | +import javax.validation.constraints.Positive; |
| 30 | + |
| 31 | +<#if restControllerStyle> |
| 32 | +import org.springframework.web.bind.annotation.RestController; |
| 33 | +<#else> |
| 34 | +import org.springframework.stereotype.Controller; |
| 35 | +</#if> |
| 36 | +<#if superControllerClassPackage??> |
| 37 | +import ${superControllerClassPackage}; |
| 38 | +</#if> |
| 39 | + |
| 40 | +/** |
| 41 | +<#if table.comment != ""> |
| 42 | + * ${table.comment!}前端控制器 |
| 43 | + * 注意:该代码生成器只实现了针对单表的新增、更新、删除、查询单个实体对象和分页查询多个实体对象的方法,如果业务较为复杂,不建议使用代码生成器! |
| 44 | + * |
| 45 | +</#if> |
| 46 | +* @author ${author} |
| 47 | +* @since ${date} |
| 48 | +*/ |
| 49 | +<#if restControllerStyle> |
| 50 | +@RestController |
| 51 | +<#else> |
| 52 | +@Controller |
| 53 | +</#if> |
| 54 | +@RequestMapping("/${package.Controller?split(".")?last}<#if package.ModuleName?? && package.ModuleName != "">/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen?replace("-do", "")}<#else>${table.entityPath?replace("DO", "")}</#if>") |
| 55 | +<#if kotlin> |
| 56 | +class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if> |
| 57 | +<#else> |
| 58 | +<#if superControllerClass??> |
| 59 | +public class ${table.controllerName} extends ${superControllerClass} { |
| 60 | +<#else> |
| 61 | +public class ${table.controllerName} { |
| 62 | +</#if> |
| 63 | + |
| 64 | + @Autowired |
| 65 | + private ${table.serviceName} ${table.serviceName?uncap_first}; |
| 66 | + |
| 67 | + <#if package.Controller?split(".")?last == "cms"> |
| 68 | + @LoginRequired |
| 69 | + </#if> |
| 70 | + @PostMapping("") |
| 71 | + public CreatedVO create(@RequestBody ${entity} dto) { |
| 72 | + ${entity} ${entity?uncap_first} = new ${entity}(); |
| 73 | + BeanUtils.copyProperties(dto, ${entity?uncap_first}); |
| 74 | + ${table.serviceName?uncap_first}.save(${entity?uncap_first}); |
| 75 | + return new CreatedVO(); |
| 76 | + } |
| 77 | + |
| 78 | + <#if package.Controller?split(".")?last == "cms"> |
| 79 | + @LoginRequired |
| 80 | + </#if> |
| 81 | + @PutMapping("/{id}") |
| 82 | + public UpdatedVO update( |
| 83 | + @PathVariable @Positive(message = "{id.positive}") Integer id, |
| 84 | + @RequestBody ${entity} dto |
| 85 | + ) { |
| 86 | + get(id); |
| 87 | + dto.setId(id); |
| 88 | + ${table.serviceName?uncap_first}.updateById(dto); |
| 89 | + return new UpdatedVO(); |
| 90 | + } |
| 91 | + |
| 92 | + <#if package.Controller?split(".")?last == "cms"> |
| 93 | + @LoginRequired |
| 94 | + </#if> |
| 95 | + @DeleteMapping("/{id}") |
| 96 | + public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) { |
| 97 | + ${entity} ${entity?uncap_first} = get(id); |
| 98 | + ${table.serviceName?uncap_first}.removeById(${entity?uncap_first}.getId()); |
| 99 | + return new DeletedVO(); |
| 100 | + } |
| 101 | + |
| 102 | + @GetMapping("/{id}") |
| 103 | + <#if package.Controller?split(".")?last == "cms"> |
| 104 | + @LoginRequired |
| 105 | + </#if> |
| 106 | + public ${entity} get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) { |
| 107 | + ${entity} ${entity?uncap_first} = ${table.serviceName?uncap_first}.getById(id); |
| 108 | + if (${entity?uncap_first} == null) { |
| 109 | + throw new NotFoundException(); |
| 110 | + } |
| 111 | + return ${entity?uncap_first}; |
| 112 | + } |
| 113 | + |
| 114 | + @GetMapping("/page") |
| 115 | + <#if package.Controller?split(".")?last == "cms"> |
| 116 | + @LoginRequired |
| 117 | + </#if> |
| 118 | + public PageResponseVO<${entity}> page( |
| 119 | + @RequestParam(name = "page", required = false, defaultValue = "0") |
| 120 | + @Min(value = 0, message = "{page.number.min}") Integer page, |
| 121 | + @RequestParam(name = "count", required = false, defaultValue = "10") |
| 122 | + @Min(value = 1, message = "{page.count.min}") |
| 123 | + @Max(value = 30, message = "{page.count.max}") Integer count |
| 124 | + ) { |
| 125 | + return PageUtil.build(${table.serviceName?uncap_first}.page(new Page<>(page, count), null)); |
| 126 | + } |
| 127 | + |
| 128 | +} |
| 129 | +</#if> |
0 commit comments