2424import org .mybatis .generator .api .CommentGenerator ;
2525import org .mybatis .generator .api .IntrospectedColumn ;
2626import org .mybatis .generator .api .IntrospectedTable ;
27+ import org .mybatis .generator .api .MyBatisGenerator ;
2728import org .mybatis .generator .api .dom .java .*;
2829import org .mybatis .generator .api .dom .xml .TextElement ;
2930import org .mybatis .generator .api .dom .xml .XmlElement ;
3031import org .mybatis .generator .config .MergeConstants ;
32+ import org .mybatis .generator .config .PropertyRegistry ;
3133import org .mybatis .generator .internal .util .StringUtility ;
3234import org .slf4j .Logger ;
3335import org .slf4j .LoggerFactory ;
3436
37+ import javax .xml .bind .DatatypeConverter ;
3538import java .io .File ;
3639import java .io .InputStream ;
3740import java .io .StringWriter ;
38- import java .util .HashMap ;
39- import java . util . Map ;
40- import java . util .Properties ;
41+ import java .util .* ;
42+
43+ import static org . mybatis . generator . internal . util .StringUtility . isTrue ;
4144
4245/**
4346 * ---------------------------------------------------------------------------
@@ -52,6 +55,10 @@ public class TemplateCommentGenerator implements CommentGenerator {
5255
5356 private Map <EnumNode , Template > templates = new HashMap <>(); // 模板
5457
58+ private boolean suppressDate = false ;
59+
60+ private boolean suppressAllComments = false ;
61+
5562 /**
5663 * 构造函数
5764 * @param templatePath 模板路径
@@ -196,7 +203,11 @@ private void addXmlElementComment(XmlElement xmlElement, Map<String, Object> map
196203 */
197204 @ Override
198205 public void addConfigurationProperties (Properties properties ) {
206+ suppressDate = isTrue (properties
207+ .getProperty (PropertyRegistry .COMMENT_GENERATOR_SUPPRESS_DATE ));
199208
209+ suppressAllComments = isTrue (properties
210+ .getProperty (PropertyRegistry .COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS ));
200211 }
201212
202213 /**
@@ -466,6 +477,80 @@ public void addRootComment(XmlElement rootElement) {
466477 addXmlElementComment (rootElement , map , EnumNode .ADD_ROOT_COMMENT );
467478 }
468479
480+ @ Override
481+ public void addGeneralMethodAnnotation (Method method , IntrospectedTable introspectedTable ,
482+ Set <FullyQualifiedJavaType > imports ) {
483+ imports .add (new FullyQualifiedJavaType ("javax.annotation.Generated" )); //$NON-NLS-1$
484+ String comment = "Source Table: " + introspectedTable .getFullyQualifiedTable ().toString (); //$NON-NLS-1$
485+ method .addAnnotation (getGeneratedAnnotation (comment ));
486+ }
487+
488+ @ Override
489+ public void addGeneralMethodAnnotation (Method method , IntrospectedTable introspectedTable ,
490+ IntrospectedColumn introspectedColumn , Set <FullyQualifiedJavaType > imports ) {
491+ imports .add (new FullyQualifiedJavaType ("javax.annotation.Generated" )); //$NON-NLS-1$
492+ String comment = "Source field: " //$NON-NLS-1$
493+ + introspectedTable .getFullyQualifiedTable ().toString ()
494+ + "." //$NON-NLS-1$
495+ + introspectedColumn .getActualColumnName ();
496+ method .addAnnotation (getGeneratedAnnotation (comment ));
497+ }
498+
499+ @ Override
500+ public void addFieldAnnotation (Field field , IntrospectedTable introspectedTable ,
501+ Set <FullyQualifiedJavaType > imports ) {
502+ imports .add (new FullyQualifiedJavaType ("javax.annotation.Generated" )); //$NON-NLS-1$
503+ String comment = "Source Table: " + introspectedTable .getFullyQualifiedTable ().toString (); //$NON-NLS-1$
504+ field .addAnnotation (getGeneratedAnnotation (comment ));
505+ }
506+
507+ @ Override
508+ public void addFieldAnnotation (Field field , IntrospectedTable introspectedTable ,
509+ IntrospectedColumn introspectedColumn , Set <FullyQualifiedJavaType > imports ) {
510+ imports .add (new FullyQualifiedJavaType ("javax.annotation.Generated" )); //$NON-NLS-1$
511+ String comment = "Source field: " //$NON-NLS-1$
512+ + introspectedTable .getFullyQualifiedTable ().toString ()
513+ + "." //$NON-NLS-1$
514+ + introspectedColumn .getActualColumnName ();
515+ field .addAnnotation (getGeneratedAnnotation (comment ));
516+ }
517+
518+ @ Override
519+ public void addClassAnnotation (InnerClass innerClass , IntrospectedTable introspectedTable ,
520+ Set <FullyQualifiedJavaType > imports ) {
521+ imports .add (new FullyQualifiedJavaType ("javax.annotation.Generated" )); //$NON-NLS-1$
522+ String comment = "Source Table: " + introspectedTable .getFullyQualifiedTable ().toString (); //$NON-NLS-1$
523+ innerClass .addAnnotation (getGeneratedAnnotation (comment ));
524+ }
525+
526+ private String getGeneratedAnnotation (String comment ) {
527+ StringBuilder buffer = new StringBuilder ();
528+ buffer .append ("@Generated(" ); //$NON-NLS-1$
529+ if (suppressAllComments ) {
530+ buffer .append ('\"' );
531+ } else {
532+ buffer .append ("value=\" " ); //$NON-NLS-1$
533+ }
534+
535+ buffer .append (MyBatisGenerator .class .getName ());
536+ buffer .append ('\"' );
537+
538+ if (!suppressDate && !suppressAllComments ) {
539+ buffer .append (", date=\" " ); //$NON-NLS-1$
540+ buffer .append (DatatypeConverter .printDateTime (Calendar .getInstance ()));
541+ buffer .append ('\"' );
542+ }
543+
544+ if (!suppressAllComments ) {
545+ buffer .append (", comments=\" " ); //$NON-NLS-1$
546+ buffer .append (comment );
547+ buffer .append ('\"' );
548+ }
549+
550+ buffer .append (')' );
551+ return buffer .toString ();
552+ }
553+
469554 /**
470555 * 评论模板节点ID
471556 */
0 commit comments