codegen:1)增加 vue3 + crud 模式下的单测;2)增加主子表的 db 字段

This commit is contained in:
zhijiantianya@gmail.com
2023-11-07 20:43:53 +08:00
parent 0af205ede1
commit 9705ae061c
51 changed files with 1643 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.column.CodegenColumnBaseVO;
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTableBaseVO;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenTemplateTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -43,6 +44,12 @@ public class CodegenUpdateReqVO {
|| getParentMenuId() != null;
}
@AssertTrue(message = "关联的子表与字段不能为空")
public boolean isSubValid() {
return ObjectUtil.notEqual(getTemplateType(), CodegenTemplateTypeEnum.MASTER_SUB)
|| (getSubTableId() != null && getSubColumnId() != null);
}
}
@Schema(description = "更新表定义")

View File

@@ -58,4 +58,9 @@ public class CodegenTableBaseVO {
@Schema(description = "父菜单编号", example = "1024")
private Long parentMenuId;
@Schema(description = "子表的表编号", example = "2048")
private Long subTableId;
@Schema(description = "子表的关联字段编号", example = "4096")
private Long subColumnId;
}

View File

@@ -116,4 +116,19 @@ public class CodegenTableDO extends BaseDO {
*/
private Long parentMenuId;
// ========== 主子表相关字段 ==========
/**
* 子表的表编号
*
* 关联 {@link CodegenTableDO#getId()}
*/
private Long subTableId;
/**
* 子表的关联字段编号
*
* 关联 {@link CodegenColumnDO#getId()}
*/
private Long subColumnId;
}

View File

@@ -14,6 +14,7 @@ public enum CodegenTemplateTypeEnum {
CRUD(1), // 单表(增删改查)
TREE(2), // 树表(增删改查)
MASTER_SUB(3), // 主子表
;
/**

View File

@@ -235,8 +235,16 @@ public class CodegenServiceImpl implements CodegenService {
throw exception(CODEGEN_COLUMN_NOT_EXISTS);
}
// 校验子表是否已经存在
CodegenTableDO subTable = null;
List<CodegenColumnDO> subColumns = null;
if (table.getSubTableId() != null) {
subTable = codegenTableMapper.selectById(table.getSubTableId());
subColumns = codegenColumnMapper.selectListByTableId(table.getSubTableId());
}
// 执行生成
return codegenEngine.execute(table, columns);
return codegenEngine.execute(table, columns, subTable, subColumns);
}
@Override

View File

@@ -26,6 +26,7 @@ import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum;
import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum;
import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.Maps;
import com.google.common.collect.Table;
@@ -37,6 +38,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import static cn.hutool.core.map.MapUtil.getStr;
import static cn.hutool.core.text.CharSequenceUtil.*;
@@ -149,7 +151,8 @@ public class CodegenEngine {
}
@PostConstruct
private void initGlobalBindingMap() {
@VisibleForTesting
void initGlobalBindingMap() {
// 全局配置
globalBindingMap.put("basePackage", codegenProperties.getBasePackage());
globalBindingMap.put("baseFrameworkPackage", codegenProperties.getBasePackage()
@@ -176,13 +179,28 @@ public class CodegenEngine {
globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName());
}
public Map<String, String> execute(CodegenTableDO table, List<CodegenColumnDO> columns) {
/**
* 生成代码
*
* @param table 表定义
* @param columns table 的字段定义数组
* @param subTable 子表定义,当且仅当主子表时使用
* @param subColumns subTable 的字段定义数组
* @return 生成的代码key 是路径value 是对应代码
*/
public Map<String, String> execute(CodegenTableDO table, List<CodegenColumnDO> columns,
CodegenTableDO subTable, List<CodegenColumnDO> subColumns) {
// 创建 bindingMap
Map<String, Object> bindingMap = new HashMap<>(globalBindingMap);
bindingMap.put("table", table);
bindingMap.put("columns", columns);
bindingMap.put("primaryColumn", CollectionUtils.findFirst(columns, CodegenColumnDO::getPrimaryKey)); // 主键字段
bindingMap.put("sceneEnum", CodegenSceneEnum.valueOf(table.getScene()));
if (subTable != null) {
bindingMap.put("subTable", subTable);
bindingMap.put("subColumns", subColumns);
bindingMap.put("subColumn", CollectionUtils.findFirst(subColumns, column -> column.getId().equals(table.getSubColumnId())));
}
// className 相关
// 去掉指定前缀,将 TestDictType 转换成 DictType. 因为在 create 等方法后,不需要带上 Test 前缀

View File

@@ -108,4 +108,4 @@ public class ${sceneEnum.prefixClass}${table.className}Controller {
ExcelUtils.write(response, "${table.classComment}.xls", "数据", ${sceneEnum.prefixClass}${table.className}ExcelVO.class, datas);
}
}
}

