package com.deppon.cmc.specmarket.util;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.XmlElement;import org.mybatis.generator.config.MergeConstants;import org.mybatis.generator.config.PropertyRegistry;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Properties;import java.util.Set;importstatic org.mybatis.generator.internal.util.StringUtility.isTrue;/**
* mybatis generator 自定义comment生成器.
*/publicclassMyCommentGeneratorimplementsCommentGenerator{private Properties properties;private Properties systemPro;privateboolean suppressDate;privateboolean suppressAllComments;private String currentDateStr;publicMyCommentGenerator(){super();
properties =newProperties();
systemPro = System.getProperties();
suppressDate =false;
suppressAllComments =false;
currentDateStr =(newSimpleDateFormat("yyyy-MM-dd")).format(newDate());}@OverridepublicvoidaddJavaFileComment(CompilationUnit compilationUnit){// add no file level comments by defaultreturn;}/**
* Adds a suitable comment to warn users that the element was generated, and
* when it was generated.
*/@OverridepublicvoidaddComment(XmlElement xmlElement){return;}@OverridepublicvoidaddRootComment(XmlElement rootElement){// add no document level comments by defaultreturn;}@OverridepublicvoidaddGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set){}@OverridepublicvoidaddGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set){}@OverridepublicvoidaddFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set){}@OverridepublicvoidaddFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> set){}@OverridepublicvoidaddClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> set){}@OverridepublicvoidaddConfigurationProperties(Properties properties){this.properties.putAll(properties);
suppressDate =isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));
suppressAllComments =isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));}/**
* 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
*/protectedvoidaddJavadocTag(JavaElement javaElement,boolean markAsDoNotDelete){
javaElement.addJavaDocLine(" *");
StringBuilder sb =newStringBuilder();
sb.append(" * ");
sb.append(MergeConstants.NEW_ELEMENT_TAG);if(markAsDoNotDelete){
sb.append(" do_not_delete_during_merge");}
String s =getDateString();if(s != null){
sb.append(' ');
sb.append(s);}
javaElement.addJavaDocLine(sb.toString());}/**
* This method returns a formated date string to include in the Javadoc tag
* and XML comments. You may return null if you do not want the date in
* these documentation elements.
*
* @return a string representing the current timestamp, or null
*/protected String getDateString(){
String result = null;if(!suppressDate){
result = currentDateStr;}return result;}@OverridepublicvoidaddClassComment(InnerClass innerClass, IntrospectedTable introspectedTable){if(suppressAllComments){return;}// StringBuilder sb = new StringBuilder();// innerClass.addJavaDocLine("/**");// sb.append(" * ");// sb.append(introspectedTable.getFullyQualifiedTable());// sb.append(" ");// sb.append(getDateString());// innerClass.addJavaDocLine(sb.toString());// innerClass.addJavaDocLine(" */");}@OverridepublicvoidaddEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable){if(suppressAllComments){return;}
StringBuilder sb =newStringBuilder();
innerEnum.addJavaDocLine("/**");// addJavadocTag(innerEnum, false);
sb.append(" * ");
sb.append(introspectedTable.getFullyQualifiedTable());
innerEnum.addJavaDocLine(sb.toString());
innerEnum.addJavaDocLine(" */");}/**
* 字段注释
* @param field
* @param introspectedTable
* @param introspectedColumn
*/@OverridepublicvoidaddFieldComment(Field field, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn){if(suppressAllComments){return;}
StringBuilder sb =newStringBuilder();
field.addJavaDocLine("/**");
sb.append(" * ");
sb.append(introspectedColumn.getRemarks());
field.addJavaDocLine(sb.toString());// addJavadocTag(field, false);
field.addJavaDocLine(" */");}@OverridepublicvoidaddFieldComment(Field field, IntrospectedTable introspectedTable){if(suppressAllComments){return;}}@OverridepublicvoidaddModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable){}@OverridepublicvoidaddGeneralMethodComment(Method method, IntrospectedTable introspectedTable){if(suppressAllComments){return;}}@OverridepublicvoidaddGetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn){if(suppressAllComments){return;}}@OverridepublicvoidaddSetterComment(Method method, IntrospectedTable introspectedTable,
IntrospectedColumn introspectedColumn){if(suppressAllComments){return;}}@OverridepublicvoidaddClassComment(InnerClass innerClass, IntrospectedTable introspectedTable,boolean markAsDoNotDelete){if(suppressAllComments){return;}}}