Skip to content

Commit ef0457a

Browse files
author
wanghaiming
committed
whm 支持基于freemarker生成的代码的合并
1 parent 60b73b8 commit ef0457a

14 files changed

Lines changed: 155 additions & 101 deletions

File tree

java-generator-core/src/main/java/org/mybatis/generator/api/MyBatisGenerator.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ public void generate(ProgressCallback callback, Set<String> contextIds,
273273
}
274274
/**
275275
* 如果JavaServiceGeneratorConfiguration存在则调用相关方法
276+
* 如果JavaDomainGeneratorConfiguration存在则调用相关方法
276277
*/
277278

278279
for (Context c:configuration.getContexts()) {
@@ -320,18 +321,17 @@ private void writeGeneratedJavaFile(GeneratedJavaFile gjf, ProgressCallback call
320321
targetFile = new File(directory, gjf.getFileName());
321322
if (targetFile.exists()) {
322323
if (shellCallback.isMergeSupported()) {
324+
// source = shellCallback.mergeJavaFile(gjf
325+
// .getFormattedContent(), targetFile
326+
// .getAbsolutePath(),
327+
// MergeConstants.OLD_ELEMENT_TAGS,
328+
// gjf.getFileEncoding());
323329
source = shellCallback.mergeJavaFile(gjf
324330
.getFormattedContent(), targetFile
325-
.getAbsolutePath(),
326-
MergeConstants.OLD_ELEMENT_TAGS,
327-
gjf.getFileEncoding());
331+
.getAbsolutePath());
328332

329333
} else if (shellCallback.isOverwriteEnabled()) {
330-
source = mergeJavaFile(gjf
331-
.getFormattedContent(), targetFile
332-
.getAbsolutePath(),
333-
gjf.getFileEncoding());
334-
// source = gjf.getFormattedContent();
334+
source = gjf.getFormattedContent();
335335
warnings.add(getString("Warning.11", //$NON-NLS-1$
336336
targetFile.getAbsolutePath()));
337337
} else {

java-generator-core/src/main/java/org/mybatis/generator/api/ShellCallback.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.mybatis.generator.api;
1717

1818
import java.io.File;
19+
import java.io.FileNotFoundException;
1920

2021
import org.mybatis.generator.exception.ShellException;
2122

@@ -106,6 +107,9 @@ File getDirectory(String targetProject, String targetPackage)
106107
String mergeJavaFile(String newFileSource, String existingFileFullPath,
107108
String[] javadocTags, String fileEncoding) throws ShellException;
108109

110+
String mergeJavaFile(String newFileSource, String existingFileFullPath
111+
) throws ShellException,FileNotFoundException;
112+
109113
/**
110114
* After all files are saved to the file system, this method is called
111115
* once for each unique project that was affected by the generation
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.mybatis.generator.codegen.freemarker;
2+
3+
import freemarker.template.Template;
4+
import freemarker.template.TemplateException;
5+
import org.mybatis.generator.codegen.freemarker.TemplateEntity.TemplateEntity;
6+
import org.mybatis.generator.internal.JavaFileMergerJaxp;
7+
8+
import java.io.*;
9+
10+
import static org.springframework.util.FileCopyUtils.copy;
11+
12+
/**
13+
* Created by whm on 2017/8/8.
14+
*/
15+
public class FreemarkerUtil {
16+
public static void generateFreemarkerFile(File file ,Template t,TemplateEntity s){
17+
try {
18+
Reader in;
19+
boolean isFirst = false;
20+
String oldFileSource = "";
21+
if (file != null && file.canRead()){
22+
in = new FileReader(file);
23+
StringWriter out = new StringWriter();
24+
copy(in,out);
25+
oldFileSource = out.toString();
26+
isFirst = true;
27+
}
28+
FileOutputStream fos = new FileOutputStream(file); //java文件的生成目录
29+
t.process(s, new OutputStreamWriter(fos, "utf-8")); //
30+
fos.flush();
31+
fos.close();
32+
33+
//读取新生成的文件内容
34+
String newFileSource = "";
35+
if (file != null && file.canRead()){
36+
in = new FileReader(file);
37+
StringWriter out = new StringWriter();
38+
copy(in,out);
39+
newFileSource = out.toString();
40+
in.close();
41+
}
42+
if (isFirst) {
43+
String newJavaFileContent = new JavaFileMergerJaxp().getNewFreemarkerJavaFile(newFileSource, oldFileSource);
44+
Writer writer = new FileWriter(file);
45+
writer.write(newJavaFileContent);
46+
writer.close();
47+
}
48+
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
} catch (TemplateException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
}

java-generator-core/src/main/java/org/mybatis/generator/codegen/freemarker/JavaDomainGenerator.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,37 @@
66
import org.mybatis.generator.codegen.freemarker.TemplateEntity.DomainTemplateEntity;
77
import org.mybatis.generator.codegen.freemarker.TemplateEntity.ServiceTemplateEntity;
88

9-
import java.io.File;
10-
import java.io.FileOutputStream;
11-
import java.io.IOException;
12-
import java.io.OutputStreamWriter;
9+
import java.io.*;
1310
import java.util.List;
1411

12+
import static org.mybatis.generator.codegen.freemarker.FreemarkerUtil.generateFreemarkerFile;
13+
1514
/**
1615
* Created by whm on 2017/7/26.
1716
*/
1817
public class JavaDomainGenerator {
1918
public static void addJavaDomainGenerator(List<DomainTemplateEntity> domainTemplateEntities) {
20-
Configuration cfg = new Configuration();
2119
try {
2220
for (DomainTemplateEntity d:domainTemplateEntities) {
21+
Configuration cfg = new Configuration();
2322
DomainTemplateEntity.DomainTemplate domainTemplate = d.getDomainTemplate();
2423
DomainTemplateEntity.NativeDomainTemplate nativeDomainTemplate = d.getNativeDomainTemplate();
2524
cfg.setClassForTemplateLoading(JavaDomainGenerator.class, "/template"); //指定模板所在的classpath目录
2625
Template t = cfg.getTemplate("DomainTemplate"); //指定模板
2726
File f = new File(System.getProperty("user.dir") +"/"+ domainTemplate.getProjectTargetPackage());
2827
f.mkdirs();
29-
FileOutputStream fos = new FileOutputStream(new File(System.getProperty("user.dir") +"/"+ domainTemplate.getProjectTargetPackage() + domainTemplate.getDomainInterface()+".java")); //java文件的生成目录
30-
t.process(domainTemplate, new OutputStreamWriter(fos, "utf-8")); //
28+
File domainFile = new File(System.getProperty("user.dir") +"/"+ domainTemplate.getProjectTargetPackage() + domainTemplate.getDomainInterface()+".java");
29+
generateFreemarkerFile(domainFile,t,domainTemplate);
3130
Configuration cfg1 = new Configuration();
3231
cfg1.setClassForTemplateLoading(JavaDomainGenerator.class, "/template"); //指定模板所在的classpath目录
3332
Template t1 = cfg1.getTemplate("NativeDomainTemplate"); //指定模板
34-
FileOutputStream fos1 = new FileOutputStream(new File(System.getProperty("user.dir") +"/"+ nativeDomainTemplate.getProjectTargetPackage() + nativeDomainTemplate.getNativeDomainClazz()+".java")); //java文件的生成目录
35-
t1.process(nativeDomainTemplate, new OutputStreamWriter(fos1, "utf-8")); //
36-
fos.flush();
37-
fos.close();
38-
fos1.flush();
39-
fos1.close();
33+
File nativeDomainFile = new File(System.getProperty("user.dir") +"/"+ nativeDomainTemplate.getProjectTargetPackage() + nativeDomainTemplate.getNativeDomainClazz()+".java");
34+
35+
generateFreemarkerFile(nativeDomainFile,t1,nativeDomainTemplate);
36+
4037
}
4138
} catch (IOException e) {
4239
e.printStackTrace();
43-
} catch (TemplateException e) {
44-
e.printStackTrace();
4540
}
4641

4742
}

java-generator-core/src/main/java/org/mybatis/generator/codegen/freemarker/JavaServiceGenerator.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,36 @@
44
import freemarker.template.Template;
55
import freemarker.template.TemplateException;
66
import org.mybatis.generator.codegen.freemarker.TemplateEntity.ServiceTemplateEntity;
7+
import org.mybatis.generator.internal.JavaFileMergerJaxp;
78

8-
import java.io.File;
9-
import java.io.FileOutputStream;
10-
import java.io.IOException;
11-
import java.io.OutputStreamWriter;
9+
import java.io.*;
1210

1311
import java.util.List;
1412

13+
import static org.mybatis.generator.codegen.freemarker.FreemarkerUtil.generateFreemarkerFile;
14+
import static org.springframework.util.FileCopyUtils.copy;
15+
1516

1617
/**
1718
* 通过解析serviceTemplate生成对应的service
1819
* Created by whm on 2017/7/26.
1920
*/
2021
public class JavaServiceGenerator {
2122

22-
public static void addJavaServiceGenerator(List<ServiceTemplateEntity> serviceTemplateEntitylist) {
23-
Configuration cfg = new Configuration();
24-
try {
25-
for (ServiceTemplateEntity s:serviceTemplateEntitylist) {
23+
public static void addJavaServiceGenerator(List<ServiceTemplateEntity> serviceTemplateEntityList) {
24+
Configuration cfg = new Configuration();
25+
try {
26+
for (ServiceTemplateEntity s : serviceTemplateEntityList) {
2627
cfg.setClassForTemplateLoading(JavaServiceGenerator.class, "/template"); //指定模板所在的classpath目录
2728
Template t = cfg.getTemplate("ServiceTemplate"); //指定模板
28-
File f = new File(System.getProperty("user.dir") +"/"+ s.getProjectTargetPackage());
29+
File f = new File(System.getProperty("user.dir") + "/" + s.getProjectTargetPackage());
2930
f.mkdirs();
30-
FileOutputStream fos = new FileOutputStream(new File(System.getProperty("user.dir") +"/"+ s.getProjectTargetPackage() + s.getClassName()+".java")); //java文件的生成目录
31-
t.process(s, new OutputStreamWriter(fos, "utf-8")); //
32-
fos.flush();
33-
fos.close();
31+
String filePath = System.getProperty("user.dir") + "/" + s.getProjectTargetPackage() + s.getClassName() + ".java";
32+
File file = new File(filePath);
33+
generateFreemarkerFile(file ,t,s);
3434
}
3535
} catch (IOException e) {
36-
e.printStackTrace();
37-
} catch (TemplateException e) {
38-
e.printStackTrace();
36+
e.printStackTrace();
3937
}
40-
4138
}
4239
}

java-generator-core/src/main/java/org/mybatis/generator/codegen/freemarker/TemplateEntity/DomainTemplateEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void setNativeDomainTemplate(NativeDomainTemplate nativeDomainTemplate) {
2626
this.nativeDomainTemplate = nativeDomainTemplate;
2727
}
2828

29-
public static class DomainTemplate{
29+
public static class DomainTemplate implements TemplateEntity{
3030
private String domainPackage;//domain所在的包
3131
private String boPackage;//对应的bo对象所在的包
3232
private String domainInterface;//domain接口名
@@ -84,7 +84,7 @@ public String getGeneratedTime() {
8484
}
8585
}
8686

87-
public static class NativeDomainTemplate{
87+
public static class NativeDomainTemplate implements TemplateEntity{
8888
private String nativeDomainPackage;//nativeDomain所在的包
8989
private String domainPackage;//domain所在的包
9090
private String boPackage;//对应的bo对象所在的包

java-generator-core/src/main/java/org/mybatis/generator/codegen/freemarker/TemplateEntity/ServiceTemplateEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Created by whm on 2017/7/26.
88
*/
9-
public class ServiceTemplateEntity {
9+
public class ServiceTemplateEntity implements TemplateEntity{
1010
private String templatePackage;//包名
1111
private String modelPackage;//所需要导入的model包
1212
private String mapperPackage;//所需要导入的mapper包
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.mybatis.generator.codegen.freemarker.TemplateEntity;
2+
3+
/**
4+
* Created by whm on 2017/8/8.
5+
*/
6+
public interface TemplateEntity {
7+
}

java-generator-core/src/main/java/org/mybatis/generator/config/Context.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -839,20 +839,4 @@ public ConnectionFactoryConfiguration getConnectionFactoryConfiguration() {
839839
public void setConnectionFactoryConfiguration(ConnectionFactoryConfiguration connectionFactoryConfiguration) {
840840
this.connectionFactoryConfiguration = connectionFactoryConfiguration;
841841
}
842-
843-
// /**
844-
// * 判断表内是否含有BLOB字段的方法
845-
// * @param tableConfiguration
846-
// * @return
847-
// */
848-
// public boolean hasBLOBColumns(TableConfiguration tableConfiguration){
849-
// boolean columnsHasBLOB = false;
850-
// System.out.println(introspectedTables);
851-
// for (IntrospectedTable i:introspectedTables){
852-
// if (i.getTableConfiguration().getTableName().equals(tableConfiguration.getTableName())){
853-
// columnsHasBLOB = i.hasBLOBColumns() ? true :false;
854-
// }
855-
// }
856-
// return columnsHasBLOB;
857-
// }
858842
}

java-generator-core/src/main/java/org/mybatis/generator/internal/DefaultShellCallback.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.mybatis.generator.internal.util.messages.Messages.getString;
1919

2020
import java.io.File;
21+
import java.io.FileNotFoundException;
2122
import java.util.StringTokenizer;
2223

2324
import org.mybatis.generator.api.ShellCallback;
@@ -92,7 +93,7 @@ public void refreshProject(String project) {
9293
* @see org.mybatis.generator.api.ShellCallback#isMergeSupported()
9394
*/
9495
public boolean isMergeSupported() {
95-
return false;
96+
return true;
9697
}
9798

9899
/* (non-Javadoc)
@@ -110,4 +111,10 @@ public String mergeJavaFile(String newFileSource,
110111
throws ShellException {
111112
throw new UnsupportedOperationException();
112113
}
114+
115+
public String mergeJavaFile(String newFileSource,
116+
String existingFileFullPath)
117+
throws ShellException, FileNotFoundException {
118+
return new JavaFileMergerJaxp().getNewJavaFile(newFileSource,existingFileFullPath);
119+
}
113120
}

0 commit comments

Comments
 (0)