File tree Expand file tree Collapse file tree 6 files changed +14
-96
lines changed
main/java/com/itfsw/mybatis/generator/plugins
java/com/itfsw/mybatis/generator/plugins Expand file tree Collapse file tree 6 files changed +14
-96
lines changed Original file line number Diff line number Diff line change 2929 * ---------------------------------------------------------------------------
3030 */
3131public 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
Original file line number Diff line number Diff line change 2525import org .mybatis .generator .api .PluginAdapter ;
2626import org .mybatis .generator .config .Context ;
2727import org .mybatis .generator .config .PluginConfiguration ;
28- import org .mybatis .generator .internal .DefaultCommentGenerator ;
2928import org .mybatis .generator .internal .util .StringUtility ;
3029import org .slf4j .Logger ;
3130import 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 ;
Original file line number Diff line number Diff line change 3838
3939import javax .xml .bind .DatatypeConverter ;
4040import java .io .File ;
41- import java .io .InputStream ;
4241import java .io .StringWriter ;
4342import 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 节点
Original file line number Diff line number Diff 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 */
Original file line number Diff line number Diff line change 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" />
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments