|
@@ -20,7 +20,9 @@ package com.pig4cloud.pigx.codegen.util;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.IoUtil;
|
|
|
import cn.hutool.core.util.CharsetUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.pig4cloud.pigx.codegen.entity.ColumnEntity;
|
|
|
+import com.pig4cloud.pigx.codegen.entity.GenConfig;
|
|
|
import com.pig4cloud.pigx.codegen.entity.TableEntity;
|
|
|
import com.pig4cloud.pigx.common.core.exception.CheckedException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -49,47 +51,60 @@ import java.util.zip.ZipOutputStream;
|
|
|
@Slf4j
|
|
|
public class GenUtils {
|
|
|
|
|
|
- public static final String ENTITY_JAVA_VM = "Entity.java.vm";
|
|
|
- public static final String MAPPER_JAVA_VM = "Mapper.java.vm";
|
|
|
- public static final String SERVICE_JAVA_VM = "Service.java.vm";
|
|
|
- public static final String SERVICE_IMPL_JAVA_VM = "ServiceImpl.java.vm";
|
|
|
- public static final String CONTROLLER_JAVA_VM = "Controller.java.vm";
|
|
|
- public static final String MAPPER_XML_VM = "Mapper.xml.vm";
|
|
|
- public static final String MENU_SQL_VM = "menu.sql.vm";
|
|
|
- public static final String INDEX_VUE_VM = "index.vue.vm";
|
|
|
- public static final String API_JS_VM = "api.js.vm";
|
|
|
- public static final String CRUD_JS_VM = "crud.js.vm";
|
|
|
-
|
|
|
- public static List<String> getTemplates() {
|
|
|
+ private static final String ENTITY_JAVA_VM = "Entity.java.vm";
|
|
|
+ private static final String MAPPER_JAVA_VM = "Mapper.java.vm";
|
|
|
+ private static final String SERVICE_JAVA_VM = "Service.java.vm";
|
|
|
+ private static final String SERVICE_IMPL_JAVA_VM = "ServiceImpl.java.vm";
|
|
|
+ private static final String CONTROLLER_JAVA_VM = "Controller.java.vm";
|
|
|
+ private static final String MAPPER_XML_VM = "Mapper.xml.vm";
|
|
|
+ private static final String MENU_SQL_VM = "menu.sql.vm";
|
|
|
+ private static final String INDEX_VUE_VM = "index.vue.vm";
|
|
|
+ private static final String API_JS_VM = "api.js.vm";
|
|
|
+ private static final String CRUD_JS_VM = "crud.js.vm";
|
|
|
+
|
|
|
+ private static List<String> getTemplates() {
|
|
|
List<String> templates = new ArrayList<>();
|
|
|
- templates.add("template/Entity.java.vm" );
|
|
|
- templates.add("template/Mapper.java.vm" );
|
|
|
- templates.add("template/Mapper.xml.vm" );
|
|
|
- templates.add("template/Service.java.vm" );
|
|
|
- templates.add("template/ServiceImpl.java.vm" );
|
|
|
- templates.add("template/Controller.java.vm" );
|
|
|
- templates.add("template/menu.sql.vm" );
|
|
|
-
|
|
|
- templates.add("template/index.vue.vm" );
|
|
|
- templates.add("template/api.js.vm" );
|
|
|
- templates.add("template/crud.js.vm" );
|
|
|
+ templates.add("template/Entity.java.vm");
|
|
|
+ templates.add("template/Mapper.java.vm");
|
|
|
+ templates.add("template/Mapper.xml.vm");
|
|
|
+ templates.add("template/Service.java.vm");
|
|
|
+ templates.add("template/ServiceImpl.java.vm");
|
|
|
+ templates.add("template/Controller.java.vm");
|
|
|
+ templates.add("template/menu.sql.vm");
|
|
|
+
|
|
|
+ templates.add("template/index.vue.vm");
|
|
|
+ templates.add("template/api.js.vm");
|
|
|
+ templates.add("template/crud.js.vm");
|
|
|
return templates;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成代码
|
|
|
*/
|
|
|
- public static void generatorCode(Map<String, String> table,
|
|
|
+ public static void generatorCode(GenConfig genConfig, Map<String, String> table,
|
|
|
List<Map<String, String>> columns, ZipOutputStream zip) {
|
|
|
//配置信息
|
|
|
Configuration config = getConfig();
|
|
|
boolean hasBigDecimal = false;
|
|
|
//表信息
|
|
|
TableEntity tableEntity = new TableEntity();
|
|
|
- tableEntity.setTableName(table.get("tableName" ));
|
|
|
- tableEntity.setComments(table.get("tableComment" ));
|
|
|
+ tableEntity.setTableName(table.get("tableName"));
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(genConfig.getComments())) {
|
|
|
+ tableEntity.setComments(genConfig.getComments());
|
|
|
+ } else {
|
|
|
+ tableEntity.setComments(table.get("tableComment"));
|
|
|
+ }
|
|
|
+
|
|
|
+ String tablePrefix;
|
|
|
+ if (StrUtil.isNotBlank(genConfig.getTablePrefix())) {
|
|
|
+ tablePrefix = genConfig.getTablePrefix();
|
|
|
+ } else {
|
|
|
+ tablePrefix = config.getString("tablePrefix");
|
|
|
+ }
|
|
|
+
|
|
|
//表名转换成Java类名
|
|
|
- String className = tableToJava(tableEntity.getTableName(), config.getString("tablePrefix" ));
|
|
|
+ String className = tableToJava(tableEntity.getTableName(), tablePrefix);
|
|
|
tableEntity.setCaseClassName(className);
|
|
|
tableEntity.setLowerClassName(StringUtils.uncapitalize(className));
|
|
|
|
|
@@ -97,10 +112,10 @@ public class GenUtils {
|
|
|
List<ColumnEntity> columnList = new ArrayList<>();
|
|
|
for (Map<String, String> 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.setColumnName(column.get("columnName"));
|
|
|
+ columnEntity.setDataType(column.get("dataType"));
|
|
|
+ columnEntity.setComments(column.get("columnComment"));
|
|
|
+ columnEntity.setExtra(column.get("extra"));
|
|
|
|
|
|
//列名转换成Java属性名
|
|
|
String attrName = columnToJava(columnEntity.getColumnName());
|
|
@@ -108,13 +123,13 @@ public class GenUtils {
|
|
|
columnEntity.setLowerAttrName(StringUtils.uncapitalize(attrName));
|
|
|
|
|
|
//列的数据类型,转换成Java类型
|
|
|
- String attrType = config.getString(columnEntity.getDataType(), "unknowType" );
|
|
|
+ String attrType = config.getString(columnEntity.getDataType(), "unknowType");
|
|
|
columnEntity.setAttrType(attrType);
|
|
|
- if (!hasBigDecimal && attrType.equals("BigDecimal" )) {
|
|
|
+ if (!hasBigDecimal && "BigDecimal".equals(attrType)) {
|
|
|
hasBigDecimal = true;
|
|
|
}
|
|
|
//是否主键
|
|
|
- if ("PRI".equalsIgnoreCase(column.get("columnKey" )) && tableEntity.getPk() == null) {
|
|
|
+ if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
|
|
|
tableEntity.setPk(columnEntity);
|
|
|
}
|
|
|
|
|
@@ -129,25 +144,44 @@ public class GenUtils {
|
|
|
|
|
|
//设置velocity资源加载器
|
|
|
Properties prop = new Properties();
|
|
|
- prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
|
|
|
+ prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
|
|
Velocity.init(prop);
|
|
|
- String mainPath = config.getString("mainPath" );
|
|
|
- mainPath = StringUtils.isBlank(mainPath) ? "com.pig4cloud.pigx" : mainPath;
|
|
|
//封装模板数据
|
|
|
Map<String, Object> map = new HashMap<>(16);
|
|
|
map.put("tableName", tableEntity.getTableName());
|
|
|
- map.put("comments", tableEntity.getComments());
|
|
|
map.put("pk", tableEntity.getPk());
|
|
|
map.put("className", tableEntity.getCaseClassName());
|
|
|
map.put("classname", tableEntity.getLowerClassName());
|
|
|
map.put("pathName", tableEntity.getLowerClassName().toLowerCase());
|
|
|
map.put("columns", tableEntity.getColumns());
|
|
|
map.put("hasBigDecimal", hasBigDecimal);
|
|
|
- map.put("mainPath", mainPath);
|
|
|
- map.put("package", config.getString("package" ));
|
|
|
- map.put("moduleName", config.getString("moduleName" ));
|
|
|
- map.put("author", config.getString("author" ));
|
|
|
map.put("datetime", DateUtil.now());
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(genConfig.getComments())) {
|
|
|
+ map.put("comments", genConfig.getComments());
|
|
|
+ } else {
|
|
|
+ map.put("comments", tableEntity.getComments());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(genConfig.getAuthor())) {
|
|
|
+ map.put("author", genConfig.getAuthor());
|
|
|
+ } else {
|
|
|
+ map.put("author", config.getString("author"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(genConfig.getModuleName())) {
|
|
|
+ map.put("moduleName", genConfig.getModuleName());
|
|
|
+ } else {
|
|
|
+ map.put("moduleName", config.getString("moduleName"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StrUtil.isNotBlank(genConfig.getPackageName())) {
|
|
|
+ map.put("package", genConfig.getPackageName());
|
|
|
+ map.put("mainPath", genConfig.getPackageName());
|
|
|
+ } else {
|
|
|
+ map.put("package", config.getString("package"));
|
|
|
+ map.put("mainPath", config.getString("mainPath"));
|
|
|
+ }
|
|
|
VelocityContext context = new VelocityContext(map);
|
|
|
|
|
|
//获取模板列表
|
|
@@ -162,7 +196,7 @@ public class GenUtils {
|
|
|
//添加到zip
|
|
|
zip.putNextEntry(new ZipEntry(Objects
|
|
|
.requireNonNull(getFileName(template, tableEntity.getCaseClassName()
|
|
|
- , config.getString("package" ), config.getString("moduleName" )))));
|
|
|
+ , map.get("package").toString(), map.get("moduleName").toString()))));
|
|
|
IoUtil.write(zip, CharsetUtil.UTF_8, false, sw.toString());
|
|
|
IoUtil.close(sw);
|
|
|
zip.closeEntry();
|
|
@@ -177,7 +211,7 @@ public class GenUtils {
|
|
|
* 列名转换成Java属性名
|
|
|
*/
|
|
|
private static String columnToJava(String columnName) {
|
|
|
- return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "" );
|
|
|
+ return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -185,7 +219,7 @@ public class GenUtils {
|
|
|
*/
|
|
|
private static String tableToJava(String tableName, String tablePrefix) {
|
|
|
if (StringUtils.isNotBlank(tablePrefix)) {
|
|
|
- tableName = tableName.replace(tablePrefix, "" );
|
|
|
+ tableName = tableName.replace(tablePrefix, "");
|
|
|
}
|
|
|
return columnToJava(tableName);
|
|
|
}
|
|
@@ -193,9 +227,9 @@ public class GenUtils {
|
|
|
/**
|
|
|
* 获取配置信息
|
|
|
*/
|
|
|
- public static Configuration getConfig() {
|
|
|
+ private static Configuration getConfig() {
|
|
|
try {
|
|
|
- return new PropertiesConfiguration("generator.properties" );
|
|
|
+ return new PropertiesConfiguration("generator.properties");
|
|
|
} catch (ConfigurationException e) {
|
|
|
throw new CheckedException("获取配置文件失败,", e);
|
|
|
}
|
|
@@ -204,8 +238,8 @@ public class GenUtils {
|
|
|
/**
|
|
|
* 获取文件名
|
|
|
*/
|
|
|
- public static String getFileName(String template, String className, String packageName, String moduleName) {
|
|
|
- String packagePath = "pigx" + File.separator + "main" + File.separator + "java" + File.separator;
|
|
|
+ private static String getFileName(String template, String className, String packageName, String moduleName) {
|
|
|
+ String packagePath = "pigx" + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator;
|
|
|
if (StringUtils.isNotBlank(packageName)) {
|
|
|
packagePath += packageName.replace(".", File.separator) + File.separator + moduleName + File.separator;
|
|
|
}
|
|
@@ -231,7 +265,7 @@ public class GenUtils {
|
|
|
}
|
|
|
|
|
|
if (template.contains(MAPPER_XML_VM)) {
|
|
|
- return "pigx" + File.separator + "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + moduleName + File.separator + className + "Mapper.xml";
|
|
|
+ return "pigx" + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "mapper" + File.separator + moduleName + File.separator + className + "Mapper.xml";
|
|
|
}
|
|
|
|
|
|
if (template.contains(MENU_SQL_VM)) {
|
|
@@ -249,7 +283,7 @@ public class GenUtils {
|
|
|
|
|
|
if (template.contains(CRUD_JS_VM)) {
|
|
|
return "pigx-ui" + File.separator + "src" + File.separator + "const" +
|
|
|
- File.separator + "curd" + File.separator + className.toLowerCase() + ".js";
|
|
|
+ File.separator + "crud" + File.separator + className.toLowerCase() + ".js";
|
|
|
}
|
|
|
|
|
|
return null;
|