Commit 45d203550fbd34893669372313a46358cedba09a
1 parent
59be930f
添加 controller.vm 的模板
Showing
4 changed files
with
140 additions
and
34 deletions
src/main/java/cn/iocoder/dashboard/modules/system/controller/test/SysTestDemoController.java
0 → 100644
| 1 | +package cn.iocoder.dashboard.modules.system.controller.test; | |
| 2 | + | |
| 3 | +import cn.iocoder.dashboard.common.pojo.CommonResult; | |
| 4 | +import cn.iocoder.dashboard.common.pojo.PageResult; | |
| 5 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoCreateReqVO; | |
| 6 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoPageReqVO; | |
| 7 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoRespVO; | |
| 8 | +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoUpdateReqVO; | |
| 9 | +import cn.iocoder.dashboard.modules.system.convert.test.SysTestDemoConvert; | |
| 10 | +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO; | |
| 11 | +import cn.iocoder.dashboard.modules.system.service.test.SysTestDemoService; | |
| 12 | +import io.swagger.annotations.Api; | |
| 13 | +import io.swagger.annotations.ApiImplicitParam; | |
| 14 | +import io.swagger.annotations.ApiOperation; | |
| 15 | +import org.springframework.validation.annotation.Validated; | |
| 16 | +import org.springframework.web.bind.annotation.*; | |
| 17 | + | |
| 18 | +import javax.annotation.Resource; | |
| 19 | +import javax.validation.Valid; | |
| 20 | +import java.util.Collection; | |
| 21 | +import java.util.List; | |
| 22 | + | |
| 23 | +import static cn.iocoder.dashboard.common.pojo.CommonResult.success; | |
| 24 | + | |
| 25 | +@Api(tags = "字典类型") | |
| 26 | +@RestController | |
| 27 | +@RequestMapping("/system/test-demo") | |
| 28 | +@Validated | |
| 29 | +public class SysTestDemoController { | |
| 30 | + | |
| 31 | + @Resource | |
| 32 | + private SysTestDemoService testDemoService; | |
| 33 | + | |
| 34 | + @ApiOperation("创建字典类型") | |
| 35 | + @PostMapping("/create") | |
| 36 | + public CommonResult<Long> createTestDemo(@Valid SysTestDemoCreateReqVO createReqVO) { | |
| 37 | + return success(testDemoService.createTestDemo(createReqVO)); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @ApiOperation("更新字典类型") | |
| 41 | + @PutMapping("/update") | |
| 42 | + public CommonResult<Boolean> updateTestDemo(@Valid SysTestDemoUpdateReqVO updateReqVO) { | |
| 43 | + testDemoService.updateTestDemo(updateReqVO); | |
| 44 | + return success(true); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @ApiOperation("删除字典类型") | |
| 48 | + @DeleteMapping("/delete") | |
| 49 | + @ApiImplicitParam(name = "id", value = "编号", required = true) | |
| 50 | + public CommonResult<Boolean> deleteTestDemo(@RequestParam("id") Long id) { | |
| 51 | + testDemoService.deleteTestDemo(id); | |
| 52 | + return success(true); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @GetMapping("/get") | |
| 56 | + @ApiOperation("获得字典类型") | |
| 57 | + @ApiImplicitParam(name = "id", value = "编号", required = true) | |
| 58 | + public CommonResult<SysTestDemoRespVO> getTestDemo(@RequestParam("id") Long id) { | |
| 59 | + SysTestDemoDO testDemo = testDemoService.getTestDemo(id); | |
| 60 | + return success(SysTestDemoConvert.INSTANCE.convert(testDemo)); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @GetMapping("/list") | |
| 64 | + @ApiOperation("获得字典类型列表") | |
| 65 | + @ApiImplicitParam(name = "ids", value = "编号列表", required = true) | |
| 66 | + public CommonResult<List<SysTestDemoRespVO>> getTestDemoList(@RequestParam("ids") Collection<Long> ids) { | |
| 67 | + List<SysTestDemoDO> list = testDemoService.getTestDemoList(ids); | |
| 68 | + return success(SysTestDemoConvert.INSTANCE.convertList(list)); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @ApiOperation("获得字典类型分页") | |
| 72 | + @GetMapping("/page") | |
| 73 | + public CommonResult<PageResult<SysTestDemoRespVO>> getTestDemoPage(@Valid SysTestDemoPageReqVO pageVO) { | |
| 74 | + PageResult<SysTestDemoDO> pageResult = testDemoService.getTestDemoPage(pageVO); | |
| 75 | + return success(SysTestDemoConvert.INSTANCE.convertPage(pageResult)); | |
| 76 | + } | |
| 77 | + | |
| 78 | +} | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.hutool.extra.template.TemplateConfig; |
| 4 | 4 | import cn.hutool.extra.template.TemplateEngine; |
| 5 | 5 | import cn.hutool.extra.template.TemplateUtil; |
| 6 | 6 | import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; |
| 7 | +import cn.iocoder.dashboard.common.pojo.CommonResult; | |
| 7 | 8 | import cn.iocoder.dashboard.common.pojo.PageParam; |
| 8 | 9 | import cn.iocoder.dashboard.common.pojo.PageResult; |
| 9 | 10 | import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; |
| ... | ... | @@ -58,6 +59,7 @@ public class ToolCodegenEngine { |
| 58 | 59 | // 全局配置 |
| 59 | 60 | globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包, 抽成参数 |
| 60 | 61 | // 全局 Java Bean |
| 62 | + globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); | |
| 61 | 63 | globalBindingMap.put("PageResultClassName", PageResult.class.getName()); |
| 62 | 64 | globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName()); |
| 63 | 65 | globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName()); |
| ... | ... | @@ -76,6 +78,7 @@ public class ToolCodegenEngine { |
| 76 | 78 | bindingMap.put("table", table); |
| 77 | 79 | bindingMap.put("columns", columns); |
| 78 | 80 | bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); // 主键字段 |
| 81 | + // moduleName 相关 | |
| 79 | 82 | String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName()); |
| 80 | 83 | bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys |
| 81 | 84 | // className 相关 |
| ... | ... | @@ -84,6 +87,7 @@ public class ToolCodegenEngine { |
| 84 | 87 | bindingMap.put("simpleClassName", simpleClassName); |
| 85 | 88 | bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type |
| 86 | 89 | bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType,用于变量 |
| 90 | + bindingMap.put("simpleClassName_strikeCase", toSymbolCase(simpleClassName, '-')); // 将 DictType 转换成 dict-type | |
| 87 | 91 | // 执行生成 |
| 88 | 92 | // String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap); |
| 89 | 93 | // String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap); |
| ... | ... | @@ -95,7 +99,8 @@ public class ToolCodegenEngine { |
| 95 | 99 | // String result = templateEngine.getTemplate("codegen/convert/convert.vm").render(bindingMap); |
| 96 | 100 | // String result = templateEngine.getTemplate("codegen/enums/errorcode.vm").render(bindingMap); |
| 97 | 101 | // String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap); |
| 98 | - String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap); | |
| 102 | +// String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap); | |
| 103 | + String result = templateEngine.getTemplate("codegen/controller/controller.vm").render(bindingMap); | |
| 99 | 104 | System.out.println(result); |
| 100 | 105 | } |
| 101 | 106 | ... | ... |
src/main/resources/codegen/controller/controller.vm
| 1 | +package ${basePackage}.${table.moduleName}.controller.${table.businessName}; | |
| 2 | + | |
| 1 | 3 | import org.springframework.web.bind.annotation.*; |
| 2 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 4 | +import javax.annotation.Resource; | |
| 5 | +import org.springframework.validation.annotation.Validated; | |
| 6 | + | |
| 3 | 7 | import io.swagger.annotations.*; |
| 8 | + | |
| 4 | 9 | import javax.validation.constraints.*; |
| 5 | 10 | import javax.validation.*; |
| 6 | 11 | import java.util.*; |
| 7 | 12 | |
| 8 | -import org.springframework.validation.annotation.Validated; | |
| 13 | +import ${PageResultClassName}; | |
| 14 | +import ${CommonResultClassName}; | |
| 15 | +import static ${CommonResultClassName}.success; | |
| 16 | + | |
| 17 | +import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; | |
| 18 | +import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; | |
| 19 | +import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert; | |
| 20 | +import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service; | |
| 9 | 21 | |
| 10 | -/** | |
| 11 | - * ${class.description} Controller | |
| 12 | - */ | |
| 22 | +@Api(tags = "${table.classComment}") | |
| 13 | 23 | @RestController |
| 14 | -@RequestMapping("/${class.classNameLowerUnderscore}") | |
| 15 | -@Api(tags = "${class.description}") | |
| 24 | +##二级的 businessName 暂时不算在 HTTP 路径上,可以根据需要写 | |
| 25 | +@RequestMapping("/${table.moduleName}/${simpleClassName_strikeCase}") | |
| 16 | 26 | @Validated |
| 17 | -public class ${class.className}Controller { | |
| 27 | +public class ${table.className}Controller { | |
| 18 | 28 | |
| 19 | - @Autowired | |
| 20 | - private ${class.className}Service ${class.classNameVar}Service; | |
| 29 | + @Resource | |
| 30 | + private ${table.className}Service ${classNameVar}Service; | |
| 21 | 31 | |
| 32 | + @ApiOperation("创建${table.classComment}") | |
| 22 | 33 | @PostMapping("/create") |
| 23 | - @ApiOperation("创建${class.description}") | |
| 24 | - public CommonResult<Integer> create${class.className}(@Valid ${class.className}CreateReqVO createVO) { | |
| 25 | - return success(${class.classNameVar}Service.create${class.className}(createVO)); | |
| 34 | + public CommonResult<${primaryColumn.javaType}> create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO) { | |
| 35 | + return success(${classNameVar}Service.create${simpleClassName}(createReqVO)); | |
| 26 | 36 | } |
| 27 | 37 | |
| 28 | - @PostMapping("/update") | |
| 29 | - @ApiOperation("更新${class.description}") | |
| 30 | - public CommonResult<Boolean> update${class.className}(@Valid ${class.className}UpdateReqVO updateVO) { | |
| 31 | - ${class.classNameVar}Service.update${class.className}(updateVO); | |
| 38 | + @ApiOperation("更新${table.classComment}") | |
| 39 | + @PutMapping("/update") | |
| 40 | + public CommonResult<Boolean> update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO) { | |
| 41 | + ${classNameVar}Service.update${simpleClassName}(updateReqVO); | |
| 32 | 42 | return success(true); |
| 33 | 43 | } |
| 34 | 44 | |
| 35 | - @PostMapping("/delete") | |
| 36 | - @ApiOperation("删除${class.description}") | |
| 37 | - @ApiImplicitParam(name = "${class.classNameVar}Id", value = "${class.description}编号", required = true) | |
| 38 | - public CommonResult<Boolean> delete${class.className}(@RequestParam("${class.classNameVar}Id") Integer ${class.classNameVar}Id) { | |
| 39 | - ${class.classNameVar}Service.delete${class.className}(${class.classNameVar}Id); | |
| 45 | + @ApiOperation("删除${table.classComment}") | |
| 46 | + @DeleteMapping("/delete") | |
| 47 | + @ApiImplicitParam(name = "id", value = "编号", required = true) | |
| 48 | + public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) { | |
| 49 | + ${classNameVar}Service.delete${simpleClassName}(id); | |
| 40 | 50 | return success(true); |
| 41 | 51 | } |
| 42 | 52 | |
| 43 | 53 | @GetMapping("/get") |
| 44 | - @ApiOperation("获得${class.description}") | |
| 45 | - @ApiImplicitParam(name = "${class.classNameVar}Id", value = "${class.description}编号", required = true) | |
| 46 | - public CommonResult<${class.className}RespVO> get${class.className}(@RequestParam("${class.classNameVar}Id") Integer ${class.classNameVar}Id) { | |
| 47 | - return success(${class.classNameVar}Service.get${class.className}(${class.classNameVar}Id)); | |
| 54 | + @ApiOperation("获得${table.classComment}") | |
| 55 | + @ApiImplicitParam(name = "id", value = "编号", required = true) | |
| 56 | + public CommonResult<${table.className}RespVO> get${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) { | |
| 57 | + ${table.className}DO ${classNameVar} = ${classNameVar}Service.get${simpleClassName}(id); | |
| 58 | + return success(${table.className}Convert.INSTANCE.convert(${classNameVar})); | |
| 48 | 59 | } |
| 49 | 60 | |
| 50 | 61 | @GetMapping("/list") |
| 51 | - @ApiOperation("获得${class.description}列表") | |
| 52 | - @ApiImplicitParam(name = "${class.classNameVar}Ids", value = "${class.description}编号列表", required = true) | |
| 53 | - public CommonResult<List<${class.className}RespVO>> list${class.className}s(@RequestParam("${class.classNameVar}Ids") List<Integer> ${class.classNameVar}Ids) { | |
| 54 | - return success(${class.classNameVar}Service.list${class.className}s(${class.classNameVar}Ids)); | |
| 62 | + @ApiOperation("获得${table.classComment}列表") | |
| 63 | + @ApiImplicitParam(name = "ids", value = "编号列表", required = true) | |
| 64 | + public CommonResult<List<${table.className}RespVO>> get${simpleClassName}List(@RequestParam("ids") Collection<${primaryColumn.javaType}> ids) { | |
| 65 | + List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(ids); | |
| 66 | + return success(${table.className}Convert.INSTANCE.convertList(list)); | |
| 55 | 67 | } |
| 56 | 68 | |
| 69 | + @ApiOperation("获得${table.classComment}分页") | |
| 57 | 70 | @GetMapping("/page") |
| 58 | - @ApiOperation("获得${class.description}分页") | |
| 59 | - public CommonResult<PageResult<${class.className}RespVO>> page${class.className}(${class.className}PageReqVO pageVO) { | |
| 60 | - return success(${class.classNameVar}Service.page${class.className}(pageVO)); | |
| 71 | + public CommonResult<PageResult<${table.className}RespVO>> get${simpleClassName}Page(@Valid ${table.className}PageReqVO pageVO) { | |
| 72 | + PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageVO); | |
| 73 | + return success(${table.className}Convert.INSTANCE.convertPage(pageResult)); | |
| 61 | 74 | } |
| 62 | 75 | |
| 63 | 76 | } | ... | ... |
src/main/resources/codegen/convert/convert.vm
| 1 | 1 | package ${basePackage}.${table.moduleName}.convert.${table.businessName}; |
| 2 | 2 | |
| 3 | +import java.util.*; | |
| 4 | + | |
| 5 | +import ${PageResultClassName}; | |
| 6 | + | |
| 3 | 7 | import org.mapstruct.Mapper; |
| 4 | 8 | import org.mapstruct.factory.Mappers; |
| 5 | 9 | import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; |
| ... | ... | @@ -19,4 +23,10 @@ public interface ${table.className}Convert { |
| 19 | 23 | |
| 20 | 24 | ${table.className}DO convert(${table.className}UpdateReqVO bean); |
| 21 | 25 | |
| 26 | + ${table.className}RespVO convert(${table.className}DO bean); | |
| 27 | + | |
| 28 | + List<${table.className}RespVO> convertList(List<${table.className}DO> list); | |
| 29 | + | |
| 30 | + PageResult<${table.className}RespVO> convertPage(PageResult<${table.className}DO> page); | |
| 31 | + | |
| 22 | 32 | } | ... | ... |