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:
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.
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:
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!