From ccbdad7eba48b492f2c6c3284542f46042af0cdc Mon Sep 17 00:00:00 2001 From: hewei Date: Fri, 9 Jun 2017 14:45:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E9=BB=98=E8=AE=A4=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generator/plugins/utils/BasePlugin.java | 8 +- .../enhanced/DefaultCommentGenerator.java | 333 ------------------ .../enhanced/TemplateCommentGenerator.java | 26 +- .../resources/mybatis-generator-comment.ftl | 278 +++++++++++++++ 4 files changed, 299 insertions(+), 346 deletions(-) delete mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/DefaultCommentGenerator.java create mode 100644 src/main/resources/mybatis-generator-comment.ftl diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java index f0edd5b..71511fd 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/BasePlugin.java @@ -17,12 +17,12 @@ package com.itfsw.mybatis.generator.plugins.utils; import com.itfsw.mybatis.generator.plugins.CommentPlugin; -import com.itfsw.mybatis.generator.plugins.utils.enhanced.DefaultCommentGenerator; import com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator; import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.config.Context; import org.mybatis.generator.config.PluginConfiguration; +import org.mybatis.generator.internal.DefaultCommentGenerator; import org.mybatis.generator.internal.util.StringUtility; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,15 +56,15 @@ public class BasePlugin extends PluginAdapter { PluginConfiguration cfg = PluginTools.getPluginConfiguration(CommentPlugin.class, context); if (cfg == null || cfg.getProperty(CommentPlugin.PRE_TEMPLATE) == null){ - if (context.getCommentGeneratorConfiguration().getConfigurationType().equals("DEFAULT")){ + if (context.getCommentGenerator() instanceof DefaultCommentGenerator){ // 使用默认模板引擎 - commentGenerator = new DefaultCommentGenerator(); + commentGenerator = new TemplateCommentGenerator("mybatis-generator-comment.ftl", true); } else { // 用户自定义 commentGenerator = context.getCommentGenerator(); } } else { - TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE)); + TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE), false); // ITFSW 插件使用的注释生成器 commentGenerator = templateCommentGenerator; diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/DefaultCommentGenerator.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/DefaultCommentGenerator.java deleted file mode 100644 index 9c826be..0000000 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/DefaultCommentGenerator.java +++ /dev/null @@ -1,333 +0,0 @@ -/* - * Copyright (c) 2017. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.itfsw.mybatis.generator.plugins.utils.enhanced; - -import org.mybatis.generator.api.CommentGenerator; -import org.mybatis.generator.api.IntrospectedColumn; -import org.mybatis.generator.api.IntrospectedTable; -import org.mybatis.generator.api.dom.java.*; -import org.mybatis.generator.api.dom.xml.TextElement; -import org.mybatis.generator.api.dom.xml.XmlElement; -import org.mybatis.generator.config.MergeConstants; -import org.mybatis.generator.internal.util.StringUtility; - -import java.util.Properties; - -/** - * --------------------------------------------------------------------------- - * 注释生成工具 - * --------------------------------------------------------------------------- - * @author: hewei - * @time:2017/4/28 15:11 - * --------------------------------------------------------------------------- - */ -public class DefaultCommentGenerator implements CommentGenerator { - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addJavaFileComment(org.mybatis.generator.api.dom.java.CompilationUnit) - */ - public void addJavaFileComment(CompilationUnit compilationUnit) { - // add no file level comments by default - } - - /** - * Adds a suitable comment to warn users that the element was generated, and when it was generated. - * @param xmlElement the xml element - */ - public void addComment(XmlElement xmlElement) { - - xmlElement.addElement(new TextElement("")); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addRootComment(org.mybatis.generator.api.dom.xml.XmlElement) - */ - public void addRootComment(XmlElement rootElement) { - // add no document level comments by default - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addConfigurationProperties(java.util.Properties) - */ - public void addConfigurationProperties(Properties properties) { - } - - /** - * This method adds the custom javadoc tag for. You may do nothing if you do not wish to include the Javadoc tag - - * however, if you do not include the Javadoc tag then the Java merge capability of the eclipse plugin will break. - * @param javaElement the java element - * @param markAsDoNotDelete the mark as do not delete - */ - protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) { - javaElement.addJavaDocLine(" *"); - StringBuilder sb = new StringBuilder(); - sb.append(" * "); - sb.append(MergeConstants.NEW_ELEMENT_TAG); - if (markAsDoNotDelete) { - sb.append(" do_not_delete_during_merge"); - } - - javaElement.addJavaDocLine(sb.toString()); - javaElement.addJavaDocLine(" * @project https://github.com/itfsw/mybatis-generator-plugin"); - } - - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addClassComment(org.mybatis.generator.api.dom.java.InnerClass, org.mybatis.generator.api.IntrospectedTable) - */ - public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { - if (innerClass instanceof InnerInterfaceWrapperToInnerClass){ - InnerInterfaceWrapperToInnerClass innerInterfaceWrapperToInnerClass = (InnerInterfaceWrapperToInnerClass) innerClass; - InnerInterface innerInterface = innerInterfaceWrapperToInnerClass.getInnerInterface(); - - StringBuilder sb = new StringBuilder(); - - innerInterface.addJavaDocLine("/**"); - innerInterface.addJavaDocLine(" * This interface was generated by MyBatis Generator."); - - sb.append(" * This interface corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - innerInterface.addJavaDocLine(sb.toString()); - - addJavadocTag(innerInterface, false); - - innerInterface.addJavaDocLine(" */"); - } else { - StringBuilder sb = new StringBuilder(); - - innerClass.addJavaDocLine("/**"); - innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); - - sb.append(" * This class corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - innerClass.addJavaDocLine(sb.toString()); - - addJavadocTag(innerClass, false); - - innerClass.addJavaDocLine(" */"); - } - - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addTopLevelClassComment(org.mybatis.generator.api.dom.java.TopLevelClass, org.mybatis.generator.api.IntrospectedTable) - */ - @Override - public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { - - StringBuilder sb = new StringBuilder(); - - topLevelClass.addJavaDocLine("/**"); - - String remarks = introspectedTable.getRemarks(); - if (StringUtility.stringHasValue(remarks)) { - topLevelClass.addJavaDocLine(" * Database Table Remarks:"); - String[] remarkLines = remarks.split(System.getProperty("line.separator")); - for (String remarkLine : remarkLines) { - topLevelClass.addJavaDocLine(" * " + remarkLine); - } - } - topLevelClass.addJavaDocLine(" *"); - - topLevelClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); - - sb.append(" * This class corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - topLevelClass.addJavaDocLine(sb.toString()); - - addJavadocTag(topLevelClass, true); - - topLevelClass.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addEnumComment(org.mybatis.generator.api.dom.java.InnerEnum, org.mybatis.generator.api.IntrospectedTable) - */ - public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { - - StringBuilder sb = new StringBuilder(); - - innerEnum.addJavaDocLine("/**"); - innerEnum.addJavaDocLine(" * This enum was generated by MyBatis Generator."); - - sb.append(" * This enum corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - innerEnum.addJavaDocLine(sb.toString()); - - addJavadocTag(innerEnum, false); - - innerEnum.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addFieldComment(org.mybatis.generator.api.dom.java.Field, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn) - */ - public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { - - field.addJavaDocLine("/**"); - - String remarks = introspectedColumn.getRemarks(); - if (StringUtility.stringHasValue(remarks)) { - field.addJavaDocLine(" * Database Column Remarks:"); - String[] remarkLines = remarks.split(System.getProperty("line.separator")); - for (String remarkLine : remarkLines) { - field.addJavaDocLine(" * " + remarkLine); - } - } - - field.addJavaDocLine(" *"); - field.addJavaDocLine(" * This field was generated by MyBatis Generator."); - - StringBuilder sb = new StringBuilder(); - sb.append(" * This field corresponds to the database column "); - sb.append(introspectedTable.getFullyQualifiedTable()); - sb.append('.'); - sb.append(introspectedColumn.getActualColumnName()); - field.addJavaDocLine(sb.toString()); - - addJavadocTag(field, false); - - field.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addFieldComment(org.mybatis.generator.api.dom.java.Field, org.mybatis.generator.api.IntrospectedTable) - */ - public void addFieldComment(Field field, IntrospectedTable introspectedTable) { - - StringBuilder sb = new StringBuilder(); - - field.addJavaDocLine("/**"); - field.addJavaDocLine(" * This field was generated by MyBatis Generator."); - - sb.append(" * This field corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - field.addJavaDocLine(sb.toString()); - - addJavadocTag(field, false); - - field.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addGeneralMethodComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable) - */ - public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { - - StringBuilder sb = new StringBuilder(); - - method.addJavaDocLine("/**"); - method.addJavaDocLine(" * This method was generated by MyBatis Generator."); - - sb.append(" * This method corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - method.addJavaDocLine(sb.toString()); - - addJavadocTag(method, false); - - method.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addGetterComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn) - */ - public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { - - StringBuilder sb = new StringBuilder(); - - method.addJavaDocLine("/**"); - method.addJavaDocLine(" * This method was generated by MyBatis Generator."); - - sb.append(" * This method returns the value of the database column "); - sb.append(introspectedTable.getFullyQualifiedTable()); - sb.append('.'); - sb.append(introspectedColumn.getActualColumnName()); - method.addJavaDocLine(sb.toString()); - - method.addJavaDocLine(" *"); - - sb.setLength(0); - sb.append(" * @return the value of "); - sb.append(introspectedTable.getFullyQualifiedTable()); - sb.append('.'); - sb.append(introspectedColumn.getActualColumnName()); - method.addJavaDocLine(sb.toString()); - - addJavadocTag(method, false); - - method.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addSetterComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn) - */ - public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { - - StringBuilder sb = new StringBuilder(); - - method.addJavaDocLine("/**"); - method.addJavaDocLine(" * This method was generated by MyBatis Generator."); - - sb.append(" * This method sets the value of the database column "); - sb.append(introspectedTable.getFullyQualifiedTable()); - sb.append('.'); - sb.append(introspectedColumn.getActualColumnName()); - method.addJavaDocLine(sb.toString()); - - method.addJavaDocLine(" *"); - - Parameter parm = method.getParameters().get(0); - sb.setLength(0); - sb.append(" * @param "); - sb.append(parm.getName()); - sb.append(" the value for "); - sb.append(introspectedTable.getFullyQualifiedTable()); - sb.append('.'); - sb.append(introspectedColumn.getActualColumnName()); - method.addJavaDocLine(sb.toString()); - - addJavadocTag(method, false); - - method.addJavaDocLine(" */"); - } - - /* (non-Javadoc) - * @see org.mybatis.generator.api.CommentGenerator#addClassComment(org.mybatis.generator.api.dom.java.InnerClass, org.mybatis.generator.api.IntrospectedTable, boolean) - */ - public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { - - StringBuilder sb = new StringBuilder(); - - innerClass.addJavaDocLine("/**"); - innerClass.addJavaDocLine(" * This class was generated by MyBatis Generator."); - - sb.append(" * This class corresponds to the database table "); - sb.append(introspectedTable.getFullyQualifiedTable()); - innerClass.addJavaDocLine(sb.toString()); - - addJavadocTag(innerClass, markAsDoNotDelete); - - innerClass.addJavaDocLine(" */"); - } -} diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java index 514dc0f..2332c46 100644 --- a/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/utils/enhanced/TemplateCommentGenerator.java @@ -33,6 +33,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.InputStream; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; @@ -54,16 +55,25 @@ public class TemplateCommentGenerator implements CommentGenerator { /** * 构造函数 * @param templatePath 模板路径 + * @param useForDefault 未使用Comment插件,用作默认注释生成器 */ - public TemplateCommentGenerator(String templatePath) { - + public TemplateCommentGenerator(String templatePath, boolean useForDefault) { try { - File file = new File(templatePath); - if (file.exists()) { - // Xml 解析 - Document doc = new SAXReader().read(file); + Document doc = null; + if (useForDefault){ + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath); + doc = new SAXReader().read(inputStream); + } else { + File file = new File(templatePath); + if (file.exists()) { + doc = new SAXReader().read(file); + } else { + logger.error("没有找到对应注释模板:" + templatePath); + } + } - // 遍历comment 节点 + // 遍历comment 节点 + if (doc != null){ for (EnumNode node : EnumNode.values()){ Element element = doc.getRootElement().elementByID(node.value()); if (element != null){ @@ -73,8 +83,6 @@ public class TemplateCommentGenerator implements CommentGenerator { templates.put(node, template); } } - } else { - logger.error("没有找到对应注释模板:" + templatePath); } } catch (Exception e) { logger.error("注释模板XML解析失败!", e); diff --git a/src/main/resources/mybatis-generator-comment.ftl b/src/main/resources/mybatis-generator-comment.ftl new file mode 100644 index 0000000..1232727 --- /dev/null +++ b/src/main/resources/mybatis-generator-comment.ftl @@ -0,0 +1,278 @@ + + \ No newline at end of file