Supplied Plugins

As usage of MyBatis Generator (MBG) grows, we find it increasingly useful to add capabilities through plugins, rather than adding to the complexity of the base code generators. Plugins are a modular and easily understood mechanism for extending MBG. For more information on writing a plugin, see Implementing Plugins. For information on configuring plugins, see <plugin>

Most of the supplied plugins are in the package org.mybatis.generator.plugins. The supplied plugins demonstrate different types of tasks that can be accomplished with MBG plugins. Source code for the plugins is available with the MBG downloads, or can be viewed online here.

org.mybatis.generator.plugins.CacheNamespacePlugin

This plugin adds a @CacheNamespace annotation to generated mapper interface (Kotlin or Java). This plugin is for MyBatis3 targeted runtimes only.

This plugin accepts the following properties. All are optional and, if specified, most values (except "cache_skip") will be passed directly to the corresponding property on the generated @CacheNamespace annotation. The cache_eviction and cache_implementation properties must be fully qualified class names. The class will be added to the import list in the mapper, and the short name will be used on the generated annotation. If "cache_skip" is set to "true" on the plugin configuration, or for any table, the annotation will not be generated.

  • cache_blocking
  • cache_flushInterval
  • cache_readWrite
  • cache_size
  • cache_eviction (must be a fully qualified class name)
  • cache_implementation (must be a fully qualified class name)
  • cache_skip

Any property can be overridden by specifying the property on a <table> element.

org.mybatis.generator.plugins.CachePlugin

This plugin has no impact when the target runtime in use does not generate XML.

This plugin adds a <cache> element to generated SQL maps.

This plugin accepts the following properties. All are optional and, if specified, the values will be passed directly to the corresponding property on the generated <cache> element.

  • cache_eviction
  • cache_flushInterval
  • cache_readOnly
  • cache_size
  • cache_type

Any property can be overridden by specifying the property on a <table> element.

org.mybatis.generator.plugins.CaseInsensitiveLikePlugin

This plugin has no impact when the target runtime in use is based on MyBatis Dynamic SQL.

This plugin adds methods to the Example class (actually to the Criteria inner class) to support case insensitive LIKE searches. This demonstrates adding functionality to the example classes via a plugin, rather than extending the class.

org.mybatis.generator.plugins.EqualsHashCodePlugin

This plugin has no impact and is not needed when the target runtime in use is MyBatis3Kotlin.

This plugin adds equals and hashCode methods to the Java model objects generated by MBG.

By default, the equals and hashCode method checks the only fields it knows about generated by this class.

This plugin accepts one property:

  • useEqualsHashCodeFromRoot (optional) If you have specified root class, you can use this property to call super equals and hashCode methods for check fields of root class. It can be useful for tables with inheritance relation.

org.mybatis.generator.plugins.FluentBuilderMethodsPlugin

This plugin has no impact and is not needed when the target runtime in use is MyBatis3Kotlin.

This plugin adds fluent builder methods to the generated model classes.

Example:
  • given the domain class MyDomainClass
  • with setter-method setFoo(String foo)
  • with setter-method setBar(Integer bar)
The plugin will create the additional Methods:
  • public MyDomainClass withFoo(String foo)
  • public MyDomainClass withBar(Integer bar)

This plugin does not accept any properties.

Using this plugin, you can configure the property values fluently with chained method calls. Example: new MyDomain().withFoo("Test").withBar(4711);

org.mybatis.generator.plugins.IgnoreViewsPlugin

This plugin will filter out any table of type "VIEW" from a code generation run. This can be useful if you use a wildcard to select many tables and views, but don't want to generate code for the views.

org.mybatis.generator.plugins.MapperAnnotationPlugin

This plugin has no impact and is not needed when the target runtime in use is based on MyBatis Dynamic SQL.

This plugin adds the @Mapper annotation to generated mapper interfaces. This plugin should only be used in MyBatis3 environments.

org.mybatis.generator.plugins.MapperConfigPlugin

This plugin has no impact and is not needed when the target runtime in use is based on MyBatis Dynamic SQL.

This plugin generates a skeleton MapperConfig.xml file that contains references to the XML mapper files generated by MBG. This file can be used to configure MyBatis 3.x environments.

This plugin accepts three properties:

  • fileName (optional) the name of the generated file. this defaults to "MapperConfig.xml" if not specified.
  • targetPackage (required) the name of the package where the file should be placed. Specified like "com.mycompany.sql".
  • targetProject (required) the name of the project where the file should be placed.

Note: targetPackage and targetProject follow the same rules as the targetPackage and targetProject values on the sqlMapGenerator configuration element.

org.mybatis.generator.plugins.RenameExampleClassPlugin

This plugin has no impact and is not needed when the target runtime in use is based on MyBatis Dynamic SQL.

This plugin demonstrates usage of the initialized method by renaming the generated example classes generated by MBG.

This plugin accepts two properties:

  • searchString (required) a regular expression used to search the default generated name of the example class.
  • replaceString (required) the string to be inserted on matches of the searchString.

For example, to rename the generated example classes from xxxExample to xxxCriteria, specify Example$ for searchString and Criteria for replaceString

org.mybatis.generator.plugins.RowBoundsPlugin

This plugin has no impact and is not needed when the target runtime in use is based on MyBatis Dynamic SQL.

This plugin will add a new version of the selectByExample method that accepts a RowBounds parameter. This supports the MyBatis RowBounds function where a returned result list can be limited in length, and a start position can be specified. This can be useful in paging applications.

This plugin is only valid for MyBatis3 target runtime.

org.mybatis.generator.plugins.SerializablePlugin

This plugin adds the marker interface java.io.Serializable to the Java model classes, and/or the Kotlin data classes, generated by MBG. For Java, this plugin also adds the serialVersionUID field to the model classes.

Important: This is a simplistic implementation of java.io.Serializable and does not attempt to do any versioning of classes.

This plugin accepts two properties. Note that both properties are ignored for Kotlin data classes. For Kotlin data classes, the plugin will always and only add java.io.Serializable.

  • addGWTInterface (optional) True/False. If true, the plugin will add the Google Web Toolkit (GWT) IsSerializable interface to the model objects. The default is false.
  • suppressJavaInterface (required) True/False. If true, the plugin will NOT add the java.io.Serializable interface. This is for the case where the objects should be serializable for GWT, but not in the strict Java sense. The default is false.

org.mybatis.generator.plugins.ToStringPlugin

This plugin has no impact and is not needed when the target runtime in use is MyBatis3Kotlin.

This plugin adds toString() methods to the generated model classes.

This plugin accepts one property:

  • useToStringFromRoot (optional) If you have specified root class, you can use this property to call super toString method for print fields of root class. It can be useful for tables with inheritance relation.

org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin

This plugin has no impact and is not needed when the target runtime in use is based on MyBatis Dynamic SQL.

This plugin will disable the XML merge function for generated mapper XML files. This will cause the generator to respect the overwrite flag for XML files in the same way it does for Java files - if overwrite is true, then an existing file will be overwritten, else a new file will be written with a unique name.

This plugin can be helpful if you disable all comments.

org.mybatis.generator.plugins.VirtualPrimaryKeyPlugin

This plugin can be used to specify columns that act as primary keys, even if they are not defined as primary key in the database. This is useful in the case where the database table does not have a primary defined. Normally, MBG will generate a very limited set of methods if there is no primary key. This plugin can be used to enable generation of the full compliment of MBG methods.

To use the plugin, add the property "virtualKeyColumns" to your <table> configuration and set the value to a comma or space delimited list of column names that should be treated as primary keys. The column names must exactly match the column names returned from the database (typically all UPPERCASE). For example:

  <table tableName="foo">
    <property name="virtualKeyColumns" value="ID1, ID2" />
  </table>