代码生成:主子表(inner)部分模版

This commit is contained in:
YunaiV 2023-11-13 09:52:48 +08:00
parent 256c560d43
commit a4b1395e92
5 changed files with 407 additions and 1 deletions

View File

@ -0,0 +1,13 @@
## 提供给 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 (${column.javaType} == 'String')
@NotEmpty(message = "${column.columnComment}不能为空")
#else
@NotNull(message = "${column.columnComment}不能为空")
#end
#end
#if (${column.javaType} == "LocalDateTime")## 时间类型
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
#end
private ${column.javaType} ${column.javaField};

View File

@ -146,6 +146,11 @@
#end
#end
#end
<el-table-column align="center" fixed="right" label="操作" width="60">
<template #default="{ $index }">
<el-button @click="handleDelete($index)" link>—</el-button>
</template>
</el-table-column>
</el-table>
</el-form>
<el-row justify="center" class="mt-3">

View File

@ -0,0 +1,100 @@
#set ($subTable = $subTables.get($subIndex))##当前表
#set ($subColumns = $subColumnsList.get($subIndex))##当前字段数组
#set ($subJoinColumn = $subJoinColumns.get($subIndex))##当前 join 字段
#set ($subSimpleClassName = $subSimpleClassNames.get($subIndex))
#set ($subJoinColumn = $subJoinColumns.get($subIndex))##当前 join 字段
#set ($SubJoinColumnName = $subJoinColumn.javaField.substring(0,1).toUpperCase() + ${subJoinColumn.javaField.substring(1)})##首字母大写
<template>
<!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
#foreach($column in $subColumns)
#if ($column.listOperationResult)
#set ($dictType=$column.dictType)
#set ($javaField = $column.javaField)
#set ($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
#set ($comment=$column.columnComment)
#if ($column.javaType == "LocalDateTime")## 时间类型
<el-table-column
label="${comment}"
align="center"
prop="${javaField}"
:formatter="dateFormatter"
width="180px"
/>
#elseif($column.dictType && "" != $column.dictType)## 数据字典
<el-table-column label="${comment}" align="center" prop="${javaField}">
<template #default="scope">
<dict-tag :type="DICT_TYPE.$dictType.toUpperCase()" :value="scope.row.${column.javaField}" />
</template>
</el-table-column>
#else
<el-table-column label="${comment}" align="center" prop="${javaField}" />
#end
#end
#end
#if ($table.templateType == 11)
<el-table-column label="操作" align="center">
<template #default="scope">
<el-button
link
type="primary"
@click="openForm('update', scope.row.id)"
v-hasPermi="['${permissionPrefix}:update']"
>
编辑
</el-button>
<el-button
link
type="danger"
@click="handleDelete(scope.row.id)"
v-hasPermi="['${permissionPrefix}:delete']"
>
删除
</el-button>
</template>
</el-table-column>
#end
</el-table>
#if ($table.templateType == 11)
<!-- 分页 -->
<Pagination
:total="total"
v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize"
@pagination="getList"
/>
#end
</ContentWrap>
</template>
<script setup lang="ts">
const props = defineProps<{
${subJoinColumn.javaField}: undefined // ${subJoinColumn.columnComment}(主表的关联字段)
}>()
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
/** 查询列表 */
const getList = async () => {
loading.value = true
try {
#if ( $subTable.subJoinMany )
formData.value = await ${simpleClassName}Api.get${subSimpleClassName}ListBy${SubJoinColumnName}(val)
#else
const data = await ${simpleClassName}Api.get${subSimpleClassName}By${SubJoinColumnName}(val)
if (!data) {
return
}
formData.value = data
#end
} finally {
loading.value = false
}
}
/** 初始化 **/
onMounted(() => {
getList()
})
</script>

View File

@ -0,0 +1,4 @@
## 子表的 erp 和 inner 使用相似的 list 列表,差异主要两点:
## 1inner 使用 list 不分页erp 使用 page 分页
## 2erp 支持单个子表的新增、修改、删除inner 不支持
#parse("codegen/vue3/views/components/list_sub_erp.vue.vm")

View File

