From 5442f6522e5e25fc2472626470bb7877ddffe06f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 6 Feb 2021 23:52:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=20vm=20=E6=A8=A1=E6=9D=BF=20?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=20test=20=E7=94=9F=E6=88=90=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/controller/GenController.java | 47 ++++----- .../com/ruoyi/generator/domain/GenTable.java | 32 ------- .../main/resources/vm/java/service.java.vm | 61 ------------ .../src/main/resources/vm/xml/mapper.xml.vm | 95 ------------------- .../modules/infra/convert/package-info.java | 1 - .../system/controller/test/package-info.java | 1 + .../controller/test/vo/SysTestDemoBaseVO.java | 30 ++++++ .../test/vo/SysTestDemoCreateReqVO.java | 14 +++ .../test/vo/SysTestDemoPageReqVO.java | 41 ++++++++ .../controller/test/vo/SysTestDemoRespVO.java | 19 ++++ .../test/vo/SysTestDemoUpdateReqVO.java | 21 ++++ .../dal/mysql/dao/test/SysTestDemoMapper.java | 28 ++++++ .../mysql/dataobject/test/SysTestDemoDO.java | 44 +++++++++ .../service/test/SysTestDemoService.java | 66 +++++++++++++ .../codegen/impl/ToolCodegenBuilder.java | 56 +++++++++-- .../codegen/impl/ToolCodegenEngine.java | 19 +++- .../codegen/controller/vo/updateReqVO.vm | 4 +- src/main/resources/codegen/convert/convert.vm | 22 +++++ src/main/resources/codegen/dal/do.vm | 2 +- src/main/resources/codegen/dal/mapper.vm | 11 ++- src/main/resources/codegen/enums/errorcode.vm | 2 + src/main/resources/codegen/service/service.vm | 8 +- .../resources/codegen/service/serviceImpl.vm | 30 ++++++ .../codegen/impl/ToolCodegenEngineTest.java | 2 +- .../impl/ToolCodegenServiceImplTest.java | 2 +- 25 files changed, 412 insertions(+), 246 deletions(-) delete mode 100644 ruoyi-generator/src/main/resources/vm/java/service.java.vm delete mode 100644 ruoyi-generator/src/main/resources/vm/xml/mapper.xml.vm delete mode 100644 src/main/java/cn/iocoder/dashboard/modules/infra/convert/package-info.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/test/package-info.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoBaseVO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoCreateReqVO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoPageReqVO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoRespVO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoUpdateReqVO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/test/SysTestDemoMapper.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/test/SysTestDemoDO.java create mode 100644 src/main/java/cn/iocoder/dashboard/modules/system/service/test/SysTestDemoService.java create mode 100644 src/main/resources/codegen/convert/convert.vm create mode 100644 src/main/resources/codegen/enums/errorcode.vm create mode 100644 src/main/resources/codegen/service/serviceImpl.vm diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java index 9de4d4472..6199d50c8 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/controller/GenController.java @@ -5,6 +5,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; + import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; @@ -30,13 +31,12 @@ import com.ruoyi.generator.service.IGenTableService; /** * 代码生成 操作处理 - * + * * @author ruoyi */ @RestController @RequestMapping("/tool/gen") -public class GenController extends BaseController -{ +public class GenController extends BaseController { @Autowired private IGenTableService genTableService; @@ -48,8 +48,7 @@ public class GenController extends BaseController */ @PreAuthorize("@ss.hasPermi('tool:gen:list')") @GetMapping("/list") - public TableDataInfo genList(GenTable genTable) - { + public TableDataInfo genList(GenTable genTable) { startPage(); List list = genTableService.selectGenTableList(genTable); return getDataTable(list); @@ -60,8 +59,7 @@ public class GenController extends BaseController */ @PreAuthorize("@ss.hasPermi('tool:gen:query')") @GetMapping(value = "/{talbleId}") - public AjaxResult getInfo(@PathVariable Long talbleId) - { + public AjaxResult getInfo(@PathVariable Long talbleId) { GenTable table = genTableService.selectGenTableById(talbleId); List list = genTableColumnService.selectGenTableColumnListByTableId(talbleId); Map map = new HashMap(); @@ -75,8 +73,7 @@ public class GenController extends BaseController */ @PreAuthorize("@ss.hasPermi('tool:gen:list')") @GetMapping("/db/list") - public TableDataInfo dataList(GenTable genTable) - { + public TableDataInfo dataList(GenTable genTable) { startPage(); List list = genTableService.selectDbTableList(genTable); return getDataTable(list); @@ -87,8 +84,7 @@ public class GenController extends BaseController */ @PreAuthorize("@ss.hasPermi('tool:gen:list')") @GetMapping(value = "/column/{talbleId}") - public TableDataInfo columnList(Long tableId) - { + public TableDataInfo columnList(Long tableId) { TableDataInfo dataInfo = new TableDataInfo(); List list = genTableColumnService.selectGenTableColumnListByTableId(tableId); dataInfo.setRows(list); @@ -102,8 +98,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:list')") @Log(title = "代码生成", businessType = BusinessType.IMPORT) @PostMapping("/importTable") - public AjaxResult importTableSave(String tables) - { + public AjaxResult importTableSave(String tables) { String[] tableNames = Convert.toStrArray(tables); // 查询表信息 List tableList = genTableService.selectDbTableListByNames(tableNames); @@ -117,8 +112,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:edit')") @Log(title = "代码生成", businessType = BusinessType.UPDATE) @PutMapping - public AjaxResult editSave(@Validated @RequestBody GenTable genTable) - { + public AjaxResult editSave(@Validated @RequestBody GenTable genTable) { genTableService.validateEdit(genTable); genTableService.updateGenTable(genTable); return AjaxResult.success(); @@ -130,8 +124,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:remove')") @Log(title = "代码生成", businessType = BusinessType.DELETE) @DeleteMapping("/{tableIds}") - public AjaxResult remove(@PathVariable Long[] tableIds) - { + public AjaxResult remove(@PathVariable Long[] tableIds) { genTableService.deleteGenTableByIds(tableIds); return AjaxResult.success(); } @@ -141,8 +134,7 @@ public class GenController extends BaseController */ @PreAuthorize("@ss.hasPermi('tool:gen:preview')") @GetMapping("/preview/{tableId}") - public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException - { + public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException { Map dataMap = genTableService.previewCode(tableId); return AjaxResult.success(dataMap); } @@ -153,8 +145,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:code')") @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/download/{tableName}") - public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException - { + public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException { byte[] data = genTableService.downloadCode(tableName); genCode(response, data); } @@ -165,8 +156,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:code')") @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/genCode/{tableName}") - public AjaxResult genCode(@PathVariable("tableName") String tableName) - { + public AjaxResult genCode(@PathVariable("tableName") String tableName) { genTableService.generatorCode(tableName); return AjaxResult.success(); } @@ -177,8 +167,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:edit')") @Log(title = "代码生成", businessType = BusinessType.UPDATE) @GetMapping("/synchDb/{tableName}") - public AjaxResult synchDb(@PathVariable("tableName") String tableName) - { + public AjaxResult synchDb(@PathVariable("tableName") String tableName) { genTableService.synchDb(tableName); return AjaxResult.success(); } @@ -189,8 +178,7 @@ public class GenController extends BaseController @PreAuthorize("@ss.hasPermi('tool:gen:code')") @Log(title = "代码生成", businessType = BusinessType.GENCODE) @GetMapping("/batchGenCode") - public void batchGenCode(HttpServletResponse response, String tables) throws IOException - { + public void batchGenCode(HttpServletResponse response, String tables) throws IOException { String[] tableNames = Convert.toStrArray(tables); byte[] data = genTableService.downloadCode(tableNames); genCode(response, data); @@ -199,8 +187,7 @@ public class GenController extends BaseController /** * 生成zip文件 */ - private void genCode(HttpServletResponse response, byte[] data) throws IOException - { + private void genCode(HttpServletResponse response, byte[] data) throws IOException { response.reset(); response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Expose-Headers", "Content-Disposition"); @@ -209,4 +196,4 @@ public class GenController extends BaseController response.setContentType("application/octet-stream; charset=UTF-8"); IOUtils.write(data, response.getOutputStream()); } -} \ No newline at end of file +} diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java index 287faa55e..f005b41af 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/domain/GenTable.java @@ -22,11 +22,6 @@ public class GenTable extends BaseEntity { @NotBlank(message = "生成包路径不能为空") private String packageName; - /** - * 主键信息 - */ - private GenTableColumn pkColumn; - /** * 其它生成选项 */ @@ -57,31 +52,4 @@ public class GenTable extends BaseEntity { */ private String parentMenuName; - public boolean isTree() { - return isTree(this.tplCategory); - } - - public static boolean isTree(String tplCategory) { - return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory); - } - - public boolean isCrud() { - return isCrud(this.tplCategory); - } - - public static boolean isCrud(String tplCategory) { - return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory); - } - - public boolean isSuperColumn(String javaField) { - return isSuperColumn(this.tplCategory, javaField); - } - - public static boolean isSuperColumn(String tplCategory, String javaField) { - if (isTree(tplCategory)) { - return StringUtils.equalsAnyIgnoreCase(javaField, - ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY)); - } - return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY); - } } diff --git a/ruoyi-generator/src/main/resources/vm/java/service.java.vm b/ruoyi-generator/src/main/resources/vm/java/service.java.vm deleted file mode 100644 index af422f342..000000000 --- a/ruoyi-generator/src/main/resources/vm/java/service.java.vm +++ /dev/null @@ -1,61 +0,0 @@ -package ${packageName}.service; - -import java.util.List; -import ${packageName}.domain.${ClassName}; - -/** - * ${functionName}Service接口 - * - * @author ${author} - * @date ${datetime} - */ -public interface I${ClassName}Service -{ - /** - * 查询${functionName} - * - * @param ${pkColumn.javaField} ${functionName}ID - * @return ${functionName} - */ - public ${ClassName} select${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}); - - /** - * 查询${functionName}列表 - * - * @param ${className} ${functionName} - * @return ${functionName}集合 - */ - public List<${ClassName}> select${ClassName}List(${ClassName} ${className}); - - /** - * 新增${functionName} - * - * @param ${className} ${functionName} - * @return 结果 - */ - public int insert${ClassName}(${ClassName} ${className}); - - /** - * 修改${functionName} - * - * @param ${className} ${functionName} - * @return 结果 - */ - public int update${ClassName}(${ClassName} ${className}); - - /** - * 批量删除${functionName} - * - * @param ${pkColumn.javaField}s 需要删除的${functionName}ID - * @return 结果 - */ - public int delete${ClassName}ByIds(${pkColumn.javaType}[] ${pkColumn.javaField}s); - - /** - * 删除${functionName}信息 - * - * @param ${pkColumn.javaField} ${functionName}ID - * @return 结果 - */ - public int delete${ClassName}ById(${pkColumn.javaType} ${pkColumn.javaField}); -} diff --git a/ruoyi-generator/src/main/resources/vm/xml/mapper.xml.vm b/ruoyi-generator/src/main/resources/vm/xml/mapper.xml.vm deleted file mode 100644 index 6c490a4b4..000000000 --- a/ruoyi-generator/src/main/resources/vm/xml/mapper.xml.vm +++ /dev/null @@ -1,95 +0,0 @@ - - - - - -#foreach ($column in $columns) - -#end - - - - select#foreach($column in $columns) $column.columnName#if($velocityCount != $columns.size()),#end#end from ${tableName} - - - - - - - - insert into ${tableName} - -#foreach($column in $columns) -#if($column.columnName != $pkColumn.columnName || !$pkColumn.increment) - $column.columnName, -#end -#end - - -#foreach($column in $columns) -#if($column.columnName != $pkColumn.columnName || !$pkColumn.increment) - #{$column.javaField}, -#end -#end - - - - - update ${tableName} - -#foreach($column in $columns) -#if($column.columnName != $pkColumn.columnName) - $column.columnName = #{$column.javaField}, -#end -#end - - where ${pkColumn.columnName} = #{${pkColumn.javaField}} - - - - delete from ${tableName} where ${pkColumn.columnName} = #{${pkColumn.javaField}} - - - - delete from ${tableName} where ${pkColumn.columnName} in - - #{${pkColumn.javaField}} - - - - \ No newline at end of file diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/convert/package-info.java b/src/main/java/cn/iocoder/dashboard/modules/infra/convert/package-info.java deleted file mode 100644 index 7479ece0e..000000000 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/convert/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package cn.iocoder.dashboard.modules.infra.convert; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/package-info.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/package-info.java new file mode 100644 index 000000000..93a04d264 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.dashboard.modules.system.controller.test; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoBaseVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoBaseVO.java new file mode 100644 index 000000000..72cad4bde --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoBaseVO.java @@ -0,0 +1,30 @@ +package cn.iocoder.dashboard.modules.system.controller.test.vo; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +/** +* 字典类型 Base VO,提供给添加、修改、详细的子 VO 使用 +* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 +*/ +@Data +public class SysTestDemoBaseVO { + + @ApiModelProperty(value = "字典名称", required = true, example = "性别额") + @NotNull(message = "字典名称不能为空") + private String name; + + @ApiModelProperty(value = "字典类型", required = true, example = "sys_sex") + @NotNull(message = "字典类型不能为空") + private String dictType; + + @ApiModelProperty(value = "状态", required = true, example = "1") + @NotNull(message = "状态不能为空") + private Integer status; + + @ApiModelProperty(value = "备注", example = "我是备注") + private String remark; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoCreateReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoCreateReqVO.java new file mode 100644 index 000000000..112822987 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoCreateReqVO.java @@ -0,0 +1,14 @@ +package cn.iocoder.dashboard.modules.system.controller.test.vo; + +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +@ApiModel("字典类型创建 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTestDemoCreateReqVO extends SysTestDemoBaseVO { + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoPageReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoPageReqVO.java new file mode 100644 index 000000000..8818bdbcf --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoPageReqVO.java @@ -0,0 +1,41 @@ +package cn.iocoder.dashboard.modules.system.controller.test.vo; + +import cn.iocoder.dashboard.common.pojo.PageParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +import static cn.iocoder.dashboard.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel("字典类型分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTestDemoPageReqVO extends PageParam { + + @ApiModelProperty(value = "字典名称", example = "性别额") + private String name; + + @ApiModelProperty(value = "字典类型", example = "sys_sex") + private String dictType; + + @ApiModelProperty(value = "状态", example = "1") + private Integer status; + + @ApiModelProperty(value = "备注", example = "我是备注") + private String remark; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始创建时间") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束创建时间") + private Date endCreateTime; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoRespVO.java new file mode 100644 index 000000000..c0b8e5f46 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoRespVO.java @@ -0,0 +1,19 @@ +package cn.iocoder.dashboard.modules.system.controller.test.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; + +@ApiModel("字典类型 Response VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTestDemoRespVO extends SysTestDemoBaseVO { + + @ApiModelProperty(value = "字典主键", required = true, example = "1") + private Long id; + + @ApiModelProperty(value = "创建时间", required = true) + private Date createTime; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoUpdateReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoUpdateReqVO.java new file mode 100644 index 000000000..e0343d7e2 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/test/vo/SysTestDemoUpdateReqVO.java @@ -0,0 +1,21 @@ +package cn.iocoder.dashboard.modules.system.controller.test.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; + +import javax.validation.constraints.NotNull; + +@ApiModel("字典类型更新 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTestDemoUpdateReqVO extends SysTestDemoBaseVO { + + @ApiModelProperty(value = "字典主键", required = true, example = "1") + @NotNull(message = "字典主键不能为空") + private Long id; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/test/SysTestDemoMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/test/SysTestDemoMapper.java new file mode 100644 index 000000000..96672187e --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/test/SysTestDemoMapper.java @@ -0,0 +1,28 @@ +package cn.iocoder.dashboard.modules.system.dal.mysql.dao.test; + +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; +import cn.iocoder.dashboard.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO; +import org.apache.ibatis.annotations.Mapper; +import cn.iocoder.dashboard.modules.system.controller.test.vo.SysTestDemoPageReqVO; + +/** +* 字典类型 Mapper +* +* @author 芋艿 +*/ +@Mapper +public interface SysTestDemoMapper extends BaseMapperX { + + default PageResult selectPage(SysTestDemoPageReqVO reqVO) { + return selectPage(reqVO, new QueryWrapperX() + .likeIfPresent("name", reqVO.getName()) + .eqIfPresent("dict_type", reqVO.getDictType()) + .eqIfPresent("status", reqVO.getStatus()) + .eqIfPresent("remark", reqVO.getRemark()) + .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) + ); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/test/SysTestDemoDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/test/SysTestDemoDO.java new file mode 100644 index 000000000..65efa29a0 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/test/SysTestDemoDO.java @@ -0,0 +1,44 @@ +package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test; + +import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.*; + +/** +* 字典类型 DO +* +* @author 芋艿 +*/ +@TableName("sys_test_demo") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class SysTestDemoDO extends BaseDO { + + /** + * 字典主键 + */ + @TableId + private Long id; + /** + * 字典名称 + */ + private String name; + /** + * 字典类型 + */ + private String dictType; + /** + * 状态 + */ + private Integer status; + /** + * 备注 + */ + private String remark; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/test/SysTestDemoService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/test/SysTestDemoService.java new file mode 100644 index 000000000..11192a0f6 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/test/SysTestDemoService.java @@ -0,0 +1,66 @@ +package cn.iocoder.dashboard.modules.system.service.test; + +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.SysTestDemoUpdateReqVO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.test.SysTestDemoDO; + +import javax.validation.Valid; +import java.util.Collection; +import java.util.List; + +/** +* 字典类型 Service 接口 +* +* @author 芋艿 +*/ +public interface SysTestDemoService { + + /** + * 创建字典类型 + * + * @param createReqVO 创建信息 + * @return 编号 + */ + Long createTestDemo(@Valid SysTestDemoCreateReqVO createReqVO); + + /** + * 更新字典类型 + * + * @param updateReqVO 更新信息 + */ + void updateTestDemo(@Valid SysTestDemoUpdateReqVO updateReqVO); + + /** + * 删除字典类型 + * + * @param id 编号 + */ + void deleteTestDemo(Long id); + + /** + * 获得字典类型 + * + * @param id 编号 + * @return 字典类型 + */ + SysTestDemoDO getTestDemo(Long id); + + /** + * 获得字典类型列表 + * + * @param ids 编号 + * @return 字典类型列表 + */ + List getTestDemoList(Collection ids); + + /** + * 获得字典类型分页 + * + * @param pageReqVO 分页查询 + * @return 字典类型分页 + */ + PageResult getTestDemoPage(SysTestDemoPageReqVO pageReqVO); + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java index 6d228a18d..57fa01dfb 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenBuilder.java @@ -17,6 +17,8 @@ import org.springframework.stereotype.Component; import java.math.BigDecimal; import java.util.*; +import static cn.hutool.core.text.CharSequenceUtil.*; + /** * 代码生成器的 Builder,负责: * 1. 将数据库的表 {@link ToolInformationSchemaTableDO} 定义,构建成 {@link ToolCodegenTableDO} @@ -25,6 +27,18 @@ import java.util.*; @Component public class ToolCodegenBuilder { + /** + * Module 名字的映射 TODO 后续梳理到配置类 + * + * key:模块的完整名 + * value:模块的缩写名 + */ + private static final Map moduleNames = MapUtil.builder() + .put("system", "sys") + .put("infra", "inf") + .put("tool", "tool") + .build(); + /** * 字段名与 {@link ToolCodegenColumnListConditionEnum} 的默认映射 * 注意,字段的匹配以后缀的方式 @@ -93,9 +107,9 @@ public class ToolCodegenBuilder { CREATE_OPERATION_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS); UPDATE_OPERATION_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS); LIST_OPERATION_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS); - LIST_OPERATION_EXCLUDE_COLUMN.remove("create_time"); // 创建时间,还是可能需要传递的 + LIST_OPERATION_EXCLUDE_COLUMN.remove("createTime"); // 创建时间,还是可能需要传递的 LIST_OPERATION_RESULT_EXCLUDE_COLUMN.addAll(BASE_DO_FIELDS); - LIST_OPERATION_RESULT_EXCLUDE_COLUMN.remove("create_time"); // 创建时间,还是需要返回的 + LIST_OPERATION_RESULT_EXCLUDE_COLUMN.remove("createTime"); // 创建时间,还是需要返回的 } public ToolCodegenTableDO buildTable(ToolInformationSchemaTableDO schemaTable) { @@ -110,13 +124,12 @@ public class ToolCodegenBuilder { * @param table 表定义 */ private void initTableDefault(ToolCodegenTableDO table) { - table.setModuleName(StrUtil.subBefore(table.getTableName(), - '_', false)); // 第一个 _ 前缀的前面,作为 module 名字 - table.setBusinessName(StrUtil.subAfter(table.getTableName(), - '_', false)); // 第一个 _ 前缀的后面,作为 module 名字 - table.setBusinessName(StrUtil.toCamelCase(table.getBusinessName())); // 可能存在多个 _ 的情况,转换成驼峰 - table.setClassName(StrUtil.upperFirst(StrUtil.toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写 - table.setClassComment(StrUtil.subBefore(table.getTableComment(), // 去除结尾的表,作为类描述 + table.setModuleName(getFullModuleName(StrUtil.subBefore(table.getTableName(), + '_', false))); // 第一个 _ 前缀的前面,作为 module 名字 + table.setBusinessName(toCamelCase(subAfter(table.getTableName(), + '_', false))); // 第一步,第一个 _ 前缀的后面,作为 module 名字; 第二步,可能存在多个 _ 的情况,转换成驼峰 + table.setClassName(upperFirst(toCamelCase(table.getTableName()))); // 驼峰 + 首字母大写 + table.setClassComment(subBefore(table.getTableComment(), // 去除结尾的表,作为类描述 '表', true)); table.setAuthor("芋艿"); // TODO 稍后改成创建人 table.setTemplateType(ToolCodegenTemplateTypeEnum.CRUD.getType()); @@ -144,7 +157,7 @@ public class ToolCodegenBuilder { private void processColumnJava(ToolCodegenColumnDO column) { // 处理 javaField 字段 - column.setJavaField(StrUtil.toCamelCase(column.getColumnName())); + column.setJavaField(toCamelCase(column.getColumnName())); // 处理 dictType 字段,暂无 // 处理 javaType 字段 String dbType = StrUtil.subBefore(column.getColumnType(), '(', false); @@ -194,4 +207,27 @@ public class ToolCodegenBuilder { } } + /** + * 获得模块的缩略名 + * + * @param fullModuleName 模块的完整名 + * @return 缩略名 + */ + public String getSimpleModuleName(String fullModuleName) { + return moduleNames.getOrDefault(fullModuleName, fullModuleName); + } + + /** + * 获得模块的完整名 + * + * @param shortModuleName 模块的缩略名 + * @return 完整名 + */ + public String getFullModuleName(String shortModuleName) { + return moduleNames.entrySet().stream() + .filter(entry -> entry.getValue().equals(shortModuleName)) // 匹配 + .findFirst().map(Map.Entry::getKey) // 返回 key + .orElse(shortModuleName); // 兜底返回 shortModuleName + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java index 4258cf192..56fb4a766 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java +++ b/src/main/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngine.java @@ -14,6 +14,7 @@ import cn.iocoder.dashboard.util.collection.CollectionUtils; import cn.iocoder.dashboard.util.date.DateUtils; import org.springframework.stereotype.Component; +import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -31,6 +32,9 @@ import static cn.hutool.core.text.CharSequenceUtil.*; @Component public class ToolCodegenEngine { + @Resource + private ToolCodegenBuilder codegenBuilder; + /** * 模板引擎,由 hutool 实现 */ @@ -69,9 +73,15 @@ public class ToolCodegenEngine { Map bindingMap = new HashMap<>(globalBindingMap); bindingMap.put("table", table); bindingMap.put("columns", columns); - bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); - bindingMap.put("simpleClassName", upperFirst(toCamelCase(subAfter( // 去掉第一个驼峰,例如说 SysUser 去掉后是 User - toUnderlineCase(table.getClassName()), '_', false)))); + bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, ToolCodegenColumnDO::getPrimaryKey)); // 主键字段 + String simpleModuleName = codegenBuilder.getSimpleModuleName(table.getModuleName()); + bindingMap.put("simpleModuleName", simpleModuleName); // 将 system 转成 sys + // className 相关 + String simpleClassName = subAfter(table.getClassName(), upperFirst(simpleModuleName) + , false); // 将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀 + bindingMap.put("simpleClassName", simpleClassName); + bindingMap.put("simpleClassName_underlineCase", toUnderlineCase(simpleClassName)); // 将 DictType 转换成 dict_type + bindingMap.put("classNameVar", lowerFirst(simpleClassName)); // 将 DictType 转换成 dictType,用于变量 // 执行生成 // String result = templateEngine.getTemplate("codegen/dal/do.vm").render(bindingMap); // String result = templateEngine.getTemplate("codegen/dal/mapper.vm").render(bindingMap); @@ -80,7 +90,10 @@ public class ToolCodegenEngine { // String result = templateEngine.getTemplate("codegen/controller/vo/createReqVO.vm").render(bindingMap); // String result = templateEngine.getTemplate("codegen/controller/vo/updateReqVO.vm").render(bindingMap); // String result = templateEngine.getTemplate("codegen/controller/vo/respVO.vm").render(bindingMap); +// 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); System.out.println(result); } diff --git a/src/main/resources/codegen/controller/vo/updateReqVO.vm b/src/main/resources/codegen/controller/vo/updateReqVO.vm index 62acbf799..e10abcc10 100644 --- a/src/main/resources/codegen/controller/vo/updateReqVO.vm +++ b/src/main/resources/codegen/controller/vo/updateReqVO.vm @@ -6,8 +6,8 @@ import io.swagger.annotations.*; import javax.validation.constraints.*; ## 处理 Date 字段的引入 #foreach ($column in $columns) -#if (${column.updateOperation} && (!${column.createOperation} || !${column.listOperationResult})) - && ${column.javaType} == "Date")## 时间类型 +#if (${column.updateOperation} && (!${column.createOperation} || !${column.listOperationResult}) + && ${column.javaType} == "Date"))## 时间类型 import org.springframework.format.annotation.DateTimeFormat; import static ${DateUtilsClassName}.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; diff --git a/src/main/resources/codegen/convert/convert.vm b/src/main/resources/codegen/convert/convert.vm new file mode 100644 index 000000000..4571c9553 --- /dev/null +++ b/src/main/resources/codegen/convert/convert.vm @@ -0,0 +1,22 @@ +package ${basePackage}.${table.moduleName}.convert.${table.businessName}; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; +import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; +import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; + +/** +* ${table.classComment} Convert +* +* @author ${table.author} +*/ +@Mapper +public interface ${table.className}Convert { + + ${table.className}Convert INSTANCE = Mappers.getMapper(${table.className}Convert.class); + + ${table.className}DO convert(${table.className}CreateReqVO bean); + + ${table.className}DO convert(${table.className}UpdateReqVO bean); + +} diff --git a/src/main/resources/codegen/dal/do.vm b/src/main/resources/codegen/dal/do.vm index 70c8cc02c..6fd37b13c 100644 --- a/src/main/resources/codegen/dal/do.vm +++ b/src/main/resources/codegen/dal/do.vm @@ -3,7 +3,7 @@ package ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.business import lombok.*; import java.util.*; import com.baomidou.mybatisplus.annotation.*; -import ${BaseDOClassName}; +import ${baseDOClassName}; /** * ${table.classComment} DO diff --git a/src/main/resources/codegen/dal/mapper.vm b/src/main/resources/codegen/dal/mapper.vm index 942f17a95..a70aadd30 100644 --- a/src/main/resources/codegen/dal/mapper.vm +++ b/src/main/resources/codegen/dal/mapper.vm @@ -5,6 +5,7 @@ import ${QueryWrapperClassName}; import ${BaseMapperClassName}; import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; import org.apache.ibatis.annotations.Mapper; +import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.${table.className}PageReqVO; /** * ${table.classComment} Mapper @@ -26,19 +27,19 @@ public interface ${table.className}Mapper extends BaseMapperX<${table.className} .neIfPresent("${column.columnName}", reqVO.get${JavaField}()) #end #if (${column.listOperationCondition} == ">")##情况三,> 的时候 - .gtIfPresent("${column.columnName}", reqVO.get${JavaField}()) + .gtIfPresent("${column.columnName}", reqVO.get${JavaField}()) #end #if (${column.listOperationCondition} == ">=")##情况四,>= 的时候 - .geIfPresent("${column.columnName}", reqVO.get${JavaField}()) + .geIfPresent("${column.columnName}", reqVO.get${JavaField}()) #end #if (${column.listOperationCondition} == "<")##情况五,< 的时候 - .gtIfPresent("${column.columnName}", reqVO.get${JavaField}()) + .gtIfPresent("${column.columnName}", reqVO.get${JavaField}()) #end #if (${column.listOperationCondition} == "LIKE")##情况七,Like 的时候 - .likeIfPresent("${column.columnName}", reqVO.get${JavaField}()) + .likeIfPresent("${column.columnName}", reqVO.get${JavaField}()) #end #if (${column.listOperationCondition} == "BETWEEN")##情况八,Between 的时候 - .betweenIfPresent("${column.columnName}", reqVO.getBegin${JavaField}(), reqVO.getEnd${JavaField}()) + .betweenIfPresent("${column.columnName}", reqVO.getBegin${JavaField}(), reqVO.getEnd${JavaField}()) #end #end #end diff --git a/src/main/resources/codegen/enums/errorcode.vm b/src/main/resources/codegen/enums/errorcode.vm new file mode 100644 index 000000000..f6f623c17 --- /dev/null +++ b/src/main/resources/codegen/enums/errorcode.vm @@ -0,0 +1,2 @@ +// ========== ${table.classCommet} TODO 补充编号 ========== +ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_FOUND = new ErrorCode(TODO 补充编号, "${table.classComment}不存在}"); diff --git a/src/main/resources/codegen/service/service.vm b/src/main/resources/codegen/service/service.vm index 68a26df89..28d28b9be 100644 --- a/src/main/resources/codegen/service/service.vm +++ b/src/main/resources/codegen/service/service.vm @@ -17,7 +17,7 @@ public interface ${table.className}Service { * 创建${table.classComment} * * @param createReqVO 创建信息 - * @return ${class.description} + * @return 编号 */ ${primaryColumn.javaType} create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO); @@ -39,7 +39,7 @@ public interface ${table.className}Service { * 获得${table.classComment} * * @param id 编号 - * @return ${class.description} + * @return ${table.classComment} */ ${table.className}DO get${simpleClassName}(${primaryColumn.javaType} id); @@ -47,7 +47,7 @@ public interface ${table.className}Service { * 获得${table.classComment}列表 * * @param ids 编号 - * @return ${class.classComment}列表 + * @return ${table.classComment}列表 */ List<${table.className}DO> get${simpleClassName}List(Collection<${primaryColumn.javaType}> ids); @@ -55,7 +55,7 @@ public interface ${table.className}Service { * 获得${table.classComment}分页 * * @param pageReqVO 分页查询 - * @return ${class.classComment}分页 + * @return ${table.classComment}分页 */ PageResult<${table.className}DO> get${simpleClassName}Page(${table.className}PageReqVO pageReqVO); diff --git a/src/main/resources/codegen/service/serviceImpl.vm b/src/main/resources/codegen/service/serviceImpl.vm new file mode 100644 index 000000000..d129c4f38 --- /dev/null +++ b/src/main/resources/codegen/service/serviceImpl.vm @@ -0,0 +1,30 @@ +package ${basePackage}.${table.moduleName}.service.${table.businessName}.impl; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import org.springframework.validation.annotation.Validated; + +import java.util.*; +import ${basePackage}.${table.moduleName}.controller.${table.businessName}.vo.*; +import ${basePackage}.${table.moduleName}.dal.mysql.dataobject.${table.businessName}.${table.className}DO; +import ${PageResultClassName}; + +import static ${basePackage}.${table.moduleName}.enums.${simpleModuleName.substring(0,1).toUpperCase()}${simpleModuleName.substring(1)}ErrorCodeConstants.*; + +/** +* ${table.classComment} Service 实现类 +* +* @author ${table.author} +*/ +@Validated +public class ${table.className}ServiceImpl implements {table.className}Service { + + @Resource + private ${table.className}Mapper ${classNameVar}Mapper; + + @Override + public ${primaryColumn.javaType} create${simpleClassName}(@Valid ${table.className}CreateReqVO createReqVO) { + } + +} diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java index faf4d850b..180349650 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenEngineTest.java @@ -24,7 +24,7 @@ public class ToolCodegenEngineTest { @Test public void testExecute() { - ToolCodegenTableDO table = codegenTableMapper.selectById(8); + ToolCodegenTableDO table = codegenTableMapper.selectById(10); List columns = codegenColumnMapper.selectByTableId(table.getId()); codegenEngine.execute(table, columns); } diff --git a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java index f534e9769..e31ed2b7c 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/tool/service/codegen/impl/ToolCodegenServiceImplTest.java @@ -16,7 +16,7 @@ class ToolCodegenServiceImplTest { @Test public void tetCreateCodegenTable() { - toolCodegenService.createCodegenTable("sys_dict_type"); + toolCodegenService.createCodegenTable("sys_test_demo"); } }