ソースを参照

新增元数据脚本模板生成,优化代码结构

caixin 3 年 前
コミット
a7ba3e64d5

+ 37 - 26
src/main/java/com/yonyou/occ/codegenerator/CodeGenerator.java

@@ -1,15 +1,5 @@
 package com.yonyou.occ.codegenerator;
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.Writer;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 import freemarker.template.Configuration;
 import freemarker.template.Template;
 import freemarker.template.TemplateException;
@@ -27,6 +17,12 @@ import org.jdom2.xpath.XPathFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.*;
+
 /**
  * 代码生成器
  *
@@ -42,17 +38,15 @@ public class CodeGenerator {
     private final String newBasePackage;
     private final String pdmFile;
     private final String tableCode;
-    private String author;
-    private String referenceType;
-    private boolean ifUseCustDocDefRef;
-    private String targetProjectRootPath;
-
-    private String moduleName;
-
+    private final String author;
+    private final String referenceType;
+    private final boolean ifUseCustDocDefRef;
+    private final String targetProjectRootPath;
+    private final String moduleName;
     private final String templateDir = getClass().getResource("/").getPath() + "generator/";
 
     public CodeGenerator(String oldBasePackage, String newBasePackage, String pdmFile, String tableCode,
-            String referenceType, boolean ifUseCustDocDefRef, String author, String targetProjectRootPath) {
+                         String referenceType, boolean ifUseCustDocDefRef, String author, String targetProjectRootPath) {
         this.oldBasePackage = oldBasePackage;
         this.newBasePackage = newBasePackage;
         this.pdmFile = pdmFile;
@@ -84,6 +78,7 @@ public class CodeGenerator {
             if (ifUseCustDocDefRef) {
                 generateMapperDecorator(table);
             }
+            generateMetaDataSql(table);
         } catch (JDOMException e) {
             log.error("解析XML文件时发生错误", e);
         } catch (IOException e) {
@@ -109,7 +104,7 @@ public class CodeGenerator {
         // namespaces
         Namespace nsAttribute = jdomDocument.getRootElement().getNamespace("a");
         Namespace nsCollection = jdomDocument.getRootElement().getNamespace("c");
-        Namespace nsObject = jdomDocument.getRootElement().getNamespace("o");
+        //Namespace nsObject = jdomDocument.getRootElement().getNamespace("o");
 
         XPathExpression<Element> expr = xFactory.compile("//a:Code[text()='" + tableCode + "']", Filters.element(),
                 null, nsAttribute);
@@ -171,16 +166,28 @@ public class CodeGenerator {
 
         // 输出文件
         String fileName = table.getClassName();
-        if (!templateName.equalsIgnoreCase("entity")) {
-            fileName += templateName.substring(0, 1).toUpperCase() + templateName.substring(1);
+        // 文件后缀名
+        String suffix = ".java";
+        // 文件保存目录
+        final String outFolderPath;
+        if (templateName.equalsIgnoreCase("metaData")) {
+            fileName = table.getName().toLowerCase() + "_元数据";
+            suffix = ".sql";
+            outFolderPath = targetProjectRootPath + targetSubPath;
+        } else {
+            if (!templateName.equalsIgnoreCase("entity")) {
+                fileName += templateName.substring(0, 1).toUpperCase() + templateName.substring(1);
+            }
+            outFolderPath = targetProjectRootPath + "src/main/java/" + newBasePackage.replace(".", "/") + "/" +
+                    moduleName + targetSubPath;
         }
-        final String outFolderPath = targetProjectRootPath + "src/main/java/" + newBasePackage.replace(".", "/") + "/" +
-                moduleName + targetSubPath;
+
         File outFolder = new File(outFolderPath);
-        if (!outFolder.exists()) {
-            outFolder.mkdirs();
+        if (!outFolder.exists() && !outFolder.mkdirs()) {
+            System.err.println("文件目录创建失败!");
+            return;
         }
-        final String outFilePath = outFolderPath + fileName + ".java";
+        final String outFilePath = outFolderPath + fileName + suffix;
         Writer outFile = new FileWriter(outFilePath);
         template.process(data, outFile);
 
@@ -218,4 +225,8 @@ public class CodeGenerator {
     private void generateMapperDecorator(Table table) throws IOException, TemplateException {
         generate(table, "mapperDecorator", "/service/mapper/");
     }
+
+    private void generateMetaDataSql(Table table) throws IOException, TemplateException {
+        generate(table, "metaData", "/sql/");
+    }
 }

+ 7 - 12
src/main/java/com/yonyou/occ/codegenerator/CodeGeneratorEntry.java

@@ -22,13 +22,12 @@ public class CodeGeneratorEntry {
     /**
      * PDM文件路径
      */
-    private static final String PDM_FILE = "D:\\work\\用友\\project\\科顺\\sql\\PhysicalDataModel_3.pdm";
-
+    private static final String PDM_FILE = "E:\\Yonyou\\project\\OCC\\PDM\\demo.pdm";
 
     /**
      * 物理表名称(英文逗号分隔)
      */
