Implementing Plugins

Plugins can be used to modify or add to the objects that are generated by MyBatis Generator. Plugins must implement the interface org.mybatis.generator.api.Plugin. The plugin interface contains many methods that are called in different phases of the code generation process. Implementing the entire interface is generally not needed for any particular plugin. Therefore, most plugins should extend the adapter class org.mybatis.generator.api.PluginAdapter. The adapter class provides basic plugin support, and implements no-operation methods for most of the interface methods (similar to Swing adapter classes).

MyBatis Generator supplies several plugins (all are in the package org.mybatis.generator.plugins). The supplied plugins demonstrate different types of tasks that can be accomplished with plugins. Source code for the plugins is available with the downloads, or can be viewed online here.

Plugin Lifecycle

Plugins have a lifecycle. Plugins are created during the initialization of the code generation process and are called, in order, in different phases of the process. The following list shows the basic lifecycle of a plugin:

  1. Plugin created through the default constructor
  2. setContext method called
  3. setProperties method called
  4. validate method called. If this method returns false, then no further methods in the plugin will be called
  5. For each table in the configuration:
    1. initialized method called
    2. Java Client Methods:1,2
      1. clientXXXMethodGenerated(Method, TopLevelClass, IntrospectedTable) - these methods are called as each method of a Java client implementation class is generated.
      2. clientXXXMethodGenerated(Method, Interface, IntrospectedTable) - these methods are called as each method of the Java client interface is generated.
      3. clientGenerated(Interface, TopLevelClass, IntrospectedTable) method called
    3. Model Methods:1
      1. modelFieldGenerated, modelGetterMethodGenerated, modelSetterMethodGenerated for each field in the class
      2. modelExampleClassGenerated(TopLevelClass, IntrospectedTable)
      3. modelPrimaryKeyClassGenerated(TopLevelClass, IntrospectedTable)
      4. modelBaseRecordClassGenerated(TopLevelClass, IntrospectedTable)
      5. modelRecordWithBLOBsClassGenerated(TopLevelClass, IntrospectedTable)
    4. SQL Map Methods:1
      1. sqlMapXXXElementGenerated(XmlElement, IntrospectedTable) - these methods are called as each element of the SQL map is generated
      2. sqlMapDocumentGenerated(Document, IntrospectedTable)
      3. sqlMapDocument(GeneratedXmlFile, IntrospectedTable)
    5. contextGenerateAdditionalJavaFiles(IntrospectedTable) method called
    6. contextGenerateAdditionalXmlFiles(IntrospectedTable) method called
  6. contextGenerateAdditionalJavaFiles() method called
  7. contextGenerateAdditionalXmlFiles() method called

Notes:
1 - These methods will be called by the packaged code generators. If you supply a custom code generator, then these methods will only be called if the custom code generator calls them.
2 - The Java client methods will only be called is a Java client generator is configured.

Coding Plugins

The best way to implement a plugin is to extend the org.mybatis.generator.api.PluginAdapter class and override only the specific methods you need in your plugin.

Methods in the plugin interface can be used to modify code generated by default, or to add additional generated code. Examples of things that can be accomplished with plugins are:

  • Add custom annotations to generated methods
  • Add additional methods to a generated class
  • Add additional elements to a generated XML file
  • Generate additional Java files
  • Generate additional XML files

The contextXXX methods will always be called. Other methods are called by the packaged code generators - and only if the rules for a table would cause the generation of a particular element. For example, the modelPrimaryKeyClassGenerated(TopLevelClass, IntrospectedTable) method will not be called if the table does not have a primary key.

Methods that return a boolean can be used to bypass code generation. If any of these methods return false, then the related item will not be added to the generated code. If there is more than one plugin configured, then the first plugin to return false from a method will cause MyBatis Generator to stop calling that method in all other plugins.

If you have an idea for a plugin, feel free to ask a question about it on the user list. We're here to help!