mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 10:57:13 +08:00 
			
		
		
		
	| @@ -16,14 +16,13 @@ | ||||
|  | ||||
| package top.charles7c.continew.admin.generator.config.properties; | ||||
|  | ||||
| import java.util.Map; | ||||
|  | ||||
| import cn.hutool.core.map.MapUtil; | ||||
| import lombok.Data; | ||||
|  | ||||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||||
| import org.springframework.stereotype.Component; | ||||
|  | ||||
| import cn.hutool.core.map.MapUtil; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * 代码生成器配置属性 | ||||
| @@ -39,7 +38,12 @@ public class GeneratorProperties { | ||||
|     /** | ||||
|      * 排除数据表(哪些数据表不展示在代码生成中) | ||||
|      */ | ||||
|     private String[] excludeTables; | ||||
|     private String[] excludeTables = new String[0]; | ||||
|  | ||||
|     /** | ||||
|      * 类型映射 | ||||
|      */ | ||||
|     private Map<String, Map<String, List<String>>> typeMappings = MapUtil.newHashMap(); | ||||
|  | ||||
|     /** | ||||
|      * 模板配置 | ||||
|   | ||||
| @@ -19,8 +19,6 @@ package top.charles7c.continew.admin.generator.model.entity; | ||||
| import cn.hutool.core.convert.Convert; | ||||
| import cn.hutool.core.util.StrUtil; | ||||
| import cn.hutool.db.meta.Column; | ||||
| import cn.hutool.setting.dialect.Props; | ||||
| import cn.hutool.setting.dialect.PropsUtil; | ||||
| import com.baomidou.mybatisplus.annotation.FieldFill; | ||||
| import com.baomidou.mybatisplus.annotation.TableField; | ||||
| import com.baomidou.mybatisplus.annotation.TableName; | ||||
| @@ -169,10 +167,4 @@ public class FieldConfigDO implements Serializable { | ||||
|         this.columnName = columnName; | ||||
|         this.fieldName = StrUtil.toCamelCase(this.columnName); | ||||
|     } | ||||
|  | ||||
|     public void setColumnType(String columnType) { | ||||
|         this.columnType = columnType; | ||||
|         Props generatorProp = PropsUtil.get("generator"); | ||||
|         this.fieldType = generatorProp.getStr(columnType); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -46,6 +46,7 @@ import top.charles7c.continew.admin.generator.service.GeneratorService; | ||||
| import top.charles7c.continew.starter.core.constant.StringConstants; | ||||
| import top.charles7c.continew.starter.core.exception.BusinessException; | ||||
| import top.charles7c.continew.starter.core.util.TemplateUtils; | ||||
| import top.charles7c.continew.starter.data.core.enums.DatabaseType; | ||||
| import top.charles7c.continew.starter.data.core.util.MetaUtils; | ||||
| import top.charles7c.continew.starter.data.core.util.Table; | ||||
| import top.charles7c.continew.starter.core.util.validate.CheckUtils; | ||||
| @@ -136,6 +137,12 @@ public class GeneratorServiceImpl implements GeneratorService { | ||||
|         List<FieldConfigDO> latestFieldConfigList = new ArrayList<>(); | ||||
|         // 获取最新数据表列信息 | ||||
|         Collection<Column> columnList = MetaUtils.getColumns(dataSource, tableName); | ||||
|         // 获取数据库对应的类型映射配置 | ||||
|         DatabaseType databaseType = MetaUtils.getDatabaseType(dataSource); | ||||
|         Map<String, List<String>> typeMappingMap = generatorProperties.getTypeMappings() | ||||
|             .get(databaseType.getDatabase()); | ||||
|         CheckUtils.throwIfEmpty(typeMappingMap, "请先配置对应数据库的类型映射"); | ||||
|         Set<Map.Entry<String, List<String>>> typeMappingEntrySet = typeMappingMap.entrySet(); | ||||
|         // 新增或更新字段配置 | ||||
|         Map<String, FieldConfigDO> fieldConfigMap = fieldConfigList.stream() | ||||
|             .collect(Collectors.toMap(FieldConfigDO::getColumnName, Function.identity(), (key1, key2) -> key2)); | ||||
| @@ -143,13 +150,19 @@ public class GeneratorServiceImpl implements GeneratorService { | ||||
|         for (Column column : columnList) { | ||||
|             FieldConfigDO fieldConfig = Optional.ofNullable(fieldConfigMap.get(column.getName())) | ||||
|                 .orElseGet(() -> new FieldConfigDO(column)); | ||||
|             fieldConfig.setFieldSort(i++); | ||||
|             // 更新已有字段配置 | ||||
|             if (null != fieldConfig.getCreateTime()) { | ||||
|                 String columnType = StrUtil.splitToArray(column.getTypeName(), StringConstants.SPACE)[0].toLowerCase(); | ||||
|                 fieldConfig.setColumnType(columnType); | ||||
|                 fieldConfig.setColumnSize(Convert.toStr(column.getSize())); | ||||
|             } | ||||
|             String fieldType = typeMappingEntrySet.stream() | ||||
|                 .filter(entry -> entry.getValue().contains(fieldConfig.getColumnType())) | ||||
|                 .map(Map.Entry::getKey) | ||||
|                 .findFirst() | ||||
|                 .orElse(null); | ||||
|             fieldConfig.setFieldType(fieldType); | ||||
|             fieldConfig.setFieldSort(i++); | ||||
|             latestFieldConfigList.add(fieldConfig); | ||||
|         } | ||||
|         return latestFieldConfigList; | ||||
| @@ -232,13 +245,13 @@ public class GeneratorServiceImpl implements GeneratorService { | ||||
|         // 渲染前端代码 | ||||
|         // api 代码 | ||||
|         genConfigMap.put("fieldConfigs", fieldConfigList); | ||||
|         String apiContent = TemplateUtils.render("generator/api.ftl", genConfigMap); | ||||
|         String apiContent = TemplateUtils.render("api.ftl", genConfigMap); | ||||
|         GeneratePreviewResp apiCodePreview = new GeneratePreviewResp(); | ||||
|         apiCodePreview.setFileName(classNamePrefix.toLowerCase() + ".ts"); | ||||
|         apiCodePreview.setContent(apiContent); | ||||
|         generatePreviewList.add(apiCodePreview); | ||||
|         // view 代码 | ||||
|         String viewContent = TemplateUtils.render("generator/index.ftl", genConfigMap); | ||||
|         String viewContent = TemplateUtils.render("index.ftl", genConfigMap); | ||||
|         GeneratePreviewResp viewCodePreview = new GeneratePreviewResp(); | ||||
|         viewCodePreview.setFileName("index.vue"); | ||||
|         viewCodePreview.setContent(viewContent); | ||||
| @@ -260,9 +273,9 @@ public class GeneratorServiceImpl implements GeneratorService { | ||||
|             // 1.确定后端代码基础路径 | ||||
|             // 例如:D:/continew-admin | ||||
|             String projectPath = SystemUtil.getUserInfo().getCurrentDir(); | ||||
|             // 例如:D:/continew-admin/continew-admin-tool | ||||
|             // 例如:D:/continew-admin/continew-admin-system | ||||
|             File backendModuleFile = new File(projectPath, genConfig.getModuleName()); | ||||
|             // 例如:D:/continew-admin/continew-admin-tool/src/main/java/top/charles7c/continew/admin/tool | ||||
|             // 例如:D:/continew-admin/continew-admin-system/src/main/java/top/charles7c/continew/admin/system | ||||
|             List<String> backendModuleChildPathList = CollUtil.newArrayList("src", "main", "java"); | ||||
|             backendModuleChildPathList.addAll(StrUtil.split(packageName, StringConstants.DOT)); | ||||
|             File backendParentFile = FileUtil.file(backendModuleFile, backendModuleChildPathList | ||||
| @@ -273,7 +286,7 @@ public class GeneratorServiceImpl implements GeneratorService { | ||||
|                 .toList(); | ||||
|             Map<String, TemplateConfig> templateConfigMap = generatorProperties.getTemplateConfigs(); | ||||
|             for (GeneratePreviewResp codePreview : backendCodePreviewList) { | ||||
|                 // 例如:D:/continew-admin/continew-admin-tool/src/main/java/top/charles7c/continew/admin/tool/service/impl/XxxServiceImpl.java | ||||
|                 // 例如:D:/continew-admin/continew-admin-system/src/main/java/top/charles7c/continew/admin/system/service/impl/XxxServiceImpl.java | ||||
|                 TemplateConfig templateConfig = templateConfigMap.get(codePreview.getFileName() | ||||
|                     .replace(classNamePrefix, StringConstants.EMPTY) | ||||
|                     .replace(FileNameUtil.EXT_JAVA, StringConstants.EMPTY)); | ||||
| @@ -310,7 +323,7 @@ public class GeneratorServiceImpl implements GeneratorService { | ||||
|             FileUtil.writeUtf8String(apiCodePreview.getContent(), apiFile); | ||||
|             // 2.生成 view 代码 | ||||
|             GeneratePreviewResp viewCodePreview = frontendCodePreviewList.get(1); | ||||
|             // 例如:D:/continew-admin-ui/src/views/tool/xxx/index.vue | ||||
|             // 例如:D:/continew-admin-ui/src/views/system/xxx/index.vue | ||||
|             File indexFile = FileUtil.file(frontendPath, apiModuleName, StrUtil | ||||
|                 .lowerFirst(classNamePrefix), "index.vue"); | ||||
|             if (indexFile.exists() && Boolean.FALSE.equals(isOverride)) { | ||||
|   | ||||
| @@ -1,56 +0,0 @@ | ||||
| --- ### 代码生成器配置 | ||||
| generator: | ||||
|   # 排除数据表 | ||||
|   excludeTables: | ||||
|     - DATABASECHANGELOG | ||||
|     - DATABASECHANGELOGLOCK | ||||
|     - gen_config | ||||
|     - gen_field_config | ||||
|   # 模板配置 | ||||
|   templateConfigs: | ||||
|     DO: | ||||
|       # 模板路径 | ||||
|       templatePath: generator/Entity.ftl | ||||
|       # 包名称 | ||||
|       packageName: model.entity | ||||
|       # 排除字段 | ||||
|       excludeFields: | ||||
|         - id | ||||
|         - createUser | ||||
|         - createTime | ||||
|         - updateUser | ||||
|         - updateTime | ||||
|     Query: | ||||
|       templatePath: generator/Query.ftl | ||||
|       packageName: model.query | ||||
|     Req: | ||||
|       templatePath: generator/Req.ftl | ||||
|       packageName: model.req | ||||
|     Resp: | ||||
|       templatePath: generator/Resp.ftl | ||||
|       packageName: model.resp | ||||
|       excludeFields: | ||||
|         - id | ||||
|         - createUser | ||||
|         - createTime | ||||
|     DetailResp: | ||||
|       templatePath: generator/DetailResp.ftl | ||||
|       packageName: model.resp | ||||
|       excludeFields: | ||||
|         - id | ||||
|         - createUser | ||||
|         - createTime | ||||
|         - updateUser | ||||
|         - updateTime | ||||
|     Mapper: | ||||
|       templatePath: generator/Mapper.ftl | ||||
|       packageName: mapper | ||||
|     Service: | ||||
|       templatePath: generator/Service.ftl | ||||
|       packageName: service | ||||
|     ServiceImpl: | ||||
|       templatePath: generator/ServiceImpl.ftl | ||||
|       packageName: service.impl | ||||
|     Controller: | ||||
|       templatePath: generator/Controller.ftl | ||||
|       packageName: controller | ||||
| @@ -1,26 +0,0 @@ | ||||
| tinyint=Integer | ||||
| smallint=Integer | ||||
| mediumint=Integer | ||||
| int=Integer | ||||
| integer=Integer | ||||
|  | ||||
| bigint=Long | ||||
|  | ||||
| float=Float | ||||
|  | ||||
| double=Double | ||||
|  | ||||
| decimal=BigDecimal | ||||
|  | ||||
| bit=Boolean | ||||
|  | ||||
| char=String | ||||
| varchar=String | ||||
| tinytext=String | ||||
| text=String | ||||
| mediumtext=String | ||||
| longtext=String | ||||
|  | ||||
| date=LocalDate | ||||
| datetime=LocalDateTime | ||||
| timestamp=LocalDateTime | ||||
		Reference in New Issue
	
	Block a user