浏览代码

添加 controller.vm 的模板

YunaiV 4 年之前
父节点
当前提交
45d203550f

+ 78 - 0
src/main/java/cn/iocoder/dashboard/modules/system/controller/test/SysTestDemoController.java

@@ -0,0 +1,78 @@
+package cn.iocoder.dashboard.modules.system.controller.test;
+
+import cn.iocoder.dashboard.common.pojo.CommonResult;
+import cn.iocoder.dashboard.common.pojo.PageResult;
+import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoCreateReqVO;
+import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoPageReqVO;
+import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoRespVO;
+import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoUpdateReqVO;
+import cn.iocoder.dashboard.modules.system.convert.test.SysTestDemoConvert;
+import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO;
+import cn.iocoder.dashboard.modules.system.service.test.SysTestDemoService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import javax.validation.Valid;
+import java.util.Collection;
+import java.util.List;
+
+import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
+
+@Api(tags = "字典类型")
+@RestController
+@RequestMapping("/system/test-demo")
+@Validated
+public class SysTestDemoController {
+
+    @Resource
+    private SysTestDemoService testDemoService;
+
+    @ApiOperation("创建字典类型")
+    @PostMapping("/create")
+    public CommonResult<Long> createTestDemo(@Valid SysTestDemoCreateReqVO createReqVO) {
+        return success(testDemoService.createTestDemo(createReqVO));
+    }
+
+    @ApiOperation("更新字典类型")
+    @PutMapping("/update")
+    public CommonResult<Boolean> updateTestDemo(@Valid SysTestDemoUpdateReqVO updateReqVO) {
+        testDemoService.updateTestDemo(updateReqVO);
+        return success(true);
+    }
+
+	@ApiOperation("删除字典类型")
+	@DeleteMapping("/delete")
+    @ApiImplicitParam(name = "id", value = "编号", required = true)
+    public CommonResult<Boolean> deleteTestDemo(@RequestParam("id") Long id) {
+        testDemoService.deleteTestDemo(id);
+        return success(true);
+    }
+
+    @GetMapping("/get")
+    @ApiOperation("获得字典类型")
+    @ApiImplicitParam(name = "id", value = "编号", required = true)
+    public CommonResult<SysTestDemoRespVO> getTestDemo(@RequestParam("id") Long id) {
+        SysTestDemoDO testDemo = testDemoService.getTestDemo(id);
+        return success(SysTestDemoConvert.INSTANCE.convert(testDemo));
+    }
+
+    @GetMapping("/list")
+    @ApiOperation("获得字典类型列表")
+    @ApiImplicitParam(name = "ids", value = "编号列表", required = true)
+    public CommonResult<List<SysTestDemoRespVO>> getTestDemoList(@RequestParam("ids") Collection<Long> ids) {
+        List<SysTestDemoDO> list = testDemoService.getTestDemoList(ids);
+        return success(SysTestDemoConvert.INSTANCE.convertList(list));
+    }
+
+    @ApiOperation("获得字典类型分页")
+    @GetMapping("/page")
+    public CommonResult<PageResult<SysTestDemoRespVO>> getTestDemoPage(@Valid SysTestDemoPageReqVO pageVO) {
+        PageResult<SysTestDemoDO> pageResult = testDemoService.getTestDemoPage(pageVO);
+        return success(SysTestDemoConvert.INSTANCE.convertPage(pageResult));
+    }
+
+}

+ 6 - 1
src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java

@@ -4,6 +4,7 @@ import cn.hutool.extra.template.TemplateConfig;
 import cn.hutool.extra.template.TemplateEngine;
 import cn.hutool.extra.template.TemplateUtil;
 import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
+import cn.iocoder.dashboard.common.pojo.CommonResult;
 import cn.iocoder.dashboard.common.pojo.PageParam;
 import cn.iocoder.dashboard.common.pojo.PageResult;
 import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
@@ -58,6 +59,7 @@ public class ToolCodegenEngine {
         // 全局配置
         globalBindingMap.put("basePackage", "cn.iocoder.dashboard.modules"); // TODO 基础包, 抽成参数
         // 全局 Java Bean
+        globalBindingMap.put("CommonResultClassName", CommonResult.class.getName());
         globalBindingMap.put("PageResultClassName", PageResult.class.getName());
         globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName());
         globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName());