@ -178,7 +178,7 @@ public class CodegenEngineTest extends BaseMockitoUnitTest {
}
@Test
public void testExecute_vue3_masterSub() {
public void testExecute_vue3_masterNormal() {
// 准备请求参数
// 主表
CodegenTableDO table = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene()).setParentMenuId(888L)
@ -461,6 +461,290 @@ public class CodegenEngineTest extends BaseMockitoUnitTest {
}
}
@Test
public void testExecute_vue3_masterInner() {
// 准备请求参数
// 主表
CodegenTableDO table = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene()).setParentMenuId(888L)
.setTableName("infra_demo12_student").setTableComment("学生表")
.setModuleName("infra").setBusinessName("demo12").setClassName("InfraDemo11Student")
.setClassComment("学生").setAuthor("芋道源码")
.setTemplateType(CodegenTemplateTypeEnum.MASTER_INNER.getType())
.setFrontType(CodegenFrontTypeEnum.VUE3.getType());
CodegenColumnDO idColumn = new CodegenColumnDO().setColumnName("id").setDataType(JdbcType.BIGINT.name())
.setId(100L)
.setColumnComment("编号").setNullable(false).setPrimaryKey(true).setAutoIncrement(true)
.setJavaType("Long").setJavaField("id").setExample("1024")
.setCreateOperation(false).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true);
CodegenColumnDO nameColumn = new CodegenColumnDO().setColumnName("name").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("名字").setNullable(false)
.setJavaType("String").setJavaField("name").setExample("芋头")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.LIKE.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.INPUT.getType());
CodegenColumnDO descriptionColumn = new CodegenColumnDO().setColumnName("description").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("简介").setNullable(false)
.setJavaType("String").setJavaField("description").setExample("我是介绍")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.TEXTAREA.getType());
CodegenColumnDO birthdayColumn = new CodegenColumnDO().setColumnName("birthday").setDataType(JdbcType.DATE.name())
.setColumnComment("出生日期").setNullable(false)
.setJavaType("LocalDateTime").setJavaField("birthday")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
CodegenColumnDO sexColumn = new CodegenColumnDO().setColumnName("sex").setDataType(JdbcType.INTEGER.name())
.setColumnComment("性别").setNullable(false)
.setJavaType("Integer").setJavaField("sex").setExample("1")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.SELECT.getType())
.setDictType("system_user_sex");
CodegenColumnDO enabledColumn = new CodegenColumnDO().setColumnName("enabled").setDataType(JdbcType.BOOLEAN.name())
.setColumnComment("是否有效").setNullable(false)
.setJavaType("Boolean").setJavaField("enabled").setExample("true")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.RADIO.getType())
.setDictType("infra_boolean_string");
CodegenColumnDO avatarColumn = new CodegenColumnDO().setColumnName("avatar").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("头像").setNullable(false)
.setJavaType("String").setJavaField("avatar").setExample("https://www.iocoder.cn/1.png")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.IMAGE_UPLOAD.getType());
CodegenColumnDO videoColumn = new CodegenColumnDO().setColumnName("video").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("附件").setNullable(true)
.setJavaType("String").setJavaField("video").setExample("https://www.iocoder.cn/1.mp4")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.FILE_UPLOAD.getType());
CodegenColumnDO memoColumn = new CodegenColumnDO().setColumnName("memo").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("备注").setNullable(false)
.setJavaType("String").setJavaField("memo").setExample("我是备注")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.EDITOR.getType());
CodegenColumnDO createTimeColumn = new CodegenColumnDO().setColumnName("create_time").setDataType(JdbcType.DATE.name())
.setColumnComment("创建时间").setNullable(true)
.setJavaType("LocalDateTime").setJavaField("createTime")
.setCreateOperation(false).setUpdateOperation(false)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.BETWEEN.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
List<CodegenColumnDO> columns = Arrays.asList(idColumn, nameColumn, descriptionColumn, birthdayColumn,
sexColumn, enabledColumn, avatarColumn, videoColumn, memoColumn, createTimeColumn);
// 子表联系人
CodegenTableDO contactTable = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene())
.setTableName("infra_demo12_student_contact").setTableComment("学生联系人表")
.setModuleName("infra").setBusinessName("demo12").setClassName("InfraDemo11StudentContact")
.setClassComment("学生联系人").setAuthor("芋道源码")
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
.setSubJoinColumnId(100L).setSubJoinMany(true);
CodegenColumnDO contactIdColumn = new CodegenColumnDO().setColumnName("id").setDataType(JdbcType.BIGINT.name())
.setColumnComment("编号").setNullable(false).setPrimaryKey(true).setAutoIncrement(true)
.setJavaType("Long").setJavaField("id").setExample("1024")
.setCreateOperation(false).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true);
CodegenColumnDO contactStudentIdColumn = new CodegenColumnDO().setColumnName("student_id").setDataType(JdbcType.BIGINT.name())
.setColumnComment("学生编号").setNullable(false).setPrimaryKey(false)
.setJavaType("Long").setJavaField("studentId").setExample("2048")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setId(100L);
CodegenColumnDO contactNameColumn = new CodegenColumnDO().setColumnName("name").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("名字").setNullable(false)
.setJavaType("String").setJavaField("name").setExample("芋头")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.LIKE.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.INPUT.getType());
CodegenColumnDO contactDescriptionColumn = new CodegenColumnDO().setColumnName("description").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("简介").setNullable(false)
.setJavaType("String").setJavaField("description").setExample("我是介绍")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.TEXTAREA.getType());
CodegenColumnDO contactBirthdayColumn = new CodegenColumnDO().setColumnName("birthday").setDataType(JdbcType.DATE.name())
.setColumnComment("出生日期").setNullable(false)
.setJavaType("LocalDateTime").setJavaField("birthday")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
CodegenColumnDO contactSexColumn = new CodegenColumnDO().setColumnName("sex").setDataType(JdbcType.INTEGER.name())
.setColumnComment("性别").setNullable(false)
.setJavaType("Integer").setJavaField("sex").setExample("1")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.SELECT.getType())
.setDictType("system_user_sex");
CodegenColumnDO contactEnabledColumn = new CodegenColumnDO().setColumnName("enabled").setDataType(JdbcType.BOOLEAN.name())
.setColumnComment("是否有效").setNullable(false)
.setJavaType("Boolean").setJavaField("enabled").setExample("true")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.RADIO.getType())
.setDictType("infra_boolean_string");
CodegenColumnDO contactAvatarColumn = new CodegenColumnDO().setColumnName("avatar").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("头像").setNullable(false)
.setJavaType("String").setJavaField("avatar").setExample("https://www.iocoder.cn/1.png")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.IMAGE_UPLOAD.getType());
CodegenColumnDO contactVideoColumn = new CodegenColumnDO().setColumnName("video").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("附件").setNullable(true)
.setJavaType("String").setJavaField("video").setExample("https://www.iocoder.cn/1.mp4")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.FILE_UPLOAD.getType());
CodegenColumnDO contactMemoColumn = new CodegenColumnDO().setColumnName("memo").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("备注").setNullable(false)
.setJavaType("String").setJavaField("memo").setExample("我是备注")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.EDITOR.getType());
CodegenColumnDO contactCreateTimeColumn = new CodegenColumnDO().setColumnName("create_time").setDataType(JdbcType.DATE.name())
.setColumnComment("创建时间").setNullable(true)
.setJavaType("LocalDateTime").setJavaField("createTime")
.setCreateOperation(false).setUpdateOperation(false)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.BETWEEN.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
List<CodegenColumnDO> contactColumns = Arrays.asList(contactIdColumn, contactStudentIdColumn,
contactNameColumn, contactDescriptionColumn, contactBirthdayColumn,
contactSexColumn, contactEnabledColumn, contactAvatarColumn, contactVideoColumn, contactMemoColumn, contactCreateTimeColumn);
// 子表班主任
CodegenTableDO teacherTable = new CodegenTableDO().setScene(CodegenSceneEnum.ADMIN.getScene())
.setTableName("infra_demo12_student_teacher").setTableComment("学生班主任表")
.setModuleName("infra").setBusinessName("demo12").setClassName("InfraDemo11StudentTeacher")
.setClassComment("学生班主任").setAuthor("芋道源码")
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
.setSubJoinColumnId(200L).setSubJoinMany(false);
CodegenColumnDO teacherIdColumn = new CodegenColumnDO().setColumnName("id").setDataType(JdbcType.BIGINT.name())
.setColumnComment("编号").setNullable(false).setPrimaryKey(true).setAutoIncrement(true)
.setJavaType("Long").setJavaField("id").setExample("1024")
.setCreateOperation(false).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true);
CodegenColumnDO teacherStudentIdColumn = new CodegenColumnDO().setColumnName("student_id").setDataType(JdbcType.BIGINT.name())
.setColumnComment("学生编号").setNullable(false).setPrimaryKey(false)
.setJavaType("Long").setJavaField("studentId").setExample("2048")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setId(200L);
CodegenColumnDO teacherNameColumn = new CodegenColumnDO().setColumnName("name").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("名字").setNullable(false)
.setJavaType("String").setJavaField("name").setExample("芋头")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.LIKE.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.INPUT.getType());
CodegenColumnDO teacherDescriptionColumn = new CodegenColumnDO().setColumnName("description").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("简介").setNullable(false)
.setJavaType("String").setJavaField("description").setExample("我是介绍")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.TEXTAREA.getType());
CodegenColumnDO teacherBirthdayColumn = new CodegenColumnDO().setColumnName("birthday").setDataType(JdbcType.DATE.name())
.setColumnComment("出生日期").setNullable(false)
.setJavaType("LocalDateTime").setJavaField("birthday")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
CodegenColumnDO teacherSexColumn = new CodegenColumnDO().setColumnName("sex").setDataType(JdbcType.INTEGER.name())
.setColumnComment("性别").setNullable(false)
.setJavaType("Integer").setJavaField("sex").setExample("1")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.SELECT.getType())
.setDictType("system_user_sex");
CodegenColumnDO teacherEnabledColumn = new CodegenColumnDO().setColumnName("enabled").setDataType(JdbcType.BOOLEAN.name())
.setColumnComment("是否有效").setNullable(false)
.setJavaType("Boolean").setJavaField("enabled").setExample("true")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.EQ.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.RADIO.getType())
.setDictType("infra_boolean_string");
CodegenColumnDO teacherAvatarColumn = new CodegenColumnDO().setColumnName("avatar").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("头像").setNullable(false)
.setJavaType("String").setJavaField("avatar").setExample("https://www.iocoder.cn/1.png")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.IMAGE_UPLOAD.getType());
CodegenColumnDO teacherVideoColumn = new CodegenColumnDO().setColumnName("video").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("附件").setNullable(true)
.setJavaType("String").setJavaField("video").setExample("https://www.iocoder.cn/1.mp4")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.FILE_UPLOAD.getType());
CodegenColumnDO teacherMemoColumn = new CodegenColumnDO().setColumnName("memo").setDataType(JdbcType.VARCHAR.name())
.setColumnComment("备注").setNullable(false)
.setJavaType("String").setJavaField("memo").setExample("我是备注")
.setCreateOperation(true).setUpdateOperation(true)
.setListOperation(false)
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.EDITOR.getType());
CodegenColumnDO teacherCreateTimeColumn = new CodegenColumnDO().setColumnName("create_time").setDataType(JdbcType.DATE.name())
.setColumnComment("创建时间").setNullable(true)
.setJavaType("LocalDateTime").setJavaField("createTime")
.setCreateOperation(false).setUpdateOperation(false)
.setListOperation(true).setListOperationCondition(CodegenColumnListConditionEnum.BETWEEN.getCondition())
.setListOperationResult(true)
.setHtmlType(CodegenColumnHtmlTypeEnum.DATETIME.getType());
List<CodegenColumnDO> teacherColumns = Arrays.asList(teacherIdColumn, teacherStudentIdColumn,
teacherNameColumn, teacherDescriptionColumn, teacherBirthdayColumn,
teacherSexColumn, teacherEnabledColumn, teacherAvatarColumn, teacherVideoColumn, teacherMemoColumn, teacherCreateTimeColumn);
// 调用
Map<String, String> result = codegenEngine.execute(table, columns,
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
// 构建 zip
writeFile(result, "/Users/yunai/test/demo12.zip");
// 断言
assertEquals(27, result.size());
for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println("");
System.out.println("");
System.out.println("");
}
}
private void assertPathContentEquals(String path, Map<String, String> result, String key) {
String pathContent = ResourceUtil.readUtf8Str("codegen/" + path);
String valueContent = result.get(key);