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>

The supplied plugins are all 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.CachePlugin

This plugin adds a <cache> element to generated SQL maps. This plugin is for MyBatis3 targeted runtimes only.

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 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 adds equals and hashCode methods to the Java model objects generated by MBG.

The equals method generated by this class is correct in most cases, but will probably NOT be correct if you have specified a rootClass - because our equals method only checks the fields it knows about.

org.mybatis.generator.plugins.MapperConfigPlugin

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 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 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 objects generated by MBG. 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:

  • 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.SqlMapConfigPlugin

This plugin generates a skeleton SqlMapConfig.xml file that contains references to the SqlMap.xml files generated by MBG. This file can be used to configure iBATIS 2.x environments.

This plugin accepts three properties:

  • fileName (optional) the name of the generated file. this defaults to "SqlMapConfig.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.ToStringPlugin

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

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>