Commit ccbdad7e authored by hewei's avatar hewei

替换默认注释工具

parent 46793b36
...@@ -17,12 +17,12 @@ ...@@ -17,12 +17,12 @@
package com.itfsw.mybatis.generator.plugins.utils; package com.itfsw.mybatis.generator.plugins.utils;
import com.itfsw.mybatis.generator.plugins.CommentPlugin; 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 com.itfsw.mybatis.generator.plugins.utils.enhanced.TemplateCommentGenerator;
import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.config.Context; import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.PluginConfiguration; import org.mybatis.generator.config.PluginConfiguration;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility; import org.mybatis.generator.internal.util.StringUtility;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -56,15 +56,15 @@ public class BasePlugin extends PluginAdapter { ...@@ -56,15 +56,15 @@ public class BasePlugin extends PluginAdapter {
PluginConfiguration cfg = PluginTools.getPluginConfiguration(CommentPlugin.class, context); PluginConfiguration cfg = PluginTools.getPluginConfiguration(CommentPlugin.class, context);
if (cfg == null || cfg.getProperty(CommentPlugin.PRE_TEMPLATE) == null){ 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 { } else {
// 用户自定义 // 用户自定义
commentGenerator = context.getCommentGenerator(); commentGenerator = context.getCommentGenerator();
} }
} else { } else {
TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE)); TemplateCommentGenerator templateCommentGenerator = new TemplateCommentGenerator(cfg.getProperty(CommentPlugin.PRE_TEMPLATE), false);
// ITFSW 插件使用的注释生成器 // ITFSW 插件使用的注释生成器
commentGenerator = templateCommentGenerator; commentGenerator = templateCommentGenerator;
......
...@@ -33,6 +33,7 @@ import org.slf4j.Logger; ...@@ -33,6 +33,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -54,16 +55,25 @@ public class TemplateCommentGenerator implements CommentGenerator { ...@@ -54,16 +55,25 @@ public class TemplateCommentGenerator implements CommentGenerator {
/** /**
* 构造函数 * 构造函数
* @param templatePath 模板路径 * @param templatePath 模板路径
* @param useForDefault 未使用Comment插件,用作默认注释生成器
*/ */
public TemplateCommentGenerator(String templatePath) { public TemplateCommentGenerator(String templatePath, boolean useForDefault) {
try { try {
Document doc = null;
if (useForDefault){
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(templatePath);
doc = new SAXReader().read(inputStream);
} else {
File file = new File(templatePath); File file = new File(templatePath);
if (file.exists()) { if (file.exists()) {
// Xml 解析 doc = new SAXReader().read(file);
Document doc = new SAXReader().read(file); } else {
logger.error("没有找到对应注释模板:" + templatePath);
}
}
// 遍历comment 节点 // 遍历comment 节点
if (doc != null){
for (EnumNode node : EnumNode.values()){ for (EnumNode node : EnumNode.values()){
Element element = doc.getRootElement().elementByID(node.value()); Element element = doc.getRootElement().elementByID(node.value());
if (element != null){ if (element != null){
...@@ -73,8 +83,6 @@ public class TemplateCommentGenerator implements CommentGenerator { ...@@ -73,8 +83,6 @@ public class TemplateCommentGenerator implements CommentGenerator {
templates.put(node, template); templates.put(node, template);
} }
} }
} else {
logger.error("没有找到对应注释模板:" + templatePath);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("注释模板XML解析失败!", e); logger.error("注释模板XML解析失败!", e);
......
<?xml version="1.0" encoding="UTF-8"?>
<template>
<!-- #############################################################################################################
/**
* This method is called to add a file level comment to a generated java file. This method could be used to add a
* general file comment (such as a copyright notice). However, note that the Java file merge function in Eclipse
* does not deal with this comment. If you run the generator repeatedly, you will only retain the comment from the
* initial run.
* <p>
*
* The default implementation does nothing.
*
* @param compilationUnit
* the compilation unit
*/
-->
<comment ID="addJavaFileComment"></comment>
<!-- #############################################################################################################
/**
* This method should add a suitable comment as a child element of the specified xmlElement to warn users that the
* element was generated and is subject to regeneration.
*
* @param xmlElement
* the xml element
*/
-->
<comment ID="addComment"><![CDATA[
<!--
WARNING - ${mgb}
This element is automatically generated by MyBatis Generator, do not modify.
@project https://github.com/itfsw/mybatis-generator-plugin
-->
]]></comment>
<!-- #############################################################################################################
/**
* This method is called to add a comment as the first child of the root element. This method could be used to add a
* general file comment (such as a copyright notice). However, note that the XML file merge function does not deal
* with this comment. If you run the generator repeatedly, you will only retain the comment from the initial run.
* <p>
*
* The default implementation does nothing.
*
* @param rootElement
* the root element
*/
-->
<comment ID="addRootComment"></comment>
<!-- #############################################################################################################
/**
* This method should add a Javadoc comment to the specified field. The field is related to the specified table and
* is used to hold the value of the specified column.
* <p>
*
* <b>Important:</b> This method should add a the nonstandard JavaDoc tag "@mbg.generated" to the comment. Without
* this tag, the Eclipse based Java merge feature will fail.
*
* @param field
* the field
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addFieldComment"><![CDATA[
<#if introspectedColumn??>
/**
<#if introspectedColumn.remarks?? && introspectedColumn.remarks != ''>
* Database Column Remarks:
<#list introspectedColumn.remarks?split("\n") as remark>
* ${remark}
</#list>
</#if>
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
<#else>
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
</#if>
]]></comment>
<!-- #############################################################################################################
/**
* Adds a comment for a model class. The Java code merger should
* be notified not to delete the entire class in case any manual
* changes have been made. So this method will always use the
* "do not delete" annotation.
*
* Because of difficulties with the Java file merger, the default implementation
* of this method should NOT add comments. Comments should only be added if
* specifically requested by the user (for example, by enabling table remark comments).
*
* @param topLevelClass
* the top level class
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addModelClassComment"><![CDATA[
/**
<#if introspectedTable.remarks?? && introspectedTable.remarks != ''>
* Database Table Remarks:
<#list introspectedTable.remarks?split("\n") as remark>
* ${remark}
</#list>
</#if>
*
* This class was generated by MyBatis Generator.
* This class corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the inner class comment.
*
* @param innerClass
* the inner class
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addClassComment"><![CDATA[
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the inner class comment.
*
* @param innerClass
* the inner class
* @param introspectedTable
* the introspected table
* @param markAsDoNotDelete
* the mark as do not delete
*/
-->
<comment ID="addClassComment"><![CDATA[
/**
* This class was generated by MyBatis Generator.
* This class corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}<#if markAsDoNotDelete?? && markAsDoNotDelete> do_not_delete_during_merge</#if>
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the enum comment.
*
* @param innerEnum
* the inner enum
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addEnumComment"><![CDATA[
/**
* This enum was generated by MyBatis Generator.
* This enum corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the interface comment.
*
* @param innerInterface
* the inner interface
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addInterfaceComment"><![CDATA[
/**
* This interface was generated by MyBatis Generator.
* This interface corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the getter comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addGetterComment"><![CDATA[
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* @return the value of ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the setter comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
* @param introspectedColumn
* the introspected column
*/
-->
<comment ID="addSetterComment"><![CDATA[
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* @param ${method.parameters[0].name} the value for ${introspectedTable.fullyQualifiedTable}.${introspectedColumn.actualColumnName}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
<!-- #############################################################################################################
/**
* Adds the general method comment.
*
* @param method
* the method
* @param introspectedTable
* the introspected table
*/
-->
<comment ID="addGeneralMethodComment"><![CDATA[
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table ${introspectedTable.fullyQualifiedTable}
*
* ${mgb}
* @project https://github.com/itfsw/mybatis-generator-plugin
*/
]]></comment>
</template>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment