From 1aa9e1d4b090b4166c309a1add5ca38cd951992d Mon Sep 17 00:00:00 2001 From: lbw Date: Sat, 31 Jul 2021 16:01:32 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20cl?= =?UTF-8?q?osed=20#I42RUU=20=E3=80=90=E6=96=B0=E9=9C=80=E6=B1=82=E3=80=91?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=E6=94=AF=E6=8C=81=E5=8E=9F?= =?UTF-8?q?=E7=94=9Felement=20=E4=BB=A3=E7=A0=81=E4=BD=9C=E4=B8=BAavue=20?= =?UTF-8?q?=E7=9A=84=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/GenFormConfServiceImpl.java | 7 +- .../service/impl/GeneratorServiceImpl.java | 10 +- .../CodeGenKits.java} | 120 ++++++++++------- .../pig/codegen/support/StyleTypeEnum.java | 36 +++++ .../resources/template/element/form.vue.vm | 93 +++++++++++++ .../resources/template/element/index.vue.vm | 126 ++++++++++++++++++ 6 files changed, 333 insertions(+), 59 deletions(-) rename pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/{util/CodeGenUtils.java => support/CodeGenKits.java} (82%) create mode 100644 pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/StyleTypeEnum.java create mode 100644 pig-visual/pig-codegen/src/main/resources/template/element/form.vue.vm create mode 100644 pig-visual/pig-codegen/src/main/resources/template/element/index.vue.vm diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GenFormConfServiceImpl.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GenFormConfServiceImpl.java index 00e80a98..36887547 100755 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GenFormConfServiceImpl.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GenFormConfServiceImpl.java @@ -24,7 +24,7 @@ import com.pig4cloud.pig.codegen.entity.GenFormConf; import com.pig4cloud.pig.codegen.mapper.GenFormConfMapper; import com.pig4cloud.pig.codegen.mapper.GeneratorMapper; import com.pig4cloud.pig.codegen.service.GenFormConfService; -import com.pig4cloud.pig.codegen.util.CodeGenUtils; +import com.pig4cloud.pig.codegen.support.CodeGenKits; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.apache.commons.lang.StringUtils; @@ -80,14 +80,13 @@ public class GenFormConfServiceImpl extends ServiceImpl column : columns) { ColumnEntity columnEntity = new ColumnEntity(); columnEntity.setComments(column.get("columnComment")); - columnEntity - .setLowerAttrName(StringUtils.uncapitalize(CodeGenUtils.columnToJava(column.get("columnName")))); + columnEntity.setLowerAttrName(StringUtils.uncapitalize(CodeGenKits.columnToJava(column.get("columnName")))); columnList.add(columnEntity); } context.put("columns", columnList); StringWriter writer = new StringWriter(); template.merge(context, writer); - return StrUtil.trim(StrUtil.removePrefix(writer.toString(), CodeGenUtils.CRUD_PREFIX)); + return StrUtil.trim(StrUtil.removePrefix(writer.toString(), CodeGenKits.CRUD_PREFIX)); } } diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java index 570ea10e..dbd4c99c 100755 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/service/impl/GeneratorServiceImpl.java @@ -29,7 +29,7 @@ import com.pig4cloud.pig.codegen.entity.GenFormConf; import com.pig4cloud.pig.codegen.mapper.GenFormConfMapper; import com.pig4cloud.pig.codegen.mapper.GeneratorMapper; import com.pig4cloud.pig.codegen.service.GeneratorService; -import com.pig4cloud.pig.codegen.util.CodeGenUtils; +import com.pig4cloud.pig.codegen.support.CodeGenKits; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -83,10 +83,10 @@ public class GeneratorServiceImpl implements GeneratorService { List> columns = generatorMapper.queryColumns(tableName, genConfig.getDsName()); // 生成代码 if (CollUtil.isNotEmpty(formConfList)) { - return CodeGenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0)); + return CodeGenKits.generatorCode(genConfig, table, columns, null, formConfList.get(0)); } else { - return CodeGenUtils.generatorCode(genConfig, table, columns, null, null); + return CodeGenKits.generatorCode(genConfig, table, columns, null, null); } } @@ -115,10 +115,10 @@ public class GeneratorServiceImpl implements GeneratorService { List> columns = generatorMapper.queryColumns(tableName, genConfig.getDsName()); // 生成代码 if (CollUtil.isNotEmpty(formConfList)) { - CodeGenUtils.generatorCode(genConfig, table, columns, zip, formConfList.get(0)); + CodeGenKits.generatorCode(genConfig, table, columns, zip, formConfList.get(0)); } else { - CodeGenUtils.generatorCode(genConfig, table, columns, zip, null); + CodeGenKits.generatorCode(genConfig, table, columns, zip, null); } } IoUtil.close(zip); diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/CodeGenKits.java similarity index 82% rename from pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java rename to pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/CodeGenKits.java index 613ce803..6d57ffa8 100644 --- a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/util/CodeGenUtils.java +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/CodeGenKits.java @@ -14,20 +14,7 @@ * limitations under the License. */ -package com.pig4cloud.pig.codegen.util; - -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Properties; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; +package com.pig4cloud.pig.codegen.support; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.IoUtil; @@ -51,6 +38,14 @@ import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.nio.charset.StandardCharsets; +import java.util.*; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + /** * 代码生成器 工具类 copy * elunez/eladmin/blob/master/eladmin-generator/src/main/java/me/zhengjie/utils/GenUtil.java @@ -61,7 +56,7 @@ import org.apache.velocity.app.Velocity; */ @Slf4j @UtilityClass -public class CodeGenUtils { +public class CodeGenKits { public final String CRUD_PREFIX = "export const tableOption ="; @@ -81,15 +76,21 @@ public class CodeGenUtils { private final String AVUE_INDEX_VUE_VM = "avue/index.vue.vm"; + private final String ELE_INDEX_VUE_VM = "element/index.vue.vm"; + + private final String ELE_ADD_UPDATE_VUE_VM = "element/form.vue.vm"; + private final String AVUE_API_JS_VM = "avue/api.js.vm"; private final String AVUE_CRUD_JS_VM = "avue/crud.js.vm"; /** * 配置 + * + * @param config * @return */ - private List getTemplates() { + private List getTemplates(GenConfig config) { List templates = new ArrayList<>(); templates.add("template/Entity.java.vm"); templates.add("template/Mapper.java.vm"); @@ -99,17 +100,26 @@ public class CodeGenUtils { templates.add("template/Controller.java.vm"); templates.add("template/menu.sql.vm"); templates.add("template/avue/api.js.vm"); - templates.add("template/avue/index.vue.vm"); - templates.add("template/avue/crud.js.vm"); + + if (StyleTypeEnum.AVUE.getStyle().equals(config.getStyle())) { + templates.add("template/avue/index.vue.vm"); + templates.add("template/avue/crud.js.vm"); + } else { + templates.add("template/element/index.vue.vm"); + templates.add("template/element/form.vue.vm"); + } + return templates; } /** * 生成代码 + * + * @return */ @SneakyThrows public Map generatorCode(GenConfig genConfig, Map table, - List> columns, ZipOutputStream zip, GenFormConf formConf) { + List> columns, ZipOutputStream zip, GenFormConf formConf) { // 配置信息 Configuration config = getConfig(); boolean hasBigDecimal = false; @@ -119,16 +129,14 @@ public class CodeGenUtils { if (StrUtil.isNotBlank(genConfig.getComments())) { tableEntity.setComments(genConfig.getComments()); - } - else { + } else { tableEntity.setComments(table.get("tableComment")); } String tablePrefix; if (StrUtil.isNotBlank(genConfig.getTablePrefix())) { tablePrefix = genConfig.getTablePrefix(); - } - else { + } else { tablePrefix = config.getString("tablePrefix"); } @@ -136,25 +144,39 @@ public class CodeGenUtils { String className = tableToJava(tableEntity.getTableName(), tablePrefix); tableEntity.setCaseClassName(className); tableEntity.setLowerClassName(StringUtils.uncapitalize(className)); - + // 获取需要在swagger文档中隐藏的属性字段 + List hiddenColumns = config.getList("hiddenColumn"); // 列信息 List columnList = new ArrayList<>(); for (Map column : columns) { ColumnEntity columnEntity = new ColumnEntity(); columnEntity.setColumnName(column.get("columnName")); columnEntity.setDataType(column.get("dataType")); - columnEntity.setComments(column.get("columnComment")); columnEntity.setExtra(column.get("extra")); columnEntity.setNullable("NO".equals(column.get("isNullable"))); columnEntity.setColumnType(column.get("columnType")); - columnEntity.setHidden(Boolean.FALSE); + // 隐藏不需要的在接口文档中展示的字段 + if (hiddenColumns.contains(column.get("columnName"))) { + columnEntity.setHidden(Boolean.TRUE); + } else { + columnEntity.setHidden(Boolean.FALSE); + } // 列名转换成Java属性名 String attrName = columnToJava(columnEntity.getColumnName()); columnEntity.setCaseAttrName(attrName); columnEntity.setLowerAttrName(StringUtils.uncapitalize(attrName)); + // 判断注释是否为空 + if (StrUtil.isNotBlank(column.get("comments"))) { + // 注意去除换行符号 + columnEntity.setComments(StrUtil.removeAllLineBreaks(column.get("comments"))); + } else { + columnEntity.setComments(columnEntity.getLowerAttrName()); + } + // 列的数据类型,转换成Java类型 - String attrType = config.getString(columnEntity.getDataType(), "unknowType"); + String dataType = StrUtil.subBefore(columnEntity.getDataType(), "(", false); + String attrType = config.getString(dataType, "unknowType"); columnEntity.setAttrType(attrType); if (!hasBigDecimal && "BigDecimal".equals(attrType)) { hasBigDecimal = true; @@ -173,10 +195,6 @@ public class CodeGenUtils { tableEntity.setPk(tableEntity.getColumns().get(0)); } - // 设置velocity资源加载器 - Properties prop = new Properties(); - prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); - Velocity.init(prop); // 封装模板数据 Map map = new HashMap<>(16); map.put("tableName", tableEntity.getTableName()); @@ -190,30 +208,26 @@ public class CodeGenUtils { if (StrUtil.isNotBlank(genConfig.getComments())) { map.put("comments", genConfig.getComments()); - } - else { + } else { map.put("comments", tableEntity.getComments()); } if (StrUtil.isNotBlank(genConfig.getAuthor())) { map.put("author", genConfig.getAuthor()); - } - else { + } else { map.put("author", config.getString("author")); } if (StrUtil.isNotBlank(genConfig.getModuleName())) { map.put("moduleName", genConfig.getModuleName()); - } - else { + } else { map.put("moduleName", config.getString("moduleName")); } if (StrUtil.isNotBlank(genConfig.getPackageName())) { map.put("package", genConfig.getPackageName()); map.put("mainPath", genConfig.getPackageName()); - } - else { + } else { map.put("package", config.getString("package")); map.put("mainPath", config.getString("mainPath")); } @@ -224,16 +238,17 @@ public class CodeGenUtils { /** * 渲染数据 - * @param genConfig 配置信息 - * @param zip 流 (为空,直接返回Map) - * @param formConf 表单信息 + * + * @param genConfig 配置信息 + * @param zip 流 (为空,直接返回Map) + * @param formConf 表单信息 * @param tableEntity 表基本信息 - * @param map 模板参数 + * @param map 模板参数 * @return map key-filename value-contents * @throws IOException */ private Map renderData(GenConfig genConfig, ZipOutputStream zip, GenFormConf formConf, - TableEntity tableEntity, Map map) throws IOException { + TableEntity tableEntity, Map map) throws IOException { // 设置velocity资源加载器 Properties prop = new Properties(); prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); @@ -241,7 +256,7 @@ public class CodeGenUtils { VelocityContext context = new VelocityContext(map); // 获取模板列表 - List templates = getTemplates(); + List templates = getTemplates(genConfig); Map resultMap = new HashMap<>(8); for (String template : templates) { @@ -287,7 +302,7 @@ public class CodeGenUtils { * 列名转换成Java属性名 */ public String columnToJava(String columnName) { - return WordUtils.capitalizeFully(columnName, new char[] { '_' }).replace("_", ""); + return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", ""); } /** @@ -303,11 +318,10 @@ public class CodeGenUtils { /** * 获取配置信息 */ - private Configuration getConfig() { + public Configuration getConfig() { try { return new PropertiesConfiguration("generator.properties"); - } - catch (ConfigurationException e) { + } catch (ConfigurationException e) { throw new CheckedException("获取配置文件失败,", e); } } @@ -352,7 +366,7 @@ public class CodeGenUtils { return className.toLowerCase() + "_menu.sql"; } - if (template.contains(AVUE_INDEX_VUE_VM)) { + if (template.contains(AVUE_INDEX_VUE_VM) || template.contains(ELE_INDEX_VUE_VM)) { return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "views" + File.separator + moduleName + File.separator + className.toLowerCase() + File.separator + "index.vue"; @@ -368,6 +382,12 @@ public class CodeGenUtils { + File.separator + "crud" + File.separator + className.toLowerCase() + ".js"; } + if (template.contains(ELE_ADD_UPDATE_VUE_VM)) { + return CommonConstants.FRONT_END_PROJECT + File.separator + "src" + File.separator + "views" + + File.separator + moduleName + File.separator + className.toLowerCase() + File.separator + + className.toLowerCase() + "-form.vue"; + } + return null; } diff --git a/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/StyleTypeEnum.java b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/StyleTypeEnum.java new file mode 100644 index 00000000..535ee116 --- /dev/null +++ b/pig-visual/pig-codegen/src/main/java/com/pig4cloud/pig/codegen/support/StyleTypeEnum.java @@ -0,0 +1,36 @@ +package com.pig4cloud.pig.codegen.support; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * @author lengleng + * @date 2021/7/31 + *

+ * 代码生成风格 + */ +@Getter +@AllArgsConstructor +public enum StyleTypeEnum { + + /** + * 前端类型-avue 风格 + */ + AVUE("0", "avue 风格"), + + /** + * 前端类型-element 风格 + */ + ELEMENT("1", "element 风格"); + + /** + * 类型 + */ + private String style; + + /** + * 描述 + */ + private String description; + +} diff --git a/pig-visual/pig-codegen/src/main/resources/template/element/form.vue.vm b/pig-visual/pig-codegen/src/main/resources/template/element/form.vue.vm new file mode 100644 index 00000000..3e716ccb --- /dev/null +++ b/pig-visual/pig-codegen/src/main/resources/template/element/form.vue.vm @@ -0,0 +1,93 @@ + + + diff --git a/pig-visual/pig-codegen/src/main/resources/template/element/index.vue.vm b/pig-visual/pig-codegen/src/main/resources/template/element/index.vue.vm new file mode 100644 index 00000000..b757d6de --- /dev/null +++ b/pig-visual/pig-codegen/src/main/resources/template/element/index.vue.vm @@ -0,0 +1,126 @@ + + +