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