Extending MyBatis Generator
MyBatis Generator (MBG) is designed for extensibility. All code generation activities are performed using a simple DOM representation of Java, Kotlin, and XML elements.
The Java DOM is included in the package org.mybatis.generator.api.dom.java
The Kotlin DOM is included in the package org.mybatis.generator.api.dom.kotlin
The XML DOM is included in the package org.mybatis.generator.api.dom.xml
These classes are not sufficient for every conceivable code generation possibility, but they are quite useful for generating simple to moderately complex Java, Kotlin, and XML code.
Using options in the configuration file, you can provide your own implementations of many of the key code generation interfaces. You can also subclass any of the provided implementations to provide customized behaviors. This page will describe the public APIs available and provide pointers to the source code for further investigation. If you have any difficulty understanding how to extend MBG, feel free to send a note to the support mailing list at mybatis-user@googlegroups.com.
Extending Versus Plugging In
Although there are a number of different extension points shown on this page, in most cases it will be far easier to extend MBG through the use of a plugin. See the Implementing Plugins reference page for more information.
The primary extension point for creating a code generator is
org.mybatis.generator.api.AbstractRuntime. Implementing a code generator
is a non-trivial task and should only be contemplated when you want to completely replace
the code generation activities of MBG. In the years since the original release of Abator,
very few enhancement requests have appeared that could not be handled by a plugin.
Extension Points
MBG provides a number of different extension points. The following sections list the different methods of extending MBG, and describe the types of activities that can be achieved with the different extensions. If you need some help understanding the different options, feel free to ask a question on the user mailing list.
Runtimes
org.mybatis.generator.api.AbstractRuntime is an abstract class that can be extended to supply
different code generators than the versions supplied with MBG. A good example of
such an implementation would be a FreeMarker or Velocity template-based
implementation. In most other instances, coding a plugin is the simpler way to go.
If you choose to extend this class, you must supply code to generate files based on the generator's introspection
operations. You may choose to generate those files with the
technology of your choice. The base AbstractRuntime class holds an instance of
org.mybatis.generator.api.IntrospectedTable which holds the introspection results for a single table.
That class holds an instance of org.mybatis.generator.internal.rules.Rules that can be queried
to determine many of the rules for code generation.
MBG supplies several implementations of a runtime. The implementation is chosen
based on the value of the targetRuntime attribute of the
<context> element. In many cases it will be far simpler
to extend one of the built-in implementations rather than creating an implementation
from scratch. The following table shows the built-in implementations:
| TargetRuntime | Implementation |
|---|---|
| MyBatis3DynamicSql (default) | org.mybatis.generator.runtime.dynamicsql.java.JavaDynamicSqlRuntime |
| MyBatis3Kotlin | org.mybatis.generator.runtime.dynamicsql.kotlin.KotlinDynamicSqlRuntime |
| MyBatis3 | org.mybatis.generator.runtime.mybatis3.LegacyJavaRuntime |
| MyBatis3Simple | org.mybatis.generator.runtime.mybatis3.LegacySimpleJavaRuntime |
If you choose to implement this extension point, specify the fully qualified
class name of a builder that extends org.mybatis.generator.api.AbstractRuntime.AbstractRuntimeBuilder
in the targetRuntime
attribute of the <context> element.
org.mybatis.generator.api.IntrospectedColumn
IntrospectedColumn is a class that holds information about a
column as it is returned from database metadata. In some rare
circumstances it might be desirable to override this class to provide
your own implementation - especially if you create a new set of code
generators.
If you choose to implement this extension point, specify the fully qualified
class name of your implementation with the introspectedColumnImpl
attribute of the <context> element.
org.mybatis.generator.api.JavaTypeResolver
MBG calls methods in this interface to map JDBC types to Java types
during database introspection. The default implementation of this
interface is org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl.
You can provide your own implementation, and the default implementation has
been designed for extensibility.
To provide your own implementation, specify the fully qualified class name in the XML configuration like this:
<javaTypeResolver type="mypackage.MyImplementation">
...
</javaTypeResolver>
org.mybatis.generator.api.ShellCallback
MBG calls methods in this interface to perform functions that it cannot do on its own. The most important of these functions is translating project/package into a directory structure.
The default implementation of this interface isorg.mybatis.generator.internal.DefaultShellCallback.
The default implementation simply concatenates project and package together and creates
the necessary package directories if needed.
You can provide your own implementation. This would be the most important class to write if you want to integrate MBG into some other environment. For example, the Eclipse plugin provides an implementation of this interface that translates Eclipse project names into file system locations.
To provide your own implementation, supply an instance of the interface on the builder
org.mybatis.generator.api.MyBatisGenerator.Builder. This cannot be configured through XML. If you are
providing your own implementation of this interface, then we assume that you are also providing some collateral
code (like a new Ant task) to run your implementation.
org.mybatis.generator.api.ProgressCallback
MBG calls methods in this interface to report progress during the generation of files (a long-running process).
The default implementation of this interface isorg.mybatis.generator.internal.NullProgressCallback
which simply ignores all progress messages. You can provide an implementation of this interface to support progress
notifications and cancellation of code generation.
Implementing this interface would be important when integrating MBG into other IDE environments. The Eclipse plugin provides an implementation of this interface that hooks into Eclipse's progress notification system.
To provide your own implementation, supply an instance of the interface on the builder
org.mybatis.generator.api.MyBatisGenerator.Builder. This cannot be configured through XML. Again,
we assume that if you are providing your own implementation of this interface then you are also providing some
collateral code (like a new Ant task or an IDE integration) to run your implementation.
