fix: 代码生成测试问题修复
This commit is contained in:
@@ -34,7 +34,6 @@ public class GeneratorProperties {
|
|||||||
/**
|
/**
|
||||||
* 后端应用名
|
* 后端应用名
|
||||||
*/
|
*/
|
||||||
|
|
||||||
private String backendAppName;
|
private String backendAppName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,6 +41,11 @@ public class GeneratorProperties {
|
|||||||
*/
|
*/
|
||||||
private String frontendAppName;
|
private String frontendAppName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件名
|
||||||
|
*/
|
||||||
|
private String downloadFileName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 排除数据表
|
* 排除数据表
|
||||||
*/
|
*/
|
||||||
@@ -72,7 +76,6 @@ public class GeneratorProperties {
|
|||||||
|
|
||||||
private String author;
|
private String author;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.youlai.system.controller;
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.youlai.system.common.result.PageResult;
|
import com.youlai.system.common.result.PageResult;
|
||||||
import com.youlai.system.common.result.Result;
|
import com.youlai.system.common.result.Result;
|
||||||
|
import com.youlai.system.config.property.GeneratorProperties;
|
||||||
import com.youlai.system.enums.LogModuleEnum;
|
import com.youlai.system.enums.LogModuleEnum;
|
||||||
import com.youlai.system.model.form.GenConfigForm;
|
import com.youlai.system.model.form.GenConfigForm;
|
||||||
import com.youlai.system.model.query.TablePageQuery;
|
import com.youlai.system.model.query.TablePageQuery;
|
||||||
@@ -17,8 +18,11 @@ import jakarta.servlet.http.HttpServletResponse;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.util.UriUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,6 +38,7 @@ import java.util.List;
|
|||||||
public class GeneratorController {
|
public class GeneratorController {
|
||||||
|
|
||||||
private final GeneratorService generatorService;
|
private final GeneratorService generatorService;
|
||||||
|
private final GeneratorProperties generatorProperties;
|
||||||
|
|
||||||
@Operation(summary = "获取数据表分页列表")
|
@Operation(summary = "获取数据表分页列表")
|
||||||
@GetMapping("/table/page")
|
@GetMapping("/table/page")
|
||||||
@@ -86,8 +91,7 @@ public class GeneratorController {
|
|||||||
String[] tableNames = tableName.split(",");
|
String[] tableNames = tableName.split(",");
|
||||||
byte[] data = generatorService.downloadCode(tableNames);
|
byte[] data = generatorService.downloadCode(tableNames);
|
||||||
response.reset();
|
response.reset();
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=\"youlai-admin-code.zip\"");
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(generatorProperties.getDownloadFileName(), StandardCharsets.UTF_8));
|
||||||
response.addHeader("Content-Length", String.valueOf(data.length));
|
|
||||||
response.addHeader("Access-Control-Allow-Origin", "*");
|
response.addHeader("Access-Control-Allow-Origin", "*");
|
||||||
response.setHeader("Pragma", "no-cache");
|
response.setHeader("Pragma", "no-cache");
|
||||||
response.setHeader("Cache-Control", "no-cache");
|
response.setHeader("Cache-Control", "no-cache");
|
||||||
|
|||||||
@@ -56,11 +56,11 @@ public enum JavaTypeEnum {
|
|||||||
/**
|
/**
|
||||||
* 根据数据库类型获取对应的Java类型
|
* 根据数据库类型获取对应的Java类型
|
||||||
*
|
*
|
||||||
* @param dbType 数据库类型
|
* @param columnType 列类型
|
||||||
* @return 对应的Java类型
|
* @return 对应的Java类型
|
||||||
*/
|
*/
|
||||||
public static String getJavaTypeByDbType(String dbType) {
|
public static String getJavaTypeByColumnType(String columnType) {
|
||||||
JavaTypeEnum javaTypeEnum = typeMap.get(dbType);
|
JavaTypeEnum javaTypeEnum = typeMap.get(columnType);
|
||||||
if (javaTypeEnum != null) {
|
if (javaTypeEnum != null) {
|
||||||
return javaTypeEnum.getJavaType();
|
return javaTypeEnum.getJavaType();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class GenConfigForm {
|
|||||||
@Schema(description = "模块名",example = "system")
|
@Schema(description = "模块名",example = "system")
|
||||||
private String moduleName;
|
private String moduleName;
|
||||||
|
|
||||||
@Schema(description = "包名",example = "com.youlai.system")
|
@Schema(description = "包名",example = "com.youlai")
|
||||||
private String packageName;
|
private String packageName;
|
||||||
|
|
||||||
@Schema(description = "实体名",example = "User")
|
@Schema(description = "实体名",example = "User")
|
||||||
@@ -44,6 +44,12 @@ public class GenConfigForm {
|
|||||||
@Schema(description = "字段配置列表")
|
@Schema(description = "字段配置列表")
|
||||||
private List<FieldConfig> fieldConfigs;
|
private List<FieldConfig> fieldConfigs;
|
||||||
|
|
||||||
|
@Schema(description = "后端应用名")
|
||||||
|
private String backendAppName;
|
||||||
|
|
||||||
|
@Schema(description = "前端应用名")
|
||||||
|
private String frontendAppName;
|
||||||
|
|
||||||
@Schema(description = "字段配置")
|
@Schema(description = "字段配置")
|
||||||
@Data
|
@Data
|
||||||
public static class FieldConfig {
|
public static class FieldConfig {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
@@ -92,6 +93,10 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
.eq(GenConfig::getTableName, tableName)
|
.eq(GenConfig::getTableName, tableName)
|
||||||
.last("LIMIT 1")
|
.last("LIMIT 1")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 是否有代码生成配置
|
||||||
|
boolean hasGenConfig = genConfig != null;
|
||||||
|
|
||||||
// 如果没有代码生成配置,则根据表的元数据生成默认配置
|
// 如果没有代码生成配置,则根据表的元数据生成默认配置
|
||||||
if (genConfig == null) {
|
if (genConfig == null) {
|
||||||
TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName);
|
TableMetaData tableMetadata = databaseMapper.getTableMetadata(tableName);
|
||||||
@@ -136,25 +141,34 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
for (ColumnMetaData tableColumn : tableColumns) {
|
for (ColumnMetaData tableColumn : tableColumns) {
|
||||||
// 根据列名获取字段生成配置
|
// 根据列名获取字段生成配置
|
||||||
String columnName = tableColumn.getColumnName();
|
String columnName = tableColumn.getColumnName();
|
||||||
GenFieldConfig genFieldConfig = fieldConfigList.stream()
|
GenFieldConfig fieldConfig = fieldConfigList.stream()
|
||||||
.filter(item -> StrUtil.equals(item.getColumnName(), columnName))
|
.filter(item -> StrUtil.equals(item.getColumnName(), columnName))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElseGet(() -> createDefaultFieldConfig(tableColumn));
|
.orElseGet(() -> createDefaultFieldConfig(tableColumn));
|
||||||
if (genFieldConfig.getFieldSort() == null) {
|
if (fieldConfig.getFieldSort() == null) {
|
||||||
genFieldConfig.setFieldSort(++maxSort);
|
fieldConfig.setFieldSort(++maxSort);
|
||||||
}
|
}
|
||||||
// 根据列类型设置字段类型
|
// 根据列类型设置字段类型
|
||||||
String fieldType = genFieldConfig.getFieldType();
|
String fieldType = fieldConfig.getFieldType();
|
||||||
if (StrUtil.isBlank(fieldType)) {
|
if (StrUtil.isBlank(fieldType)) {
|
||||||
String javaType = JavaTypeEnum.getJavaTypeByDbType(genFieldConfig.getColumnType());
|
String javaType = JavaTypeEnum.getJavaTypeByColumnType(fieldConfig.getColumnType());
|
||||||
genFieldConfig.setFieldType(javaType);
|
fieldConfig.setFieldType(javaType);
|
||||||
}
|
}
|
||||||
genFieldConfigs.add(genFieldConfig);
|
// 如果没有代码生成配置,则默认展示在列表和表单
|
||||||
|
if (!hasGenConfig) {
|
||||||
|
fieldConfig.setIsShowInList(1);
|
||||||
|
fieldConfig.setIsShowInForm(1);
|
||||||
|
}
|
||||||
|
genFieldConfigs.add(fieldConfig);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//对genFieldConfigs按照fieldSort排序
|
//对genFieldConfigs按照fieldSort排序
|
||||||
genFieldConfigs = genFieldConfigs.stream().sorted(Comparator.comparing(GenFieldConfig::getFieldSort)).collect(Collectors.toList());
|
genFieldConfigs = genFieldConfigs.stream().sorted(Comparator.comparing(GenFieldConfig::getFieldSort)).toList();
|
||||||
return genConfigConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
GenConfigForm genConfigForm = genConfigConverter.toGenConfigForm(genConfig, genFieldConfigs);
|
||||||
|
|
||||||
|
genConfigForm.setFrontendAppName(generatorProperties.getFrontendAppName());
|
||||||
|
genConfigForm.setBackendAppName(generatorProperties.getBackendAppName());
|
||||||
|
return genConfigForm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -171,7 +185,15 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
fieldConfig.setFieldComment(columnMetaData.getColumnComment());
|
fieldConfig.setFieldComment(columnMetaData.getColumnComment());
|
||||||
fieldConfig.setFieldName(StrUtil.toCamelCase(columnMetaData.getColumnName()));
|
fieldConfig.setFieldName(StrUtil.toCamelCase(columnMetaData.getColumnName()));
|
||||||
fieldConfig.setIsRequired("YES".equals(columnMetaData.getIsNullable()) ? 1 : 0);
|
fieldConfig.setIsRequired("YES".equals(columnMetaData.getIsNullable()) ? 1 : 0);
|
||||||
|
|
||||||
|
if (fieldConfig.getColumnType().equals("date")) {
|
||||||
|
fieldConfig.setFormType(FormTypeEnum.DATE);
|
||||||
|
} else if (fieldConfig.getColumnType().equals("datetime")) {
|
||||||
|
fieldConfig.setFormType(FormTypeEnum.DATE_TIME);
|
||||||
|
} else {
|
||||||
fieldConfig.setFormType(FormTypeEnum.INPUT);
|
fieldConfig.setFormType(FormTypeEnum.INPUT);
|
||||||
|
}
|
||||||
|
|
||||||
fieldConfig.setQueryType(QueryTypeEnum.EQ);
|
fieldConfig.setQueryType(QueryTypeEnum.EQ);
|
||||||
fieldConfig.setMaxLength(columnMetaData.getCharacterMaximumLength());
|
fieldConfig.setMaxLength(columnMetaData.getCharacterMaximumLength());
|
||||||
return fieldConfig;
|
return fieldConfig;
|
||||||
@@ -268,7 +290,6 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
String fileName = getFileName(entityName, templateName, extension);
|
String fileName = getFileName(entityName, templateName, extension);
|
||||||
previewVO.setFileName(fileName);
|
previewVO.setFileName(fileName);
|
||||||
|
|
||||||
|
|
||||||
/* 2. 生成文件路径 */
|
/* 2. 生成文件路径 */
|
||||||
// com.youlai
|
// com.youlai
|
||||||
String packageName = genConfig.getPackageName();
|
String packageName = genConfig.getPackageName();
|
||||||
@@ -282,7 +303,6 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
|
|
||||||
/* 3. 生成文件内容 */
|
/* 3. 生成文件内容 */
|
||||||
|
|
||||||
// 生成文件内容
|
|
||||||
String content = getCodeContent(templateConfig, genConfig, fieldConfigs);
|
String content = getCodeContent(templateConfig, genConfig, fieldConfigs);
|
||||||
previewVO.setContent(content);
|
previewVO.setContent(content);
|
||||||
|
|
||||||
@@ -292,25 +312,37 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成文件名
|
||||||
|
*
|
||||||
|
* @param entityName 实体类名 UserController
|
||||||
|
* @param templateName 模板名 Entity
|
||||||
|
* @param extension 文件后缀 .java
|
||||||
|
* @return 文件名
|
||||||
|
*/
|
||||||
private String getFileName(String entityName, String templateName, String extension) {
|
private String getFileName(String entityName, String templateName, String extension) {
|
||||||
if ("Entity".equals(templateName)) {
|
if ("Entity".equals(templateName)) {
|
||||||
return entityName + extension;
|
return entityName + extension;
|
||||||
}
|
} else if ("MapperXml".equals(templateName)) {
|
||||||
if ("MapperXml".equals(templateName)) {
|
|
||||||
return entityName + "Mapper" + extension;
|
return entityName + "Mapper" + extension;
|
||||||
}
|
} else if ("API".equals(templateName)) {
|
||||||
if ("API".equals(templateName)) {
|
|
||||||
return StrUtil.toSymbolCase(entityName, '-') + extension;
|
return StrUtil.toSymbolCase(entityName, '-') + extension;
|
||||||
}
|
} else if ("VIEW".equals(templateName)) {
|
||||||
|
|
||||||
if ("VIEW".equals(templateName)) {
|
|
||||||
return "index.vue";
|
return "index.vue";
|
||||||
}
|
}
|
||||||
|
|
||||||
return entityName + templateName + extension;
|
return entityName + templateName + extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成文件路径
|
||||||
|
*
|
||||||
|
* @param templateName 模板名 Entity
|
||||||
|
* @param moduleName 模块名 system
|
||||||
|
* @param packageName 包名 com.youlai
|
||||||
|
* @param subPackageName 子包名 controller
|
||||||
|
* @param entityName 实体类名 UserController
|
||||||
|
* @return 文件路径 src/main/java/com/youlai/system/controller
|
||||||
|
*/
|
||||||
private String getFilePath(String templateName, String moduleName, String packageName, String subPackageName, String entityName) {
|
private String getFilePath(String templateName, String moduleName, String packageName, String subPackageName, String entityName) {
|
||||||
String path;
|
String path;
|
||||||
if ("MapperXml".equals(templateName)) {
|
if ("MapperXml".equals(templateName)) {
|
||||||
@@ -368,7 +400,8 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
bindMap.put("entityName", entityName);
|
bindMap.put("entityName", entityName);
|
||||||
bindMap.put("tableName", genConfig.getTableName());
|
bindMap.put("tableName", genConfig.getTableName());
|
||||||
bindMap.put("author", genConfig.getAuthor());
|
bindMap.put("author", genConfig.getAuthor());
|
||||||
bindMap.put("lowerFirstEntityName", StrUtil.lowerFirst(entityName));
|
bindMap.put("lowerFirstEntityName", StrUtil.lowerFirst(entityName)); // UserTest → userTest
|
||||||
|
bindMap.put("kebabCaseEntityName", StrUtil.toSymbolCase(entityName,'-')); // UserTest → user-test
|
||||||
bindMap.put("businessName", genConfig.getBusinessName());
|
bindMap.put("businessName", genConfig.getBusinessName());
|
||||||
bindMap.put("fieldConfigs", fieldConfigs);
|
bindMap.put("fieldConfigs", fieldConfigs);
|
||||||
|
|
||||||
@@ -405,6 +438,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载代码
|
* 下载代码
|
||||||
|
*
|
||||||
* @param tableNames 表名,可以支持多张表。
|
* @param tableNames 表名,可以支持多张表。
|
||||||
* @return 压缩文件字节数组
|
* @return 压缩文件字节数组
|
||||||
*/
|
*/
|
||||||
@@ -412,8 +446,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
public byte[] downloadCode(String[] tableNames) {
|
public byte[] downloadCode(String[] tableNames) {
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||||
for (String tableName : tableNames)
|
for (String tableName : tableNames) {
|
||||||
{
|
|
||||||
generatorCode(tableName, zip);
|
generatorCode(tableName, zip);
|
||||||
}
|
}
|
||||||
IOUtils.closeQuietly(zip);
|
IOUtils.closeQuietly(zip);
|
||||||
@@ -434,7 +467,7 @@ public class GeneratorServiceImpl implements GeneratorService {
|
|||||||
String path = previewVO.getPath();
|
String path = previewVO.getPath();
|
||||||
try {
|
try {
|
||||||
zip.putNextEntry(new java.util.zip.ZipEntry(path + File.separator + fileName));
|
zip.putNextEntry(new java.util.zip.ZipEntry(path + File.separator + fileName));
|
||||||
zip.write(content.getBytes("UTF-8"));
|
zip.write(content.getBytes(StandardCharsets.UTF_8));
|
||||||
zip.closeEntry();
|
zip.closeEntry();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ spring:
|
|||||||
|
|
||||||
# 代码生成器配置
|
# 代码生成器配置
|
||||||
generator:
|
generator:
|
||||||
|
# 下载代码文件名称
|
||||||
|
downloadFileName: youlai-admin-code.zip
|
||||||
# 后端项目名称
|
# 后端项目名称
|
||||||
backendAppName: youlai-boot
|
backendAppName: youlai-boot
|
||||||
# 前端项目名称
|
# 前端项目名称
|
||||||
@@ -19,6 +21,14 @@ generator:
|
|||||||
- gen_field_config
|
- gen_field_config
|
||||||
## 模板配置
|
## 模板配置
|
||||||
templateConfigs:
|
templateConfigs:
|
||||||
|
API:
|
||||||
|
templatePath: generator/api.ts.vm
|
||||||
|
subpackageName: api
|
||||||
|
extension: .ts
|
||||||
|
VIEW:
|
||||||
|
templatePath: generator/index.vue.vm
|
||||||
|
subpackageName: views
|
||||||
|
extension: .vue
|
||||||
Controller:
|
Controller:
|
||||||
templatePath: generator/controller.java.vm
|
templatePath: generator/controller.java.vm
|
||||||
subpackageName: controller
|
subpackageName: controller
|
||||||
@@ -50,13 +60,5 @@ generator:
|
|||||||
Entity:
|
Entity:
|
||||||
templatePath: generator/entity.java.vm
|
templatePath: generator/entity.java.vm
|
||||||
subpackageName: model.entity
|
subpackageName: model.entity
|
||||||
API:
|
|
||||||
templatePath: generator/api.ts.vm
|
|
||||||
subpackageName: api
|
|
||||||
extension: .ts
|
|
||||||
VIEW:
|
|
||||||
templatePath: generator/index.vue.vm
|
|
||||||
subpackageName: views
|
|
||||||
extension: .vue
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -250,7 +250,7 @@
|
|||||||
inheritAttrs: false,
|
inheritAttrs: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
import ${entityName}API, { ${entityName}PageVO, ${entityName}Form, ${entityName}PageQuery } from "@/api/${lowerFirstEntityName}";
|
import ${entityName}API, { ${entityName}PageVO, ${entityName}Form, ${entityName}PageQuery } from "@/api/${kebabCaseEntityName}";
|
||||||
|
|
||||||
const queryFormRef = ref(ElForm);
|
const queryFormRef = ref(ElForm);
|
||||||
const dataFormRef = ref(ElForm);
|
const dataFormRef = ref(ElForm);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import lombok.Getter;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
#if(${hasLocalDateTime})
|
#if(${hasLocalDateTime})
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
#end
|
#end
|
||||||
#if(${hasBigDecimal})
|
#if(${hasBigDecimal})
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|||||||
Reference in New Issue
Block a user