diff --git a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/config/properties/GeneratorProperties.java b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/config/properties/GeneratorProperties.java index a6a99d6ff57125982f60c92645134aa6fb2e7dd1..24eeea21de3a9bd899893a183e17fa78971c6c64 100644 --- a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/config/properties/GeneratorProperties.java +++ b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/config/properties/GeneratorProperties.java @@ -16,11 +16,15 @@ package top.charles7c.cnadmin.tool.config.properties; +import java.util.Map; + import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; +import cn.hutool.core.map.MapUtil; + /** * 代码生成器配置属性 * @@ -36,4 +40,26 @@ public class GeneratorProperties { * 排除数据表(哪些数据表不展示在代码生成中) */ private String[] excludeTables; + + /** + * 模板配置 + */ + private Map templateConfigs = MapUtil.newHashMap(true); + + /** + * 模板配置 + */ + @Data + public static class TemplateConfig { + + /** + * 模板路径 + */ + private String templatePath; + + /** + * 包名称 + */ + private String packageName; + } } diff --git a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/entity/GenConfigDO.java b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/entity/GenConfigDO.java index 163662fe4b111e133926b4eaf8172e1b0fd92bef..b20af25c7b88d313d925dd77f36346896d3cab43 100644 --- a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/entity/GenConfigDO.java +++ b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/entity/GenConfigDO.java @@ -18,19 +18,25 @@ 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; import javax.validation.constraints.Pattern; +import lombok.AccessLevel; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.Setter; import io.swagger.v3.oas.annotations.media.Schema; import org.hibernate.validator.constraints.Length; import com.baomidou.mybatisplus.annotation.*; +import com.fasterxml.jackson.annotation.JsonIgnore; + +import cn.hutool.core.util.StrUtil; import top.charles7c.cnadmin.common.constant.RegexConsts; @@ -78,6 +84,7 @@ public class GenConfigDO implements Serializable { */ @Schema(description = "前端路径") @Length(max = 255, message = "前端路径不能超过 {max} 个字符") + @Pattern(regexp = "^(?=.*src\\/views)(?!.*\\/views\\/?$).*", message = "前端路径配置错误") private String frontendPath; /** @@ -123,7 +130,28 @@ public class GenConfigDO implements Serializable { @TableField(fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; + /** + * 类名前缀 + */ + @Setter(AccessLevel.NONE) + @JsonIgnore + @TableField(exist = false) + private String classNamePrefix; + + /** + * 字段配置信息 + */ + @JsonIgnore + @TableField(exist = false) + private List fieldConfigs; + public GenConfigDO(String tableName) { this.tableName = tableName; } + + public String getClassNamePrefix() { + String rawClassName = StrUtil.isNotBlank(this.tablePrefix) + ? StrUtil.removePrefix(this.tableName, this.tablePrefix) : this.tableName; + return StrUtil.upperFirst(StrUtil.toCamelCase(rawClassName)); + } } diff --git a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/request/GenConfigRequest.java b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/request/GenConfigRequest.java index 09d283b0f7aa489374f228318b88a5f340987eab..16e24a1fa2c52e835d7e00420ae4d7d2bac71f5a 100644 --- a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/request/GenConfigRequest.java +++ b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/model/request/GenConfigRequest.java @@ -43,16 +43,16 @@ public class GenConfigRequest implements Serializable { private static final long serialVersionUID = 1L; /** - * 字段配置 + * 字段配置信息 */ - @Schema(description = "字段配置") + @Schema(description = "字段配置信息") @NotEmpty(message = "字段配置不能为空") private List fieldConfigs = new ArrayList<>(); /** - * 生成配置 + * 生成配置信息 */ - @Schema(description = "生成配置") + @Schema(description = "生成配置信息") @NotNull(message = "生成配置不能为空") private GenConfigDO genConfig; } diff --git a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/GeneratorService.java b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/GeneratorService.java index 3a3894c1ffae682eeefae7a9d0123f1995f8afaa..2ad614fa239bdd1f720d02d0b4f8b56d3a05f7f0 100644 --- a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/GeneratorService.java +++ b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/GeneratorService.java @@ -79,4 +79,12 @@ public interface GeneratorService { * 表名称 */ void saveConfig(GenConfigRequest request, String tableName); + + /** + * 生成代码 + * + * @param tableName + * 表名称 + */ + void generate(String tableName); } diff --git a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/impl/GeneratorServiceImpl.java b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/impl/GeneratorServiceImpl.java index c8bf495d4068b5bd1cea0e318caa78a4bbd09c48..b88addd7d975f68cf1dc0221c10e52d5edae1e68 100644 --- a/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/impl/GeneratorServiceImpl.java +++ b/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool/service/impl/GeneratorServiceImpl.java @@ -16,6 +16,8 @@ package top.charles7c.cnadmin.tool.service.impl; +import java.io.File; +import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -35,15 +37,24 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; import cn.hutool.core.bean.BeanUtil; 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; +import cn.hutool.system.SystemUtil; import top.charles7c.cnadmin.common.constant.StringConsts; +import top.charles7c.cnadmin.common.enums.QueryTypeEnum; +import top.charles7c.cnadmin.common.exception.ServiceException; import top.charles7c.cnadmin.common.model.query.PageQuery; import top.charles7c.cnadmin.common.model.vo.PageDataVO; +import top.charles7c.cnadmin.common.util.TemplateUtils; import top.charles7c.cnadmin.common.util.validate.CheckUtils; import top.charles7c.cnadmin.tool.config.properties.GeneratorProperties; +import top.charles7c.cnadmin.tool.config.properties.GeneratorProperties.TemplateConfig; import top.charles7c.cnadmin.tool.mapper.FieldConfigMapper; import top.charles7c.cnadmin.tool.mapper.GenConfigMapper; import top.charles7c.cnadmin.tool.model.entity.FieldConfigDO; @@ -182,6 +193,10 @@ public class GeneratorServiceImpl implements GeneratorService { // 保存或更新生成配置信息 GenConfigDO newGenConfig = request.getGenConfig(); + String frontendPath = newGenConfig.getFrontendPath(); + if (StrUtil.isNotBlank(frontendPath)) { + CheckUtils.throwIf(!StrUtil.containsAll(frontendPath, "src", "views"), "前端路径配置错误"); + } GenConfigDO oldGenConfig = genConfigMapper.selectById(tableName); if (null != oldGenConfig) { BeanUtil.copyProperties(newGenConfig, oldGenConfig); @@ -190,4 +205,122 @@ public class GeneratorServiceImpl implements GeneratorService { genConfigMapper.insert(newGenConfig); } } + + @Override + public void generate(String tableName) { + GenConfigDO genConfig = genConfigMapper.selectById(tableName); + CheckUtils.throwIfNull(genConfig, "请先进行数据表 [{}] 生成配置", tableName); + List fieldConfigList = fieldConfigMapper.selectListByTableName(tableName); + CheckUtils.throwIfEmpty(fieldConfigList, "请先进行数据表 [{}] 字段配置", tableName); + Map genConfigMap = this.pretreatment(genConfig, fieldConfigList); + + try { + String classNamePrefix = genConfig.getClassNamePrefix(); + Boolean isOverride = genConfig.getIsOverride(); + // 生成后端代码 + // 1、确定后端代码基础路径 + // 例如:D:/continew-admin + String projectPath = SystemUtil.getUserInfo().getCurrentDir(); + // 例如:D:/continew-admin/continew-admin-tool + File backendModuleFile = new File(projectPath, genConfig.getModuleName()); + // 例如:D:/continew-admin/continew-admin-tool/src/main/java/top/charles7c/cnadmin/tool + List backendModuleChildPathList = CollUtil.newArrayList("src", "main", "java"); + backendModuleChildPathList.addAll(StrUtil.split(genConfig.getPackageName(), StringConsts.DOT)); + File backendParentFile = + FileUtil.file(backendModuleFile, backendModuleChildPathList.toArray(new String[0])); + // 2、生成代码 + Map templateConfigMap = generatorProperties.getTemplateConfigs(); + for (Map.Entry 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)); + String className = classNamePrefix + StrUtil.nullToEmpty(templateConfigEntry.getKey()); + genConfigMap.put("className", className); + File classFile = new File(classParentFile, className + FileNameUtil.EXT_JAVA); + // 如果已经存在,且不允许覆盖,则跳过 + if (classFile.exists() && !isOverride) { + continue; + } + String content = TemplateUtils.render(templateConfig.getTemplatePath(), genConfigMap); + FileUtil.writeString(content, classFile, StandardCharsets.UTF_8); + } + + // 生成前端代码 + String frontendPath = genConfig.getFrontendPath(); + if (StrUtil.isBlank(frontendPath)) { + return; + } + // 1、生成 api 代码 + // 例如:D:/continew-admin/continew-admin-ui + List frontendSubPathList = StrUtil.split(frontendPath, "src"); + String frontendModulePath = frontendSubPathList.get(0); + // 例如:D:/continew-admin/continew-admin-ui/src/api/tool/xxx.ts + String moduleSimpleName = new File(frontendPath).getName(); + File apiParentFile = FileUtil.file(frontendModulePath, "src", "api", moduleSimpleName); + String apiFileName = classNamePrefix.toLowerCase() + ".ts"; + File apiFile = new File(apiParentFile, apiFileName); + if (apiFile.exists() && !isOverride) { + return; + } + String apiContent = TemplateUtils.render("generator/api.ftl", genConfigMap); + FileUtil.writeString(apiContent, apiFile, StandardCharsets.UTF_8); + // 2、生成 view 代码 + // 例如:D:/continew-admin/continew-admin-ui/src/views/tool/xxx/index.vue + File indexFile = FileUtil.file(frontendPath, classNamePrefix, "index.vue"); + if (indexFile.exists() && !isOverride) { + return; + } + String indexContent = TemplateUtils.render("generator/index.ftl", genConfigMap); + FileUtil.writeString(indexContent, indexFile, StandardCharsets.UTF_8); + } catch (Exception e) { + log.error("Generate code occurred an error: {}. tableName: {}.", e.getMessage(), tableName, e); + throw new ServiceException("代码生成失败,请手动清理生成文件"); + } + } + + /** + * 预处理生成配置 + * + * @param genConfig + * 生成配置 + * @param fieldConfigList + * 字段配置列表 + * @return 处理后的生成配置 + */ + private Map pretreatment(GenConfigDO genConfig, List fieldConfigList) { + Map genConfigMap = MapUtil.newHashMap(); + genConfigMap.put("date", DateUtil.date().toString("yyyy/MM/dd HH:mm")); + genConfigMap.put("hasLocalDateTime", false); + genConfigMap.put("hasBigDecimal", false); + genConfigMap.put("hasRequiredField", false); + genConfigMap.put("hasListQueryField", false); + for (FieldConfigDO fieldConfig : fieldConfigList) { + String fieldType = fieldConfig.getFieldType(); + if ("LocalDateTime".equals(fieldType)) { + genConfigMap.put("hasLocalDateTime", true); + } + if ("BigDecimal".equals(fieldType)) { + genConfigMap.put("hasLocalDateTime", true); + } + if (Boolean.TRUE.equals(fieldConfig.getIsRequired())) { + genConfigMap.put("hasRequiredField", true); + } + QueryTypeEnum queryType = fieldConfig.getQueryType(); + if (null != queryType && StrUtil.equalsAny(queryType.name(), QueryTypeEnum.IN.name(), + QueryTypeEnum.NOT_IN.name(), QueryTypeEnum.BETWEEN.name())) { + 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; + } } diff --git a/continew-admin-tool/src/main/resources/templates/generator/Controller.ftl b/continew-admin-tool/src/main/resources/templates/generator/Controller.ftl new file mode 100644 index 0000000000000000000000000000000000000000..5e1b647bc47e337afad8a33bedd3d9b65270bfc1 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/Controller.ftl @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +import static top.charles7c.cnadmin.common.annotation.CrudRequestMapping.Api; + +import io.swagger.v3.oas.annotations.tags.Tag; + +import org.springframework.web.bind.annotation.*; + +import top.charles7c.cnadmin.common.annotation.CrudRequestMapping; +import top.charles7c.cnadmin.common.base.BaseController; +import ${packageName}.model.query.${classNamePrefix}Query; +import ${packageName}.model.request.${classNamePrefix}Request; +import ${packageName}.model.vo.${classNamePrefix}DetailVO; +import ${packageName}.model.vo.${classNamePrefix}VO; +import ${packageName}.service.${classNamePrefix}Service; + +/** + * ${businessName}管理 API + * + * @author ${author} + * @since ${date} + */ +@Tag(name = "${businessName}管理 API") +@RestController +@CrudRequestMapping(value = "/${moduleName}/${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> {} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/DetailVO.ftl b/continew-admin-tool/src/main/resources/templates/generator/DetailVO.ftl new file mode 100644 index 0000000000000000000000000000000000000000..542cd0f9118f23b58921577b41c9f41d176949bb --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/DetailVO.ftl @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +<#if hasLocalDateTime> +import java.time.LocalDateTime; + +<#if hasBigDecimal> +import java.math.BigDecimal; + + +import lombok.Data; + +import io.swagger.v3.oas.annotations.media.Schema; + +import com.alibaba.excel.annotation.ExcelIgnoreUnannotated; +import com.alibaba.excel.annotation.ExcelProperty; + +import top.charles7c.cnadmin.common.base.BaseDetailVO; + +/** + * ${businessName}详情信息 + * + * @author ${author} + * @since ${date} + */ +@Data +@ExcelIgnoreUnannotated +@Schema(description = "${businessName}详情信息") +public class ${className} extends BaseDetailVO { + + private static final long serialVersionUID = 1L; +<#if fieldConfigs??> + <#list fieldConfigs as fieldConfig> + + /** + * ${fieldConfig.comment} + */ + @Schema(description = "${fieldConfig.comment}") + @ExcelProperty(value = "${fieldConfig.comment}") + private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; + + +} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/Entity.ftl b/continew-admin-tool/src/main/resources/templates/generator/Entity.ftl new file mode 100644 index 0000000000000000000000000000000000000000..1d93e0fa9f54893ae7175b633a1ca5e49a8c0140 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/Entity.ftl @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +<#if hasLocalDateTime> +import java.time.LocalDateTime; + +<#if hasBigDecimal> +import java.math.BigDecimal; + + +import lombok.Data; + +import com.baomidou.mybatisplus.annotation.TableName; + +import top.charles7c.cnadmin.common.base.BaseDO; + +/** + * ${businessName}实体 + * + * @author ${author} + * @since ${date} + */ +@Data +@TableName("${tableName}") +public class ${className} extends BaseDO { + + private static final long serialVersionUID = 1L; +<#if fieldConfigs??> + <#list fieldConfigs as fieldConfig> + + /** + * ${fieldConfig.comment} + */ + private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; + + +} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/Mapper.ftl b/continew-admin-tool/src/main/resources/templates/generator/Mapper.ftl new file mode 100644 index 0000000000000000000000000000000000000000..1aca1b4e5237011c9d36c8507b963aae796712df --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/Mapper.ftl @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +import top.charles7c.cnadmin.common.base.BaseMapper; +import ${packageName}.model.entity.${classNamePrefix}DO; + +/** +* ${businessName} Mapper +* +* @author ${author} +* @since ${date} +*/ +public interface ${className} extends BaseMapper<${classNamePrefix}DO> {} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/Query.ftl b/continew-admin-tool/src/main/resources/templates/generator/Query.ftl new file mode 100644 index 0000000000000000000000000000000000000000..77296bffb0924e137cf954758ca949f6cbcb9ad6 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/Query.ftl @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +import java.io.Serializable; +<#if hasLocalDateTime> +import java.time.LocalDateTime; + +<#if hasBigDecimal> +import java.math.BigDecimal; + +<#if hasListQueryField> +import java.util.List; + + +import lombok.Data; + +import io.swagger.v3.oas.annotations.media.Schema; + +import top.charles7c.cnadmin.common.annotation.Query; +import top.charles7c.cnadmin.common.enums.QueryTypeEnum; + +/** + * ${businessName}查询条件 + * + * @author ${author} + * @since ${date} + */ +@Data +@Schema(description = "${businessName}查询条件") +public class ${className} implements Serializable { + + private static final long serialVersionUID = 1L; +<#if fieldConfigs??> + <#list fieldConfigs as fieldConfig> + <#if fieldConfig.showInQuery> + + /** + * ${fieldConfig.comment} + */ + @Schema(description = "${fieldConfig.comment}") + @Query(type = QueryTypeEnum.${fieldConfig.queryType}) + <#if fieldConfig.queryType == 'IN' || fieldConfig.queryType == 'NOT_IN' || fieldConfig.queryType == 'BETWEEN'> + private List<${fieldConfig.fieldType}> ${fieldConfig.fieldName}; + <#else> + private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; + + + + +} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/Request.ftl b/continew-admin-tool/src/main/resources/templates/generator/Request.ftl new file mode 100644 index 0000000000000000000000000000000000000000..084163789934a3647b25ada3d02926fcf07c2ff4 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/Request.ftl @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +<#if hasLocalDateTime> +import java.time.LocalDateTime; + +<#if hasBigDecimal> +import java.math.BigDecimal; + + +<#if hasRequiredField> +import javax.validation.constraints.*; + + +import lombok.Data; + +import io.swagger.v3.oas.annotations.media.Schema; + +import top.charles7c.cnadmin.common.base.BaseRequest; + +/** + * 创建或修改${businessName}信息 + * + * @author ${author} + * @since ${date} + */ +@Data +@Schema(description = "创建或修改${businessName}信息") +public class ${className} extends BaseRequest { + + private static final long serialVersionUID = 1L; +<#if fieldConfigs??> + <#list fieldConfigs as fieldConfig> + <#if fieldConfig.showInForm> + + /** + * ${fieldConfig.comment} + */ + @Schema(description = "${fieldConfig.comment}") + <#if fieldConfig.isRequired> + <#if fieldConfig.fieldType = 'String'> + @NotBlank(message = "${fieldConfig.comment}不能为空") + <#else> + @NotNull(message = "${fieldConfig.comment}不能为空") + + + private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; + + + +} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/Service.ftl b/continew-admin-tool/src/main/resources/templates/generator/Service.ftl new file mode 100644 index 0000000000000000000000000000000000000000..7ce48b027e3f7f5699534a84c210e001e52c3fb6 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/Service.ftl @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +import top.charles7c.cnadmin.common.base.BaseService; +import ${packageName}.model.query.${classNamePrefix}Query; +import ${packageName}.model.request.${classNamePrefix}Request; +import ${packageName}.model.vo.${classNamePrefix}DetailVO; +import ${packageName}.model.vo.${classNamePrefix}VO; + +/** + * ${businessName}业务接口 + * + * @author ${author} + * @since ${date} + */ +public interface ${className} extends BaseService<${classNamePrefix}VO, ${classNamePrefix}DetailVO, ${classNamePrefix}Query, ${classNamePrefix}Request> {} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/ServiceImpl.ftl b/continew-admin-tool/src/main/resources/templates/generator/ServiceImpl.ftl new file mode 100644 index 0000000000000000000000000000000000000000..7089dc22af575a22ff5631d7ffed7af280718830 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/ServiceImpl.ftl @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +import lombok.RequiredArgsConstructor; + +import org.springframework.stereotype.Service; + +import top.charles7c.cnadmin.common.base.BaseServiceImpl; +import ${packageName}.mapper.${classNamePrefix}Mapper; +import ${packageName}.model.entity.${classNamePrefix}DO; +import ${packageName}.model.query.${classNamePrefix}Query; +import ${packageName}.model.request.${classNamePrefix}Request; +import ${packageName}.model.vo.${classNamePrefix}DetailVO; +import ${packageName}.model.vo.${classNamePrefix}VO; +import ${packageName}.service.${classNamePrefix}Service; + +/** + * ${businessName}业务实现 + * + * @author ${author} + * @since ${date} + */ +@Service +@RequiredArgsConstructor +public class ${className} extends BaseServiceImpl<${classNamePrefix}Mapper, ${classNamePrefix}DO, ${classNamePrefix}VO, ${classNamePrefix}DetailVO, ${classNamePrefix}Query, ${classNamePrefix}Request> implements ${classNamePrefix}Service {} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/VO.ftl b/continew-admin-tool/src/main/resources/templates/generator/VO.ftl new file mode 100644 index 0000000000000000000000000000000000000000..c93a10a6344e28def6a4a60f3e2188967cc80886 --- /dev/null +++ b/continew-admin-tool/src/main/resources/templates/generator/VO.ftl @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ${packageName}.${subPackageName}; + +<#if hasLocalDateTime> +import java.time.LocalDateTime; + +<#if hasBigDecimal> +import java.math.BigDecimal; + + +import lombok.Data; + +import io.swagger.v3.oas.annotations.media.Schema; + +import top.charles7c.cnadmin.common.base.BaseVO; + +/** + * ${businessName}信息 + * + * @author ${author} + * @since ${date} + */ +@Data +@Schema(description = "${businessName}信息") +public class ${className} extends BaseVO { + + private static final long serialVersionUID = 1L; +<#if fieldConfigs??> + <#list fieldConfigs as fieldConfig> + <#if fieldConfig.showInList> + + /** + * ${fieldConfig.comment} + */ + @Schema(description = "${fieldConfig.comment}") + private ${fieldConfig.fieldType} ${fieldConfig.fieldName}; + + + +} \ No newline at end of file diff --git a/continew-admin-tool/src/main/resources/templates/generator/api.ftl b/continew-admin-tool/src/main/resources/templates/generator/api.ftl new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/continew-admin-tool/src/main/resources/templates/generator/index.ftl b/continew-admin-tool/src/main/resources/templates/generator/index.ftl new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/continew-admin-ui/src/api/tool/generator.ts b/continew-admin-ui/src/api/tool/generator.ts index 90cf6d019cb99677a75f766a3b285312b37e4639..2cf793d3c27a953b2a2c01234fcf24da7314fbf4 100644 --- a/continew-admin-ui/src/api/tool/generator.ts +++ b/continew-admin-ui/src/api/tool/generator.ts @@ -75,3 +75,7 @@ export interface GeneratorConfigRecord { export function saveConfig(tableName: string, req: GeneratorConfigRecord) { return axios.post(`${BASE_URL}/config/${tableName}`, req); } + +export function generate(tableName: string) { + return axios.post(`${BASE_URL}/${tableName}`); +} diff --git a/continew-admin-ui/src/views/tool/generator/index.vue b/continew-admin-ui/src/views/tool/generator/index.vue index 3a0d2057c4237089c81db9d0a745167c98beaa9f..7006dd41b95272e5c9b4022d133bdc84aa960b01 100644 --- a/continew-admin-ui/src/views/tool/generator/index.vue +++ b/continew-admin-ui/src/views/tool/generator/index.vue @@ -298,6 +298,7 @@ getGenConfig, GeneratorConfigRecord, saveConfig, + generate, } from '@/api/tool/generator'; const { proxy } = getCurrentInstance() as any; @@ -434,7 +435,9 @@ * @param tableName 表名称 */ const handleGenerate = (tableName: string) => { - proxy.$message.info('功能尚在开发中'); + generate(tableName).then((res) => { + proxy.$message.success(res.msg); + }); }; /** diff --git a/continew-admin-webapi/src/main/java/top/charles7c/cnadmin/webapi/controller/tool/GeneratorController.java b/continew-admin-webapi/src/main/java/top/charles7c/cnadmin/webapi/controller/tool/GeneratorController.java index 9e87e09d976944c1605434f116d930150c02ee71..bbcc4121722d6984da6ac8afb6d3ff7b35a2050a 100644 --- a/continew-admin-webapi/src/main/java/top/charles7c/cnadmin/webapi/controller/tool/GeneratorController.java +++ b/continew-admin-webapi/src/main/java/top/charles7c/cnadmin/webapi/controller/tool/GeneratorController.java @@ -27,9 +27,12 @@ import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import cn.hutool.extra.spring.SpringUtil; + import top.charles7c.cnadmin.common.model.query.PageQuery; import top.charles7c.cnadmin.common.model.vo.PageDataVO; import top.charles7c.cnadmin.common.model.vo.R; +import top.charles7c.cnadmin.common.util.validate.ValidationUtils; import top.charles7c.cnadmin.tool.model.entity.FieldConfigDO; import top.charles7c.cnadmin.tool.model.entity.GenConfigDO; import top.charles7c.cnadmin.tool.model.query.TableQuery; @@ -77,4 +80,12 @@ public class GeneratorController { generatorService.saveConfig(request, tableName); return R.ok("保存成功"); } + + @Operation(summary = "生成代码", description = "生成代码") + @PostMapping("/{tableName}") + public R generate(@PathVariable String tableName) { + ValidationUtils.throwIf("prod".equals(SpringUtil.getActiveProfile()), "仅支持在开发环境生成代码"); + generatorService.generate(tableName); + return R.ok("生成成功,请查看生成代码是否正确"); + } } diff --git a/continew-admin-webapi/src/main/resources/application.yml b/continew-admin-webapi/src/main/resources/application.yml index 521ac4ac8c87733add1af46a823662f604a82193..55e3367d3b59088eafadffdbdd87a3d21b758616 100644 --- a/continew-admin-webapi/src/main/resources/application.yml +++ b/continew-admin-webapi/src/main/resources/application.yml @@ -45,15 +45,6 @@ logging: - password - Authorization ---- ### 代码生成器配置 -generator: - # 排除数据表 - excludeTables: - - DATABASECHANGELOG - - DATABASECHANGELOGLOCK - - gen_config - - gen_field_config - --- ### 接口文档配置 springdoc: # 设置对象型参数的展示形式(设为 true 表示将对象型参数平展开,即对象内的属性直接作为参数展示而不是嵌套在对象内,默认为 false) @@ -214,3 +205,41 @@ thread-pool: queueCapacity: 128 # 活跃时间 keepAliveSeconds: 300 + +--- ### 代码生成器配置 +generator: + # 排除数据表 + excludeTables: + - DATABASECHANGELOG + - DATABASECHANGELOGLOCK + - gen_config + - gen_field_config + # 模板配置 + templateConfigs: + DO: + templatePath: generator/Entity.ftl + packageName: model.entity + Query: + templatePath: generator/Query.ftl + packageName: model.query + Request: + templatePath: generator/Request.ftl + packageName: model.request + VO: + templatePath: generator/VO.ftl + packageName: model.vo + DetailVO: + templatePath: generator/DetailVO.ftl + packageName: model.vo + 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 \ No newline at end of file