-    private static final String TABLE_CODES = "CKS_CUST_OBJECTIVE,CKS_CUST_OBJ_ITEM";
+    private static final String TABLE_CODES = "DEMO_PERSON_INFO";
 
     /**
      * 参照类型
@@ -50,22 +49,18 @@ public class CodeGeneratorEntry {
      */
     private static final String TARGET_PROJECT_ROOT_PATH = "E:\\Yonyou\\project\\OTHER\\occ-toolkit\\code\\";
 
-
     /**
-     * dzz 代码生成
-     * @param args
+     * 代码生成主函数
+     *
+     * @param args args
      */
     public static void main(String[] args) {
-        String pdmFile = "E:\\Yonyou\\project\\OCC\\PDM\\demo.pdm";
-        String tableCodes = "DEMO_PERSON_INFO";
-
-
-        for (String tableCode : tableCodes.split(",")) {
+        for (String tableCode : TABLE_CODES.split(",")) {
             if (StringUtils.isEmpty(tableCode)) {
                 continue;
             }
             tableCode = tableCode.trim();
-            CodeGenerator codeGenerator = new CodeGenerator(OLD_BASE_PACKAGE, NEW_BASE_PACKAGE, pdmFile, tableCode,
+            CodeGenerator codeGenerator = new CodeGenerator(OLD_BASE_PACKAGE, NEW_BASE_PACKAGE, PDM_FILE, tableCode,
                     REFERENCE_TYPE, IF_USE_CUST_DOC_DEF_REF, AUTHOR, TARGET_PROJECT_ROOT_PATH);
             codeGenerator.run();
         }

+ 11 - 11
src/main/java/com/yonyou/occ/codegenerator/GeneratorHelper.java

@@ -14,14 +14,14 @@ public class GeneratorHelper {
 	 * @return 类名。
 	 */
 	public static String getClassName(String tableCode) {
-		String className = "";
+		StringBuilder className = new StringBuilder();
 		String[] parts = tableCode.split("_");
 		for (int i = 1; i < parts.length; ++i) {
 			String part = parts[i];
 			part = part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase();
-			className += part;
+			className.append(part);
 		}
-		return className;
+		return className.toString();
 	}
 
 	/**
@@ -31,12 +31,12 @@ public class GeneratorHelper {
 	 * @return 类名。
 	 */
 	public static String getRequestMapping(String tableCode) {
-		String className = "";
+		StringBuilder className = new StringBuilder();
 		String[] parts = tableCode.split("_");
 		for (int i = 1; i < parts.length; ++i) {
 			String part = parts[i];
 			part = part.toLowerCase();
-			className += "-" + part;
+			className.append("-").append(part);
 		}
 		return parts[0].toLowerCase() + "/" + className.substring(1);
 	}
@@ -48,7 +48,7 @@ public class GeneratorHelper {
 	 * @return 字段名。
 	 */
 	public static String getFieldName(String columnCode) {
-		String fieldName = "";
+		StringBuilder fieldName = new StringBuilder();
 		String[] parts = columnCode.split("_");
 		for (int i = 0; i < parts.length; ++i) {
 			String part = parts[i];
@@ -58,9 +58,9 @@ public class GeneratorHelper {
 			else {
 				part = part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase();
 			}
-			fieldName += part;
+			fieldName.append(part);
 		}
-		return fieldName;
+		return fieldName.toString();
 	}
 
 	/**
@@ -70,14 +70,14 @@ public class GeneratorHelper {
 	 * @return 字段名。
 	 */
 	public static String getPropertyName(String columnCode) {
-		String className = "";
+		StringBuilder className = new StringBuilder();
 		String[] parts = columnCode.split("_");
 		for (int i = 0; i < parts.length; ++i) {
 			String part = parts[i];
 			part = part.substring(0, 1).toUpperCase() + part.substring(1).toLowerCase();
-			className += part;
+			className.append(part);
 		}
-		return className;
+		return className.toString();
 	}
 
 	public static String getFieldType(String typeInDb) {

+ 146 - 146
src/main/java/com/yonyou/occ/codegenerator/Table.java

@@ -4,155 +4,155 @@ import java.util.ArrayList;
 import java.util.List;
 
 public class Table {
-	private final String name;
+    private final String name;
 
-	private final String code;
+    private final String code;
 
-	private final String comment;
+    private final String comment;
 
-	private final String className;
-
-	private final String requestMapping;
+    private final String className;
+
+    private final String requestMapping;
 
-	private final String referenceType;
-
-	private List<Column> columns = new ArrayList<Column>();
-
-	public Table(String name, String code, String comment, String referenceType) {
-		this.name = name;
-		this.code = code;
-		this.comment = comment;
+    private final String referenceType;
+
+    private List<Column> columns = new ArrayList<Column>();
+
+    public Table(String name, String code, String comment, String referenceType) {
+        this.name = name;
+        this.code = code;
+        this.comment = comment;
 
-		className = GeneratorHelper.getClassName(code);
-		requestMapping = GeneratorHelper.getRequestMapping(code);
-		this.referenceType = referenceType;
-	}
-
-	public String getName() {
-		return name;
-	}
-
-	public String getCode() {
-		return code;
-	}
-
-	public String getComment() {
-		return comment;
-	}
-
-	public String getClassName() {
-		return className;
-	}
-
-	public String getRequestMapping() {
-		return requestMapping;
-	}
-
-	public String getReferenceType() {
-		return referenceType;
-	}
-
-	public List<Column> getColumns() {
-		return columns;
-	}
-
-	public void setColumns(List<Column> columns) {
-		this.columns = columns;
-	}
-
-	public class Column {
-		/**
-		 * 名称
-		 */
-		private final String name;
-
-		/**
-		 * 编码
-		 */
-		private final String code;
-
-		/**
-		 * 注释
-		 */
-		private final String comment;
-
-		/**
-		 * 数据类型
-		 */
-		private final String dataType;
-
-		/**
-		 * 长度
-		 */
-		private final Integer length;
-
-		/**
-		 * 精度
-		 */
-		private final Integer precision;
-
-		/**
-		 * 字段名
-		 */
-		private final String fieldName;
-
-		/**
-		 * 属性名
-		 */
-		private final String propertyName;
-
-		/**
-		 * 字段类型
-		 */
-		private final String fieldType;
-
-		public Column(String name, String code, String comment, String dataType, Integer length, Integer precision) {
-			this.name = name;
-			this.code = code;
-			this.comment = comment;
-			this.dataType = dataType;
-			this.length = length;
-			this.precision = precision;
-
-			fieldName = GeneratorHelper.getFieldName(code);
-			propertyName = GeneratorHelper.getPropertyName(code);
-			fieldType = GeneratorHelper.getFieldType(dataType);
-		}
-
-		public String getName() {
-			return name;
-		}
-
-		public String getCode() {
-			return code;
-		}
-
-		public String getComment() {
-			return comment;
-		}
-
-		public String getDataType() {
-			return dataType;
-		}
-
-		public Integer getLength() {
-			return length;
-		}
-
-		public Integer getPrecision() {
-			return precision;
-		}
-
-		public String getFieldName() {
-			return fieldName;
-		}
-
-		public String getPropertyName() {
-			return propertyName;
-		}
-
-		public String getFieldType() {
-			return fieldType;
-		}
-	}
+        className = GeneratorHelper.getClassName(code);
+        requestMapping = GeneratorHelper.getRequestMapping(code);
+        this.referenceType = referenceType;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getComment() {
+        return comment;
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public String getRequestMapping() {
+        return requestMapping;
+    }
+
+    public String getReferenceType() {
+        return referenceType;
+    }
+
+    public List<Column> getColumns() {
+        return columns;
+    }
+
+    public void setColumns(List<Column> columns) {
+        this.columns = columns;
+    }
+
+    public class Column {
+        /**
+         * 名称
+         */
+        private final String name;
+
+        /**
+         * 编码
+         */
+        private final String code;
+
+        /**
+         * 注释
+         */
+        private final String comment;
+
+        /**
+         * 数据类型
+         */
+        private final String dataType;
+
+        /**
+         * 长度
+         */
+        private final Integer length;
+
+        /**
+         * 精度
+         */
+        private final Integer precision;
+
+        /**
+         * 字段名
+         */
+        private final String fieldName;
+
+        /**
+         * 属性名
+         */
+        private final String propertyName;
+
+        /**
+         * 字段类型
+         */
+        private final String fieldType;
+
+        public Column(String name, String code, String comment, String dataType, Integer length, Integer precision) {
+            this.name = name;
+            this.code = code;
+            this.comment = comment;
+            this.dataType = dataType;
+            this.length = length;
+            this.precision = precision;
+
+            fieldName = GeneratorHelper.getFieldName(code);
+            propertyName = GeneratorHelper.getPropertyName(code);
+            fieldType = GeneratorHelper.getFieldType(dataType);
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public String getCode() {
+            return code;
+        }
+
+        public String getComment() {
+            return comment;
+        }
+
+        public String getDataType() {
+            return dataType;
+        }
+
+        public Integer getLength() {
+            return length;
+        }
+
+        public Integer getPrecision() {
+            return precision;
+        }
+
+        public String getFieldName() {
+            return fieldName;
+        }
+
+        public String getPropertyName() {
+            return propertyName;
+        }
+
+        public String getFieldType() {
+            return fieldType;
+        }
+    }
 }

ファイルの差分が大きいため隠しています
+ 5 - 0
src/main/resources/generator/metaData.ftl