From 9b4433fddf5b401edb0aace8a404ac733b122702 Mon Sep 17 00:00:00 2001 From: 田源 <tianyuan@vci-tech.com> Date: 星期四, 03 四月 2025 14:35:02 +0800 Subject: [PATCH] 添加非密字段显示 --- Source/BladeX-Tool/blade-starter-develop/src/main/java/org/springblade/develop/support/BladeCodeGenerator.java | 356 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 356 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-develop/src/main/java/org/springblade/develop/support/BladeCodeGenerator.java b/Source/BladeX-Tool/blade-starter-develop/src/main/java/org/springblade/develop/support/BladeCodeGenerator.java new file mode 100644 index 0000000..d56a569 --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-develop/src/main/java/org/springblade/develop/support/BladeCodeGenerator.java @@ -0,0 +1,356 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 搴勯獮 (smallchill@163.com) + */ +package org.springblade.develop.support; + +import com.baomidou.mybatisplus.generator.FastAutoGenerator; +import com.baomidou.mybatisplus.generator.config.TemplateType; +import com.baomidou.mybatisplus.generator.config.rules.DateType; +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.annotations.Mapper; +import org.springblade.core.tool.utils.Func; +import org.springblade.core.tool.utils.StringUtil; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PropertiesLoaderUtils; + +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; + +import static org.springblade.develop.constant.DevelopConstant.*; + +/** + * 浠g爜鐢熸垚鍣ㄩ厤缃被 + * + * @author Chill + */ +@Data +@Slf4j +public class BladeCodeGenerator { + /** + * 浠g爜椋庢牸 + */ + private String codeStyle = SABER_NAME; + /** + * 浠g爜妯″潡鍚嶇О + */ + private String codeName; + /** + * 妯″瀷缂栧彿 + */ + private String modelCode; + /** + * 妯″瀷瀹炰綋绫� + */ + private String modelClass; + /** + * 浠g爜鎵�鍦ㄦ湇鍔″悕 + */ + private String serviceName = "blade-service"; + /** + * 浠g爜鐢熸垚鐨勫寘鍚� + */ + private String packageName = "org.springblade.test"; + /** + * 妯$増绫诲瀷 + */ + private String templateType; + /** + * 浣滆�呬俊鎭� + */ + private String author; + /** + * 瀛愯〃妯″瀷涓婚敭 + */ + private String subModelId; + /** + * 瀛愯〃缁戝畾澶栭敭 + */ + private String subFkId; + /** + * 鏍戜富閿瓧娈� + */ + private String treeId; + /** + * 鏍戠埗涓婚敭瀛楁 + */ + private String treePid; + /** + * 鏍戝悕绉板瓧娈� + */ + private String treeName; + /** + * 浠g爜鍚庣鐢熸垚鐨勫湴鍧� + */ + private String packageDir; + /** + * 浠g爜鍓嶇鐢熸垚鐨勫湴鍧� + */ + private String packageWebDir; + /** + * 闇�瑕佸幓鎺夌殑琛ㄥ墠缂� + */ + private String[] tablePrefix = {"blade_"}; + /** + * 闇�瑕佺敓鎴愮殑琛ㄥ悕(涓よ�呭彧鑳藉彇鍏朵竴) + */ + private String[] includeTables = {"blade_test"}; + /** + * 闇�瑕佹帓闄ょ殑琛ㄥ悕(涓よ�呭彧鑳藉彇鍏朵竴) + */ + private String[] excludeTables = {}; + /** + * 鏄惁鍖呭惈鍩虹涓氬姟瀛楁 + */ + private Boolean hasSuperEntity = Boolean.TRUE; + /** + * 鏄惁鍖呭惈鍖呰鍣� + */ + private Boolean hasWrapper = Boolean.TRUE; + /** + * 鏄惁鍖呭惈杩滅▼璋冪敤 + */ + private Boolean hasFeign = Boolean.FALSE; + /** + * 鏄惁鍖呭惈鏈嶅姟鍚� + */ + private Boolean hasServiceName = Boolean.FALSE; + /** + * 鍩虹涓氬姟瀛楁 + */ + private String[] superEntityColumns = {"create_time", "create_user", "create_dept", "update_time", "update_user", "status", "is_deleted"}; + /** + * 绉熸埛瀛楁 + */ + private String tenantColumn = "tenant_id"; + /** + * 鏁版嵁搴撻┍鍔ㄥ悕 + */ + private String driverName; + /** + * 鏁版嵁搴撻摼鎺ュ湴鍧� + */ + private String url; + /** + * 鏁版嵁搴撶敤鎴峰悕 + */ + private String username; + /** + * 鏁版嵁搴撳瘑鐮� + */ + private String password; + /** + * 鏁版嵁妯″瀷 + */ + private Map<String, Object> model; + /** + * 鏁版嵁鍘熷瀷 + */ + private List<Map<String, Object>> prototypes; + /** + * 瀛愭暟鎹ā鍨� + */ + private Map<String, Object> subModel; + /** + * 瀛愭暟鎹師鍨� + */ + private List<Map<String, Object>> subPrototypes; + + /** + * 浠g爜鐢熸垚鎵ц + */ + public void run() { + // 涓绘ā鍧椾唬鐮佺敓鎴� + getAutoGenerator(getCustomMap(TEMPLATE_MAIN), getCustomFile(TEMPLATE_MAIN)).templateEngine(new BladeTemplateEngine(getOutputDir(), getOutputWebDir())).execute(); + // 瀛愭ā鍧椾唬鐮佺敓鎴� + if (Func.equals(templateType, TEMPLATE_SUB) && StringUtil.isNotBlank(subModelId)) { + getAutoGenerator(getCustomMap(TEMPLATE_SUB), getCustomFile(TEMPLATE_SUB)).templateEngine(new BladeTemplateEngine(getOutputDir(), getOutputWebDir())).execute(); + } + } + + /** + * 璁剧疆 customMap + */ + private Map<String, Object> getCustomMap(String generateType) { + List<Map<String, Object>> prototypeList; + String[] split = packageName.split("\\."); + String serviceCode = split[split.length - 1]; + Map<String, Object> customMap = new HashMap<>(11); + customMap.put("generateType", generateType); + customMap.put("codeName", codeName); + customMap.put("serviceName", serviceName); + customMap.put("serviceCode", serviceCode); + customMap.put("packageName", packageName); + customMap.put("tenantColumn", tenantColumn); + customMap.put("hasWrapper", hasWrapper); + customMap.put("hasServiceName", hasServiceName); + customMap.put("templateType", templateType); + customMap.put("author", author); + customMap.put("subModelId", subModelId); + customMap.put("subFkId", subFkId); + customMap.put("treeId", treeId); + customMap.put("treePid", treePid); + customMap.put("treeName", treeName); + customMap.put("subFkIdHump", StringUtil.underlineToHump(subFkId)); + customMap.put("treeIdHump", StringUtil.underlineToHump(treeId)); + customMap.put("treePidHump", StringUtil.underlineToHump(treePid)); + if (Func.equals(generateType, TEMPLATE_SUB)) { + prototypeList = subPrototypes; + customMap.put("model", subModel); + customMap.put("prototypes", subPrototypes); + customMap.put("modelCode", subModel.get("modelCode")); + customMap.put("modelClass", subModel.get("modelClass")); + customMap.put("modelTable", subModel.get("modelTable")); + } else { + prototypeList = prototypes; + customMap.put("model", model); + customMap.put("prototypes", prototypes); + customMap.put("subModel", subModel); + customMap.put("subPrototypes", subPrototypes); + customMap.put("modelCode", model.get("modelCode")); + customMap.put("modelClass", model.get("modelClass")); + customMap.put("modelTable", model.get("modelTable")); + } + List<String> propertyImport = prototypeList.stream().filter(prototype -> { + String propertyType = String.valueOf(prototype.get("propertyType")); + return !"String".equals(propertyType) && !"Integer".equals(propertyType) && !"Long".equals(propertyType); + }).map(prototype -> String.valueOf(prototype.get("propertyEntity"))).distinct().collect(Collectors.toList()); + customMap.put("propertyImport", propertyImport); + return customMap; + } + + /** + * 璁剧疆 customFile + */ + private Map<String, String> getCustomFile(String type) { + Map<String, String> customFile = new HashMap<>(15); + if (!Func.equals(type, TEMPLATE_SUB)) { + customFile.put("menu.sql", "/templates/sql/menu.sql.btl"); + } + customFile.put("entityVO.java", "/templates/api/entityVO.java.btl"); + customFile.put("entityDTO.java", "/templates/api/entityDTO.java.btl"); + if (hasWrapper) { + customFile.put("wrapper.java", "/templates/api/wrapper.java.btl"); + } + if (hasFeign) { + customFile.put("feign.java", "/templates/api/feign.java.btl"); + customFile.put("feignclient.java", "/templates/api/feignclient.java.btl"); + } + if (Func.isNotBlank(packageWebDir)) { + if (Func.equals(codeStyle, SWORD_NAME)) { + customFile.put("action.js", "/templates/sword/action.js.btl"); + customFile.put("model.js", "/templates/sword/model.js.btl"); + customFile.put("service.js", "/templates/sword/service.js.btl"); + customFile.put("list.js", "/templates/sword/list.js.btl"); + customFile.put("add.js", "/templates/sword/add.js.btl"); + customFile.put("edit.js", "/templates/sword/edit.js.btl"); + customFile.put("view.js", "/templates/sword/view.js.btl"); + } else if (Func.equals(codeStyle, SABER_NAME)) { + customFile.put("api.js", "/templates/saber/" + templateType + "/api.js.btl"); + customFile.put("const.js", "/templates/saber/" + templateType + "/const.js.btl"); + if (!Func.equals(type, TEMPLATE_SUB)) { + customFile.put("crud.vue", "/templates/saber/" + templateType + "/crud.vue.btl"); + } + } else if (Func.equals(codeStyle, ELEMENT_NAME)) { + customFile.put("api.js", "/templates/element/" + templateType + "/api.js.btl"); + customFile.put("const.js", "/templates/element/" + templateType + "/const.js.btl"); + if (!Func.equals(type, TEMPLATE_SUB)) { + customFile.put("crud.vue", "/templates/element/" + templateType + "/crud.vue.btl"); + } else { + customFile.put("sub.vue", "/templates/element/" + templateType + "/sub.vue.btl"); + } + } else if (Func.equals(codeStyle, LEMON_NAME)) { + customFile.put("data.ts", "/templates/lemon/" + templateType + "/data.ts.btl"); + customFile.put("Modal.vue", "/templates/lemon/" + templateType + "/Modal.vue.btl"); + customFile.put("data.data.ts", "/templates/lemon/" + templateType + "/data.data.ts.btl"); + if (!Func.equals(type, TEMPLATE_SUB)) { + customFile.put("index.vue", "/templates/lemon/" + templateType + "/index.vue.btl"); + } else { + customFile.put("lemonSub.vue", "/templates/lemon/" + templateType + "/sub.vue.btl"); + } + + } + } + return customFile; + } + + private FastAutoGenerator getAutoGenerator(Map<String, Object> customMap, Map<String, String> customFile) { + Properties props = getProperties(); + String url = Func.toStr(this.url, props.getProperty("spring.datasource.url")); + String username = Func.toStr(this.username, props.getProperty("spring.datasource.username")); + String password = Func.toStr(this.password, props.getProperty("spring.datasource.password")); + return FastAutoGenerator.create(url, username, password) + .globalConfig(builder -> builder.author(StringUtil.isBlank(author) ? props.getProperty("author") : author).dateType(DateType.TIME_PACK).enableSwagger().outputDir(getOutputDir()).disableOpenDir()) + .packageConfig(builder -> builder.parent(packageName).controller("controller").entity("entity").service("service").serviceImpl("service.impl").mapper("mapper").xml("mapper")) + .strategyConfig(builder -> builder.addTablePrefix(tablePrefix).addInclude(Func.toStrArray(String.valueOf(customMap.get("modelTable")))).addExclude(excludeTables) + .entityBuilder().naming(NamingStrategy.underline_to_camel).columnNaming(NamingStrategy.underline_to_camel).enableLombok().superClass("org.springblade.core.mp.base.BaseEntity").formatFileName("%sEntity").addSuperEntityColumns(superEntityColumns).enableFileOverride() + .serviceBuilder().superServiceClass("org.springblade.core.mp.base.BaseService").superServiceImplClass("org.springblade.core.mp.base.BaseServiceImpl").formatServiceFileName("I%sService").formatServiceImplFileName("%sServiceImpl").enableFileOverride() + .mapperBuilder().mapperAnnotation(Mapper.class).enableBaseResultMap().enableBaseColumnList().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").enableFileOverride() + .controllerBuilder().superClass("org.springblade.core.boot.ctrl.BladeController").formatFileName("%sController").enableRestStyle().enableHyphenStyle().enableFileOverride() + ) + .templateConfig(builder -> builder.disable(TemplateType.ENTITY) + .entity("/templates/api/entity.java") + .service("/templates/api/service.java") + .serviceImpl("/templates/api/serviceImpl.java") + .mapper("/templates/api/mapper.java") + .xml("/templates/api/mapper.xml") + .controller("/templates/api/controller.java")) + .injectionConfig(builder -> builder.beforeOutputFile( + (tableInfo, objectMap) -> System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size()) + ).customMap(customMap).customFile(customFile) + ); + } + + /** + * 鑾峰彇閰嶇疆鏂囦欢 + * + * @return 閰嶇疆Props + */ + private Properties getProperties() { + // 璇诲彇閰嶇疆鏂囦欢 + Resource resource = new ClassPathResource("/templates/code.properties"); + Properties props = new Properties(); + try { + props = PropertiesLoaderUtils.loadProperties(resource); + } catch (IOException e) { + e.printStackTrace(); + } + return props; + } + + /** + * 鐢熸垚鍒伴」鐩腑 + * + * @return outputDir + */ + public String getOutputDir() { + return (Func.isBlank(packageDir) ? System.getProperty("user.dir") + "/blade-ops/blade-develop" : packageDir) + "/src/main/java"; + } + + + /** + * 鐢熸垚鍒癢eb椤圭洰涓� + * + * @return outputDir + */ + public String getOutputWebDir() { + return (Func.isBlank(packageWebDir) ? System.getProperty("user.dir") : packageWebDir) + "/src"; + } + +} -- Gitblit v1.9.3