|
@@ -1,15 +1,5 @@
|
|
package com.yonyou.occ.codegenerator;
|
|
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.Configuration;
|
|
import freemarker.template.Template;
|
|
import freemarker.template.Template;
|
|
import freemarker.template.TemplateException;
|
|
import freemarker.template.TemplateException;
|
|
@@ -27,6 +17,12 @@ import org.jdom2.xpath.XPathFactory;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
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 newBasePackage;
|
|
private final String pdmFile;
|
|
private final String pdmFile;
|
|
private final String tableCode;
|
|
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/";
|
|
private final String templateDir = getClass().getResource("/").getPath() + "generator/";
|
|
|
|
|
|
public CodeGenerator(String oldBasePackage, String newBasePackage, String pdmFile, String tableCode,
|
|
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.oldBasePackage = oldBasePackage;
|
|
this.newBasePackage = newBasePackage;
|
|
this.newBasePackage = newBasePackage;
|
|
this.pdmFile = pdmFile;
|
|
this.pdmFile = pdmFile;
|
|
@@ -84,6 +78,7 @@ public class CodeGenerator {
|
|
if (ifUseCustDocDefRef) {
|
|
if (ifUseCustDocDefRef) {
|
|
generateMapperDecorator(table);
|
|
generateMapperDecorator(table);
|
|
}
|
|
}
|
|
|
|
+ generateMetaDataSql(table);
|
|
} catch (JDOMException e) {
|
|
} catch (JDOMException e) {
|
|
log.error("解析XML文件时发生错误", e);
|
|
log.error("解析XML文件时发生错误", e);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
@@ -109,7 +104,7 @@ public class CodeGenerator {
|
|
// namespaces
|
|
// namespaces
|
|
Namespace nsAttribute = jdomDocument.getRootElement().getNamespace("a");
|
|
Namespace nsAttribute = jdomDocument.getRootElement().getNamespace("a");
|
|
Namespace nsCollection = jdomDocument.getRootElement().getNamespace("c");
|
|
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(),
|
|
XPathExpression<Element> expr = xFactory.compile("//a:Code[text()='" + tableCode + "']", Filters.element(),
|
|
null, nsAttribute);
|
|
null, nsAttribute);
|
|
@@ -171,16 +166,28 @@ public class CodeGenerator {
|
|
|
|
|
|
// 输出文件
|
|
// 输出文件
|
|
String fileName = table.getClassName();
|
|
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);
|
|
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);
|
|
Writer outFile = new FileWriter(outFilePath);
|
|
template.process(data, outFile);
|
|
template.process(data, outFile);
|
|
|
|
|
|
@@ -218,4 +225,8 @@ public class CodeGenerator {
|
|
private void generateMapperDecorator(Table table) throws IOException, TemplateException {
|
|
private void generateMapperDecorator(Table table) throws IOException, TemplateException {
|
|
generate(table, "mapperDecorator", "/service/mapper/");
|
|
generate(table, "mapperDecorator", "/service/mapper/");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private void generateMetaDataSql(Table table) throws IOException, TemplateException {
|
|
|
|
+ generate(table, "metaData", "/sql/");
|
|
|
|
+ }
|
|
}
|
|
}
|