The <commentGenerator> Element

The <commentGenerator> element is used to define properties of the comment generator. The comment generator is used to generate comments and annotations for the various elements generated by MyBatis Generator (MBG) (Java fields, Java methods, Kotlin files, XML elements, etc.). There are two purposes for comments:

  • Inform users that the elements are generated and are subject to regeneration (i.e. - they shouldn't be altered).
  • Support the Java and XML merge functionality. The Java and XML mergers use the generated annotations to understand what elements can be deleted during merge operations.

This element is an optional child element of the <context> element.

In the runtimes that generate Java code, the comment generator will add a jakarta.annotation.Generated annotation to generated Java elements.

Generated XML mappers will contain XML comments with the string "@mbg.generated".

In the Kotlin runtime, a simple comment is added to all generated Kotlin files.

The default implementation is org.mybatis.generator.internal.DefaultCommentGenerator. The default implementation is designed for extensibility if you only want to modify certain behaviors.

Required Attributes

None

Optional Attributes

Attribute Description
type This may be used to specify the type of the user provided Comment Generator. The class must implement the interface org.mybatis.generator.api.CommentGenerator and must have a public default constructor. The attribute also accepts the special value DEFAULT in which case the default implementation will be used (this has the same effect as not specifying the type).

Child Elements

Supported Properties

This table lists the properties of the default comment generator that can be specified with the <property> child element:

Property Name Property Values
suppressAllComments This property is used to specify whether MBG will include any comments or annotations in the generated code. The property supports these values:
false This is the default value
When the property is false or unspecified, all generated elements will include comments or annotations indicating that the element is a generated element.
true When the property is true, no comment or annotation will be added to any generated element.

Warning: if you set this value to true, then Java and XML code merging will be effectively disabled.

If you disable all comments, you might find the UnmergeableXmlMappersPlugin useful. It will cause the generator to respect the overwrite flag for XML files.

minimizeComments This property is used to specify that MBG should only add the minimum comments and annotations required to support Java and XML merging. The property supports these values:
false This is the default value
When the property is false or unspecified, the settings of the other properties govern the format and content of the generated comments and annotations.
true When the property is true, no dates or other comments will be added to any generated comment or annotation.
suppressDate This property is used to specify whether MBG will include the generation timestamp in the generated comments and annotations. The timestamp is always formatted in ISO-8601 format (as required in the "@Generated" annotation). The property supports these values:
false This is the default value
When the property is false or unspecified, all generated comments and annotations will include the timestamp when the element was generated.
true When the property is true, no timestamp will be added to the generated comments or annotations.
addRemarkComments This property is used to specify whether MBG will include table and column remarks from the database table in the generated comments. Remarks are added in two places: 1) in a general comment in a generated model class and 2) in a comment on a generated field in a model class.
false This is the default value
When the property is false or unspecified, generated comments will not include table and column remarks from the database table when the element was generated.
true When the property is true, table and columns remarks from the database table will be added to generated comments.

Warning: If either the "suppressAllComments" or "minimizeComments" options are true, this option will be ignored.

Example

This element specifies that we do not want the timestamp added to any generated comment or annotation:

<commentGenerator>
  <property name="suppressDate" value="true" />
</commentGenerator>