@@ -76,6 +78,7 @@ public class ToolCodegenEngine {
         bindingMap.put("table", table);
         bindingMap.put("columns", columns);
         bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); // 主键字段
+        // moduleName 相关
         String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName());
         bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys
         // className 相关
@@ -84,6 +87,7 @@ public class ToolCodegenEngine {
         bindingMap.put("simpleClassName", simpleClassName);
         bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type
         bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType,用于变量
+        bindingMap.put("simpleClassName_strikeCase", toSymbolCase(simpleClassName, '-')); // 将 DictType 转换成 dict-type
         // 执行生成
 //        String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap);
 //        String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap);
@@ -95,7 +99,8 @@ public class ToolCodegenEngine {
 //        String result = templateEngine.getTemplate("codegen/convert/convert.vm").render(bindingMap);
 //        String result = templateEngine.getTemplate("codegen/enums/errorcode.vm").render(bindingMap);
 //        String result = templateEngine.getTemplate("codegen/service/service.vm").render(bindingMap);
-        String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap);
+//        String result = templateEngine.getTemplate("codegen/service/serviceImpl.vm").render(bindingMap);
+        String result = templateEngine.getTemplate("codegen/controller/controller.vm").render(bindingMap);
         System.out.println(result);
     }
 

+ 46 - 33
src/main/resources/codegen/controller/controller.vm

@@ -1,63 +1,76 @@
+package ${basePackage}.${table.moduleName}.controller.${table.businessName};
+
 import org.springframework.web.bind.annotation.*;
-import org.springframework.beans.factory.annotation.Autowired;
+import javax.annotation.Resource;
+import org.springframework.validation.annotation.Validated;
+
 import io.swagger.annotations.*;
+
 import javax.validation.constraints.*;
 import javax.validation.*;
 import java.util.*;
 
-import org.springframework.validation.annotation.Validated;
+import ${PageResultClassName};
+import ${CommonResultClassName};
+import static ${CommonResultClassName}.success;
+
+import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
+import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO;
+import ${basePackage}.${table.moduleName}.convert.${table.businessName}.${table.className}Convert;
+import ${basePackage}.${table.moduleName}.service.${table.businessName}.${table.className}Service;
 
-/**
- * ${class.description} Controller
- */
+@Api(tags = "${table.classComment}")
 @RestController
-@RequestMapping("/${class.classNameLowerUnderscore}")
-@Api(tags = "${class.description}")
+##二级的 businessName 暂时不算在 HTTP 路径上,可以根据需要写
+@RequestMapping("/${table.moduleName}/${simpleClassName_strikeCase}")
 @Validated