View File

@@ -1,7 +1,7 @@
## 提供给 baseVO、createVO、updateVO 生成字段
@Schema(description = "${column.columnComment}"#if (!${column.nullable}), requiredMode = Schema.RequiredMode.REQUIRED#end#if ("$!column.example" != ""), example = "${column.example}"#end)
#if (!${column.nullable})## 判断 @NotEmpty 和 @NotNull 注解
#if (${field.fieldType} == 'String')
#if (${column.javaType} == 'String')
@NotEmpty(message = "${column.columnComment}不能为空")
#else
@NotNull(message = "${column.columnComment}不能为空")

View File

@@ -27,4 +27,4 @@ public class ${sceneEnum.prefixClass}${table.className}CreateReqVO extends ${sce
#end
#end
}
}

View File

@@ -42,4 +42,4 @@ public class ${sceneEnum.prefixClass}${table.className}ExcelVO {
#end
#end
}
}

View File

@@ -36,4 +36,4 @@ public class ${sceneEnum.prefixClass}${table.className}ExportReqVO {
#end
#end
}
}

View File

@@ -38,4 +38,4 @@ public class ${sceneEnum.prefixClass}${table.className}PageReqVO extends PagePar
#end
#end
}
}

View File

@@ -22,4 +22,4 @@ public class ${sceneEnum.prefixClass}${table.className}RespVO extends ${sceneEnu
#end
#end
}
}

View File

@@ -27,4 +27,4 @@ public class ${sceneEnum.prefixClass}${table.className}UpdateReqVO extends ${sce
#end
#end
}
}

View File

@@ -31,4 +31,4 @@ public interface ${table.className}Convert {
List<${sceneEnum.prefixClass}${table.className}ExcelVO> convertList02(List<${table.className}DO> list);
}
}

View File

@@ -44,4 +44,4 @@ public class ${table.className}DO extends BaseDO {
#end
#end
}
}

View File

@@ -63,4 +63,4 @@ public interface ${table.className}Mapper extends BaseMapperX<${table.className}
}
}
}

View File

@@ -9,4 +9,4 @@
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
</mapper>
</mapper>

View File

@@ -1,3 +1,3 @@
// TODO 待办:请将下面的错误码复制到 yudao-module-${table.moduleName}-api 模块的 ErrorCodeConstants 类中。注意请给“TODO 补充编号”设置一个错误码编号!!!
// ========== ${table.classComment} TODO 补充编号 ==========
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在");
ErrorCode ${simpleClassName_underlineCase.toUpperCase()}_NOT_EXISTS = new ErrorCode(TODO 补充编号, "${table.classComment}不存在");

View File

@@ -67,4 +67,4 @@ public interface ${table.className}Service {
*/
List<${table.className}DO> get${simpleClassName}List(${sceneEnum.prefixClass}${table.className}ExportReqVO exportReqVO);
}
}

View File

@@ -85,4 +85,4 @@ public class ${table.className}ServiceImpl implements ${table.className}Service
return ${classNameVar}Mapper.selectList(exportReqVO);
}
}
}

View File

@@ -162,4 +162,4 @@ public class ${table.className}ServiceImplTest extends BaseDbUnitTest {
assertPojoEquals(db${simpleClassName}, list.get(0));
}
}
}

View File

@@ -32,4 +32,4 @@ CREATE TABLE IF NOT EXISTS "${table.tableName.toLowerCase()}" (
) COMMENT '${table.tableComment}';
-- 将该删表 SQL 语句,添加到 yudao-module-${table.moduleName}-biz 模块的 test/resources/sql/clean.sql 文件里
DELETE FROM "${table.tableName}";
DELETE FROM "${table.tableName}";

View File

@@ -25,4 +25,4 @@ VALUES (
'${table.classComment}${functionName}', '${permissionPrefix}:${functionOps.get($index)}', 3, $foreach.count, @parentId,
'', '', '', 0
);
#end
#end

View File

@@ -43,4 +43,4 @@ export const delete${simpleClassName} = async (id: number) => {
// 导出${table.classComment} Excel
export const export${simpleClassName} = async (params) => {
return await request.download({ url: `${baseURL}/export-excel`, params })
}
}

View File

@@ -231,4 +231,4 @@ const resetForm = () => {
}
formRef.value?.resetFields()
}
</script>
</script>

View File

@@ -286,4 +286,4 @@ const handleExport = async () => {
onMounted(() => {
getList()
})
</script>
</script>