Skip to content

Commit 6da91a2

Browse files
author
hewei
committed
修正插件注释的默认实现行为
1 parent 02e3e67 commit 6da91a2

File tree

6 files changed

+14
-96
lines changed

6 files changed

+14
-96
lines changed

src/main/java/com/itfsw/mybatis/generator/plugins/CommentPlugin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
* ---------------------------------------------------------------------------
3030
*/
3131
public class CommentPlugin extends BasePlugin {
32-
public static final String PRO_TEMPLATE = "template"; // 模板 property
32+
/**
33+
* 模板 property
34+
*/
35+
public static final String PRO_TEMPLATE = "template";
3336

3437
/**
3538
* 插件具体实现查看BasePlugin

src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.mybatis.generator.api.PluginAdapter;
2626
import org.mybatis.generator.config.Context;
2727
import org.mybatis.generator.config.PluginConfiguration;
28-
import org.mybatis.generator.internal.DefaultCommentGenerator;
2928
import org.mybatis.generator.internal.util.StringUtility;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
@@ -66,15 +65,9 @@ public void setContext(Context context) {
6665
PluginConfiguration cfg = PluginTools.getPluginConfiguration(context, CommentPlugin.class);
6766

6867
if (cfg == null || cfg.getProperty(CommentPlugin.PRO_TEMPLATE) == null) {
69-
if (context.getCommentGenerator() instanceof DefaultCommentGenerator) {
70-
// 使用默认模板引擎
71-
commentGenerator = new TemplateCommentGenerator(context, "default-comment.ftl", true);
72-
} else {
73-
// 用户自定义
74-
commentGenerator = context.getCommentGenerator();
75-
}
68+
commentGenerator = context.getCommentGenerator();
7669
} else {
77-
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(context, cfg.getProperty(CommentPlugin.PRO_TEMPLATE), false);
70+
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(context, cfg.getProperty(CommentPlugin.PRO_TEMPLATE));
7871

7972
// ITFSW 插件使用的注释生成器
8073
commentGenerator = templateCommentGenerator;

src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
import javax.xml.bind.DatatypeConverter;
4040
import java.io.File;
41-
import java.io.InputStream;
4241
import java.io.StringWriter;
4342
import java.util.*;
4443

@@ -65,22 +64,15 @@ public class TemplateCommentGenerator implements CommentGenerator {
6564
* 构造函数
6665
* @param context
6766
* @param templatePath 模板路径
68-
* @param useForDefault 未使用Comment插件,用作默认注释生成器
6967
*/
70-
public TemplateCommentGenerator(Context context, String templatePath, boolean useForDefault) {
68+
public TemplateCommentGenerator(Context context, String templatePath) {
7169
try {
7270
Document doc = null;
73-
if (useForDefault) {
74-
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath);
75-
doc = new SAXReader().read(inputStream);
76-
inputStream.close();
71+
File file = new File(templatePath);
72+
if (file.exists()) {
73+
doc = new SAXReader().read(file);
7774
} else {
78-
File file = new File(templatePath);
79-
if (file.exists()) {
80-
doc = new SAXReader().read(file);
81-
} else {
82-
logger.error("没有找到对应注释模板:" + templatePath);
83-
}
75+
logger.error("没有找到对应注释模板:" + templatePath);
8476
}
8577

8678
// 遍历comment 节点

src/test/java/com/itfsw/mybatis/generator/plugins/CommentPluginTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,6 @@ public static void init() throws Exception {
5454
DBHelper.createDB("scripts/CommentPlugin/init.sql");
5555
}
5656

57-
/**
58-
* 测试没有配置模板的情况
59-
*/
60-
@Test
61-
public void testGenerateWithoutTemplate() throws Exception {
62-
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/CommentPlugin/mybatis-generator-without-template.xml");
63-
MyBatisGenerator myBatisGenerator = tool.generate();
64-
// 是否在使用系统默认模板
65-
int count = 0;
66-
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
67-
if (file.getFormattedContent().indexOf("@project https://github.com/itfsw/mybatis-generator-plugin") != -1) {
68-
count++;
69-
}
70-
}
71-
Assert.assertTrue(count > 0);
72-
}
73-
7457
/**
7558
* 测试配置了模板参数转换
7659
*/

src/test/resources/scripts/CommentPlugin/mybatis-generator-without-comment.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
<context id="default" targetRuntime="MyBatis3">
2626
<!-- 插件 -->
2727
<!-- 注释插件 -->
28-
<plugin type="com.itfsw.mybatis.generator.plugins.CommentPlugin"/>
28+
<plugin type="com.itfsw.mybatis.generator.plugins.CommentPlugin">
29+
<property name="template" value="src/test/resources/scripts/CommentPlugin/test-comment.ftl"/>
30+
</plugin>
2931

3032
<commentGenerator>
3133
<property name="suppressAllComments" value="true"/>

src/test/resources/scripts/CommentPlugin/mybatis-generator-without-template.xml

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)