refactor: 目录重构之后包名取值调整,添加默认包名

This commit is contained in:
ray
2024-09-01 10:29:28 +08:00
parent 177adad357
commit ea0db128a2
3 changed files with 28 additions and 11 deletions

View File

@@ -57,8 +57,14 @@ public class GeneratorProperties {
@Data @Data
public static class TemplateConfig { public static class TemplateConfig {
/**
* 模板路径 (e.g. /templates/generator/controller.java.vm)
*/
private String templatePath; private String templatePath;
/**
* 子包名 (e.g. controller/service/mapper/model)
*/
private String subpackageName; private String subpackageName;
/** /**
@@ -74,8 +80,16 @@ public class GeneratorProperties {
@Data @Data
public static class DefaultConfig { public static class DefaultConfig {
/**
* 作者 (e.g. Ray)
*/
private String author; private String author;
/**
* 默认模块名(e.g. system)
*/
private String moduleName;
} }

View File

@@ -42,7 +42,6 @@ import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
/** /**
* 数据库服务实现类 * 数据库服务实现类
* *
@@ -113,6 +112,7 @@ public class GeneratorServiceImpl implements GeneratorService {
genConfig.setEntityName(entityName); genConfig.setEntityName(entityName);
genConfig.setPackageName(YouLaiApplication.class.getPackageName()); genConfig.setPackageName(YouLaiApplication.class.getPackageName());
genConfig.setModuleName(generatorProperties.getDefaultConfig().getModuleName()); // 默认模块名
genConfig.setAuthor(generatorProperties.getDefaultConfig().getAuthor()); genConfig.setAuthor(generatorProperties.getDefaultConfig().getAuthor());
} }
@@ -225,7 +225,6 @@ public class GeneratorServiceImpl implements GeneratorService {
* 删除代码生成配置 * 删除代码生成配置
* *
* @param tableName 表名 * @param tableName 表名
* @return
*/ */
@Override @Override
public void deleteGenConfig(String tableName) { public void deleteGenConfig(String tableName) {
@@ -257,14 +256,18 @@ public class GeneratorServiceImpl implements GeneratorService {
GenConfig genConfig = genConfigService.getOne(new LambdaQueryWrapper<GenConfig>() GenConfig genConfig = genConfigService.getOne(new LambdaQueryWrapper<GenConfig>()
.eq(GenConfig::getTableName, tableName) .eq(GenConfig::getTableName, tableName)
); );
Assert.isTrue(genConfig != null, "未找到表生成配置"); if (genConfig == null) {
throw new BusinessException("未找到表生成配置");
}
List<GenFieldConfig> fieldConfigs = genFieldConfigService.list(new LambdaQueryWrapper<GenFieldConfig>() List<GenFieldConfig> fieldConfigs = genFieldConfigService.list(new LambdaQueryWrapper<GenFieldConfig>()
.eq(GenFieldConfig::getConfigId, genConfig.getId()) .eq(GenFieldConfig::getConfigId, genConfig.getId())
.orderByAsc(GenFieldConfig::getFieldSort) .orderByAsc(GenFieldConfig::getFieldSort)
); );
Assert.isTrue(CollectionUtil.isNotEmpty(fieldConfigs), "未找到字段生成配置"); if (CollectionUtil.isEmpty(fieldConfigs)) {
throw new BusinessException("未找到字段生成配置");
}
// 遍历模板配置 // 遍历模板配置
Map<String, GeneratorProperties.TemplateConfig> templateConfigs = generatorProperties.getTemplateConfigs(); Map<String, GeneratorProperties.TemplateConfig> templateConfigs = generatorProperties.getTemplateConfigs();
@@ -286,22 +289,21 @@ public class GeneratorServiceImpl implements GeneratorService {
previewVO.setFileName(fileName); previewVO.setFileName(fileName);
/* 2. 生成文件路径 */ /* 2. 生成文件路径 */
// com.youlai // 包名:com.youlai.boot
String packageName = genConfig.getPackageName(); String packageName = genConfig.getPackageName();
// system // 模块名:system
String moduleName = genConfig.getModuleName(); String moduleName = genConfig.getModuleName();
// controller // 子包名:controller
String subpackageName = templateConfig.getSubpackageName(); String subpackageName = templateConfig.getSubpackageName();
// 文件路径 src/main/java/com/youlai/system/controller // 组合成文件路径src/main/java/com/youlai/boot/system/controller
String filePath = getFilePath(templateName, moduleName, packageName, subpackageName, entityName); String filePath = getFilePath(templateName, moduleName, packageName, subpackageName, entityName);
previewVO.setPath(filePath); previewVO.setPath(filePath);
/* 3. 生成文件内容 */ /* 3. 生成文件内容 */
// 将模板文件中的变量替换为具体的值 生成代码内容
String content = getCodeContent(templateConfig, genConfig, fieldConfigs); String content = getCodeContent(templateConfig, genConfig, fieldConfigs);
previewVO.setContent(content); previewVO.setContent(content);
list.add(previewVO); list.add(previewVO);
} }
return list; return list;

View File

@@ -15,6 +15,7 @@ generator:
# 默认配置 # 默认配置
defaultConfig: defaultConfig:
author: youlaitech author: youlaitech
moduleName: system
# 排除数据表 # 排除数据表
excludeTables: excludeTables:
- gen_config - gen_config