refactor: 优化代码生成 POJO 逻辑及部分 freemarker 模板变量命名
This commit is contained in:
parent
c1a6ef2304
commit
cd3ac8a120
@ -61,5 +61,10 @@ public class GeneratorProperties {
|
||||
* 包名称
|
||||
*/
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
* 排除字段
|
||||
*/
|
||||
private String[] excludeFields;
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,6 @@ package top.charles7c.cnadmin.tool.model.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
@ -138,13 +137,6 @@ public class GenConfigDO implements Serializable {
|
||||
@TableField(exist = false)
|
||||
private String classNamePrefix;
|
||||
|
||||
/**
|
||||
* 字段配置信息
|
||||
*/
|
||||
@JsonIgnore
|
||||
@TableField(exist = false)
|
||||
private List<FieldConfigDO> fieldConfigs;
|
||||
|
||||
public GenConfigDO(String tableName) {
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
@ -40,7 +40,6 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.meta.Column;
|
||||
@ -212,7 +211,13 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
CheckUtils.throwIfNull(genConfig, "请先进行数据表 [{}] 生成配置", tableName);
|
||||
List<FieldConfigDO> fieldConfigList = fieldConfigMapper.selectListByTableName(tableName);
|
||||
CheckUtils.throwIfEmpty(fieldConfigList, "请先进行数据表 [{}] 字段配置", tableName);
|
||||
Map<String, Object> genConfigMap = this.pretreatment(genConfig, fieldConfigList);
|
||||
Map<String, Object> genConfigMap = BeanUtil.beanToMap(genConfig);
|
||||
genConfigMap.put("date", DateUtil.date().toString("yyyy/MM/dd HH:mm"));
|
||||
String packageName = genConfig.getPackageName();
|
||||
String apiModuleName =
|
||||
StrUtil.subSuf(packageName, StrUtil.lastIndexOfIgnoreCase(packageName, StringConsts.DOT) + 1);
|
||||
genConfigMap.put("apiModuleName", apiModuleName);
|
||||
genConfigMap.put("apiName", StrUtil.lowerFirst(genConfig.getClassNamePrefix()));
|
||||
|
||||
try {
|
||||
String classNamePrefix = genConfig.getClassNamePrefix();
|
||||
@ -232,13 +237,12 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
Map<String, TemplateConfig> templateConfigMap = generatorProperties.getTemplateConfigs();
|
||||
for (Map.Entry<String, TemplateConfig> templateConfigEntry : templateConfigMap.entrySet()) {
|
||||
// 例如:D:/continew-admin/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/impl/XxxServiceImpl.java
|
||||
TemplateConfig templateConfig = templateConfigEntry.getValue();
|
||||
String subPackageName = templateConfig.getPackageName();
|
||||
genConfigMap.put("subPackageName", subPackageName);
|
||||
File classParentFile =
|
||||
FileUtil.file(backendParentFile, StrUtil.splitToArray(subPackageName, StringConsts.DOT));
|
||||
this.pretreatment(genConfigMap, fieldConfigList, templateConfigEntry);
|
||||
String className = classNamePrefix + StrUtil.nullToEmpty(templateConfigEntry.getKey());
|
||||
genConfigMap.put("className", className);
|
||||
TemplateConfig templateConfig = templateConfigEntry.getValue();
|
||||
File classParentFile = FileUtil.file(backendParentFile,
|
||||
StrUtil.splitToArray(templateConfig.getPackageName(), StringConsts.DOT));
|
||||
File classFile = new File(classParentFile, className + FileNameUtil.EXT_JAVA);
|
||||
// 如果已经存在,且不允许覆盖,则跳过
|
||||
if (classFile.exists() && !isOverride) {
|
||||
@ -284,15 +288,22 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
/**
|
||||
* 预处理生成配置
|
||||
*
|
||||
* @param genConfig
|
||||
* @param genConfigMap
|
||||
* 生成配置
|
||||
* @param fieldConfigList
|
||||
* 字段配置列表
|
||||
* @return 处理后的生成配置
|
||||
* @param originFieldConfigList
|
||||
* 原始字段配置列表
|
||||
* @param templateConfigEntry
|
||||
* 模板配置
|
||||
*/
|
||||
private Map<String, Object> pretreatment(GenConfigDO genConfig, List<FieldConfigDO> fieldConfigList) {
|
||||
Map<String, Object> genConfigMap = MapUtil.newHashMap();
|
||||
genConfigMap.put("date", DateUtil.date().toString("yyyy/MM/dd HH:mm"));
|
||||
private void pretreatment(Map<String, Object> genConfigMap, List<FieldConfigDO> originFieldConfigList,
|
||||
Map.Entry<String, TemplateConfig> templateConfigEntry) {
|
||||
TemplateConfig templateConfig = templateConfigEntry.getValue();
|
||||
// 移除需要忽略的字段
|
||||
List<FieldConfigDO> fieldConfigList = originFieldConfigList.stream()
|
||||
.filter(fieldConfig -> !StrUtil.equalsAny(fieldConfig.getFieldName(), templateConfig.getExcludeFields()))
|
||||
.collect(Collectors.toList());
|
||||
genConfigMap.put("fieldConfigs", fieldConfigList);
|
||||
// 统计部分特殊字段特征
|
||||
genConfigMap.put("hasLocalDateTime", false);
|
||||
genConfigMap.put("hasBigDecimal", false);
|
||||
genConfigMap.put("hasRequiredField", false);
|
||||
@ -303,7 +314,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
genConfigMap.put("hasLocalDateTime", true);
|
||||
}
|
||||
if ("BigDecimal".equals(fieldType)) {
|
||||
genConfigMap.put("hasLocalDateTime", true);
|
||||
genConfigMap.put("hasBigDecimal", true);
|
||||
}
|
||||
if (Boolean.TRUE.equals(fieldConfig.getIsRequired())) {
|
||||
genConfigMap.put("hasRequiredField", true);
|
||||
@ -314,13 +325,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
genConfigMap.put("hasListQueryField", true);
|
||||
}
|
||||
}
|
||||
genConfig.setFieldConfigs(fieldConfigList);
|
||||
genConfigMap.putAll(BeanUtil.beanToMap(genConfig));
|
||||
String packageName = genConfig.getPackageName();
|
||||
String moduleName =
|
||||
StrUtil.subSuf(packageName, StrUtil.lastIndexOfIgnoreCase(packageName, StringConsts.DOT) + 1);
|
||||
genConfigMap.put("moduleName", moduleName);
|
||||
genConfigMap.put("apiName", StrUtil.lowerFirst(genConfig.getClassNamePrefix()));
|
||||
return genConfigMap;
|
||||
String subPackageName = templateConfig.getPackageName();
|
||||
genConfigMap.put("subPackageName", subPackageName);
|
||||
}
|
||||
}
|
||||
|
@ -38,5 +38,5 @@ import ${packageName}.service.${classNamePrefix}Service;
|
||||
*/
|
||||
@Tag(name = "${businessName}管理 API")
|
||||
@RestController
|
||||
@CrudRequestMapping(value = "/${moduleName}/${apiName}", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
|
||||
@CrudRequestMapping(value = "/${apiModuleName}/${apiName}", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
|
||||
public class ${className} extends BaseController<${classNamePrefix}Service, ${classNamePrefix}VO, ${classNamePrefix}DetailVO, ${classNamePrefix}Query, ${classNamePrefix}Request> {}
|
@ -1,7 +1,7 @@
|
||||
import axios from 'axios';
|
||||
import qs from 'query-string';
|
||||
|
||||
const BASE_URL = '/${moduleName}/${apiName}';
|
||||
const BASE_URL = '/${apiModuleName}/${apiName}';
|
||||
|
||||
export interface ${classNamePrefix}Record {
|
||||
<#if fieldConfigs??>
|
||||
|
@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<div class="app-container">
|
||||
<Breadcrumb :items="['menu.${moduleName}', 'menu.${moduleName}.${apiName}.list']" />
|
||||
<a-card class="general-card" :title="$t('menu.${moduleName}.${apiName}.list')">
|
||||
<Breadcrumb :items="['menu.${apiModuleName}', 'menu.${apiModuleName}.${apiName}.list']" />
|
||||
<a-card class="general-card" :title="$t('menu.${apiModuleName}.${apiName}.list')">
|
||||
<!-- 头部区域 -->
|
||||
<div class="header">
|
||||
<!-- 搜索栏 -->
|
||||
@ -38,14 +38,14 @@
|
||||
<a-col :span="12">
|
||||
<a-space>
|
||||
<a-button
|
||||
v-permission="['${moduleName}:${apiName}:add']"
|
||||
v-permission="['${apiModuleName}:${apiName}:add']"
|
||||
type="primary"
|
||||
@click="toAdd"
|
||||
>
|
||||
<template #icon><icon-plus /></template>新增
|
||||
</a-button>
|
||||
<a-button
|
||||
v-permission="['${moduleName}:${apiName}:update']"
|
||||
v-permission="['${apiModuleName}:${apiName}:update']"
|
||||
type="primary"
|
||||
status="success"
|
||||
:disabled="single"
|
||||
@ -55,7 +55,7 @@
|
||||
<template #icon><icon-edit /></template>修改
|
||||
</a-button>
|
||||
<a-button
|
||||
v-permission="['${moduleName}:${apiName}:delete']"
|
||||
v-permission="['${apiModuleName}:${apiName}:delete']"
|
||||
type="primary"
|
||||
status="danger"
|
||||
:disabled="multiple"
|
||||
@ -65,7 +65,7 @@
|
||||
<template #icon><icon-delete /></template>删除
|
||||
</a-button>
|
||||
<a-button
|
||||
v-permission="['${moduleName}:${apiName}:export']"
|
||||
v-permission="['${apiModuleName}:${apiName}:export']"
|
||||
:loading="exportLoading"
|
||||
type="primary"
|
||||
status="warning"
|
||||
@ -117,13 +117,13 @@
|
||||
</#if>
|
||||
</#list>
|
||||
<a-table-column
|
||||
v-if="checkPermission(['${moduleName}:${apiName}:update', '${moduleName}:${apiName}:delete'])"
|
||||
v-if="checkPermission(['${apiModuleName}:${apiName}:update', '${apiModuleName}:${apiName}:delete'])"
|
||||
title="操作"
|
||||
align="center"
|
||||
>
|
||||
<template #cell="{ record }">
|
||||
<a-button
|
||||
v-permission="['${moduleName}:${apiName}:update']"
|
||||
v-permission="['${apiModuleName}:${apiName}:update']"
|
||||
type="text"
|
||||
size="small"
|
||||
title="修改"
|
||||
@ -137,7 +137,7 @@
|
||||
@ok="handleDelete([record.id])"
|
||||
>
|
||||
<a-button
|
||||
v-permission="['${moduleName}:${apiName}:delete']"
|
||||
v-permission="['${apiModuleName}:${apiName}:delete']"
|
||||
type="text"
|
||||
size="small"
|
||||
title="删除"
|
||||
@ -244,7 +244,7 @@
|
||||
add${classNamePrefix},
|
||||
update${classNamePrefix},
|
||||
delete${classNamePrefix},
|
||||
} from '@/api/${moduleName}/${apiName}';
|
||||
} from '@/api/${apiModuleName}/${apiName}';
|
||||
import checkPermission from '@/utils/permission';
|
||||
|
||||
const { proxy } = getCurrentInstance() as any;
|
||||
@ -442,7 +442,7 @@
|
||||
if (exportLoading.value) return;
|
||||
exportLoading.value = true;
|
||||
proxy
|
||||
.download('/${moduleName}/${apiName}/export', { ...queryParams.value }, '${businessName}数据')
|
||||
.download('/${apiModuleName}/${apiName}/export', { ...queryParams.value }, '${businessName}数据')
|
||||
.finally(() => {
|
||||
exportLoading.value = false;
|
||||
});
|
||||
|
@ -55,12 +55,7 @@
|
||||
{{ rowIndex + 1 + (queryParams.page - 1) * queryParams.size }}
|
||||
</template>
|
||||
</a-table-column>
|
||||
<a-table-column
|
||||
title="表名称"
|
||||
data-index="tableName"
|
||||
:width="200"
|
||||
tooltip
|
||||
/>
|
||||
<a-table-column title="表名称" data-index="tableName" :width="225" />
|
||||
<a-table-column title="描述" data-index="comment" tooltip />
|
||||
<a-table-column title="存储引擎" data-index="engine" align="center" />
|
||||
<a-table-column title="字符集" data-index="charset" />
|
||||
|
@ -245,8 +245,17 @@ generator:
|
||||
# 模板配置
|
||||
templateConfigs:
|
||||
DO:
|
||||
# 模板路径
|
||||
templatePath: generator/Entity.ftl
|
||||
# 包名称
|
||||
packageName: model.entity
|
||||
# 排除字段
|
||||
excludeFields:
|
||||
- id
|
||||
- createUser
|
||||
- createTime
|
||||
- updateUser
|
||||
- updateTime
|
||||
Query:
|
||||
templatePath: generator/Query.ftl
|
||||
packageName: model.query
|
||||
@ -256,9 +265,19 @@ generator:
|
||||
VO:
|
||||
templatePath: generator/VO.ftl
|
||||
packageName: model.vo
|
||||
excludeFields:
|
||||
- id
|
||||
- createUser
|
||||
- createTime
|
||||
DetailVO:
|
||||
templatePath: generator/DetailVO.ftl
|
||||
packageName: model.vo
|
||||
excludeFields:
|
||||
- id
|
||||
- createUser
|
||||
- createTime
|
||||
- updateUser
|
||||
- updateTime
|
||||
Mapper:
|
||||
templatePath: generator/Mapper.ftl
|
||||
packageName: mapper
|
||||
|
Loading…
Reference in New Issue
Block a user