The <clientGenerator> Element

The <clientGenerator> element is used to define properties of the client generator. The client generator builds interfaces and classes that allow easy use of the generated model and XML map files (if any). For MyBatis, the generated objects take the form of mapper interfaces. If the <context> targetRuntime spepcifies either "MyBatis3DynamicSql" or "MyBatis3Kotlin", then the client generator will also be used to generate the "support" classes that define a metamodel for the database table. This element is an optional child element of the <context> element. If you do not specify this element, then MyBatis Generator (MBG) will not generate client interfaces and classes.

Required Attributes

Attribute Description
type For the legacy runtimes, this attribute is required and is used to specify the type of mapper that should be generated. The options specify if, and how much, XM should be generated. For the newer runtimes based on MyBatis Dynamic SQL, this attribute is optional and ignored.

The attribute accepts the following values for selecting a client type:

If the <context> targetRuntime is MyBatis3DynamicSql this attribute is optional and ignored.
If the <context> targetRuntime is MyBatis3Kotlin this attribute is optional and ignored.
If the <context> targetRuntime is MyBatis3 the following predefined values can be used:
ANNOTATEDMAPPER The generated objects will be Java interfaces for the MyBatis 3.x mapper infrastructure. The interfaces will be based on annotations, and MyBatis 3.x SqlProviders. No XML mapper files will be generated.

The ANNOTATEDMAPPER requires MyBatis version 3.0.4 or higher.

MIXEDMAPPER The generated objects will be Java interfaces for the MyBatis 3.x mapper infrastructure. The interfaces will be based on a mix of annotations and XML. An annotation will be used where a simple annotation will work. This client will not generate and SQL Provider, so all complex dynamic SQL will be generated in XML.

The MIXEDMAPPER requires MyBatis version 3.0.4 or higher.

XMLMAPPER The generated objects will be Java interfaces for the MyBatis 3.x mapper infrastructure. The interfaces will be dependent on generated XML mapper files.
If the <context> targetRuntime is MyBatis3Simple the following predefined values can be specified:
ANNOTATEDMAPPER The generated objects will be Java interfaces for the MyBatis 3.x mapper infrastructure. The interfaces will be based on annotations, and MyBatis 3.x SqlProviders. No XML mapper files will be generated.

The ANNOTATEDMAPPER requires MyBatis version 3.0.4 or higher.

XMLMAPPER The generated objects will be Java interfaces for the MyBatis 3.x mapper infrastructure. The interfaces will be dependent on generated XML mapper files.
targetPackage This is the package where the generated interfaces and implementation classes will be placed. In the default generators, the property "enableSubPackages" controls how the actual package is calculated. If true, then the calculated package will be the targetPackage plus sub packages for the table's catalog and schema if they exist. If "enableSubPackages" is false (the default) then the calculated package will be exactly what is specified in the targetPackage attribute. MBG will create folders as required for the generated packages.
targetProject This is used to specify a target project for the generated interfaces and classes. When running in the Eclipse environment, this specifies the project and source folder where the objects will be saved. In other environments, this value should be an existing directory on the local file system. MBG will not create this directory if it does not exist.

Child Elements

Supported Properties

This table lists the properties of the default client generators that can be specified with the <property> child element:

Property Name Property Values
dynamicSqlSupportPackage This property is only applicable to target runtimes MyBatis3DynamicSql or MyBatis3Kotlin. Use this property to set the package where generated MyBatis Dynamic SQL Support classes will be placed. The calculated package can be modified by the "enableSubPackages" property in the same way as the "targetPackage". If not specified, the package will be the same as "targetPackage" specified above.

Since version 1.4.1

enableSubPackages This property is used to select whether MBG will generate different Java packages for the objects based on the catalog and schema of the introspected table.

For example, suppose a table MYTABLE in schema MYSCHMA. Also suppose that the targetPackage attribute is set to "com.mycompany". If this property is true, the generated client code for the table will be placed in the package "com.mycompany.myschema". If the property is false, the generated SQL Map will be placed in the "com.mycompany" schema.

The default value is false.

rootInterface This property is ignored if the target runtime is "MyBatis3Kotlin"
This property can be used to specify a super interface for all generated interface objects. This value may be overridden by specifying the rootInterface property on a Table configuration.

Important: MBG does not verify that the interface exists, or is a valid Java interface.

If specified, the value of this property should be a fully qualified interface name (like com.mycompany.MyRootInterface).

injectModelIntoRootInterface This property is ignored if the target runtime is "MyBatis3Kotlin"
If "true", then the generated model type will be added as a type parameter to a specified "rootInterface". This property can be used to generate mappers based on a generic super interface. The model will be added as a type parameter if, and only if, the related "rootInterface" is generic and has exactly one type parameter. If specified here, then the setting applies to all mappers generated in this context. This value may be overridden by specifying the injectModelIntoRootInterface property on a Table configuration.

If "true", then the related rootInterface should include the generic specification (like com.mycompany.MyRootInterface<T extends Entity>). If there is no type parameter in the related rootInterface, then this property has no effect.

useSnakeCaseIdentifiers This property is ignored if the target runtime is "MyBatis3" or "MyBatis3Simple"
If true, then generated MyBatis Dynamic SQL support classes that model database tables and columns will use snake case names (e.g., ORDER_MASTER) rather than camel case names (e.g., orderMaster). This helps avoid awkward parameter naming for some methods where we appended an underscore to generated parameters in some cases. If specified here, then the setting will apply to all objects generated in this context. This value may be overridden by specifying the useSnakeCaseIdentifiers property on a Table configuration.

Example

This element specifies that we always want to place generated interfaces and objects in the "test.mapper" package and that we want to use subpackages based on the table schema and catalog. It also specifies that we want to generate mapper interfaces that reference an XML configuration file for MyBatis3.

<clientGenerator targetPackage="test.mapper"
     targetProject="\MyProject\src" type="XMLMAPPER">
  <property name="enableSubPackages" value="true" />
</clientGenerator>