-public class ${class.className}Controller {
+public class ${table.className}Controller {
 
-    @Autowired
-    private ${class.className}Service ${class.classNameVar}Service;
+    @Resource
+    private ${table.className}Service ${classNameVar}Service;
 
+    @ApiOperation("创建${table.classComment}")
     @PostMapping("/create")
-    @ApiOperation("创建${class.description}")
-    public CommonResult<Integer> create${class.className}(@Valid ${class.className}CreateReqVO createVO) {
-        return success(${class.classNameVar}Service.create${class.className}(createVO));
+    public CommonResult<${primaryColumn.javaType}> create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO) {
+        return success(${classNameVar}Service.create${simpleClassName}(createReqVO));
     }
 
-    @PostMapping("/update")
-    @ApiOperation("更新${class.description}")
-    public CommonResult<Boolean> update${class.className}(@Valid ${class.className}UpdateReqVO updateVO) {
-        ${class.classNameVar}Service.update${class.className}(updateVO);
+    @ApiOperation("更新${table.classComment}")
+    @PutMapping("/update")
+    public CommonResult<Boolean> update${simpleClassName}(@Valid ${table.className}UpdateReqVO updateReqVO) {
+        ${classNameVar}Service.update${simpleClassName}(updateReqVO);
         return success(true);
     }
 
-    @PostMapping("/delete")
-    @ApiOperation("删除${class.description}")
-    @ApiImplicitParam(name = "${class.classNameVar}Id", value = "${class.description}编号", required = true)
-    public CommonResult<Boolean> delete${class.className}(@RequestParam("${class.classNameVar}Id") Integer ${class.classNameVar}Id) {
-        ${class.classNameVar}Service.delete${class.className}(${class.classNameVar}Id);
+	@ApiOperation("删除${table.classComment}")
+	@DeleteMapping("/delete")
+    @ApiImplicitParam(name = "id", value = "编号", required = true)
+    public CommonResult<Boolean> delete${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) {
+        ${classNameVar}Service.delete${simpleClassName}(id);
         return success(true);
     }
 
     @GetMapping("/get")
-    @ApiOperation("获得${class.description}")
-    @ApiImplicitParam(name = "${class.classNameVar}Id", value = "${class.description}编号", required = true)
-    public CommonResult<${class.className}RespVO> get${class.className}(@RequestParam("${class.classNameVar}Id") Integer ${class.classNameVar}Id) {
-        return success(${class.classNameVar}Service.get${class.className}(${class.classNameVar}Id));
+    @ApiOperation("获得${table.classComment}")
+    @ApiImplicitParam(name = "id", value = "编号", required = true)
+    public CommonResult<${table.className}RespVO> get${simpleClassName}(@RequestParam("id") ${primaryColumn.javaType} id) {
+        ${table.className}DO ${classNameVar} = ${classNameVar}Service.get${simpleClassName}(id);
+        return success(${table.className}Convert.INSTANCE.convert(${classNameVar}));
     }
 
     @GetMapping("/list")
-    @ApiOperation("获得${class.description}列表")
-    @ApiImplicitParam(name = "${class.classNameVar}Ids", value = "${class.description}编号列表", required = true)
-    public CommonResult<List<${class.className}RespVO>> list${class.className}s(@RequestParam("${class.classNameVar}Ids") List<Integer> ${class.classNameVar}Ids) {
-        return success(${class.classNameVar}Service.list${class.className}s(${class.classNameVar}Ids));
+    @ApiOperation("获得${table.classComment}列表")
+    @ApiImplicitParam(name = "ids", value = "编号列表", required = true)
+    public CommonResult<List<${table.className}RespVO>> get${simpleClassName}List(@RequestParam("ids") Collection<${primaryColumn.javaType}> ids) {
+        List<${table.className}DO> list = ${classNameVar}Service.get${simpleClassName}List(ids);
+        return success(${table.className}Convert.INSTANCE.convertList(list));
     }
 
+    @ApiOperation("获得${table.classComment}分页")
     @GetMapping("/page")
-    @ApiOperation("获得${class.description}分页")
-    public CommonResult<PageResult<${class.className}RespVO>> page${class.className}(${class.className}PageReqVO pageVO) {
-        return success(${class.classNameVar}Service.page${class.className}(pageVO));
+    public CommonResult<PageResult<${table.className}RespVO>> get${simpleClassName}Page(@Valid ${table.className}PageReqVO pageVO) {
+        PageResult<${table.className}DO> pageResult = ${classNameVar}Service.get${simpleClassName}Page(pageVO);
+        return success(${table.className}Convert.INSTANCE.convertPage(pageResult));
     }
 
 }

+ 10 - 0
src/main/resources/codegen/convert/convert.vm

@@ -1,5 +1,9 @@
 package ${basePackage}.${table.moduleName}.convert.${table.businessName};
 
+import java.util.*;
+
+import ${PageResultClassName};
+
 import org.mapstruct.Mapper;
 import org.mapstruct.factory.Mappers;
 import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*;
@@ -19,4 +23,10 @@ public interface ${table.className}Convert {
 
     ${table.className}DO convert(${table.className}UpdateReqVO bean);
 
+    ${table.className}RespVO convert(${table.className}DO bean);
+
+    List<${table.className}RespVO> convertList(List<${table.className}DO> list);
+
+	PageResult<${table.className}RespVO> convertPage(PageResult<${table.className}DO> page);
+
 }