The <javaClientGenerator> Element

The <javaClientGenerator> element is used to define properties of the Java client generator. The Java client Generator builds Java interfaces and classes that allow easy use of the generated Java model and XML map files. For iBATIS2 target environments, these generated objects take the form of DAO interface and implementation classes. For MyBatis, the generated objects take the form of mapper interfaces. This element is a optional child element of the <context> element. If you do not specify this element, then MyBatis Generator (MBG) will not generate Java client interfaces and classes.

Required Attributes

Attribute Description
type This attribute is used to select one of the predefined Java Client generators, or to specify a user provided Java Client generator. Any user provided DAO generator must extend the class org.mybatis.generator.codegen.AbstractJavaClientGenerator class, and must have a public default constructor.

The attribute accepts the following values for selecting one of the predefined generators:

If the <context> targetRuntime is MyBatis3:
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:
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.
If the <context> targetRuntime is Ibatis2Java2 or Ibatis2Java5:
IBATIS The generated objects will conform to the (deprecated) iBATIS DAO framework.
GENERIC-CI The generated objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by constructor dependency injection. The generated objects will be in the form of DAO interfaces amd implementation classes.
GENERIC-SI The generated objects will rely only on the SqlMapClient. The SqlMapClient will be supplied by setter dependency injection. The generated objects will be in the form of DAO interfaces amd implementation classes.
SPRING The generated objects will conform to the Spring DAO framework.
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.

Note: the package for implementation classes may be overridden by specifying the optional implementationPackage attribute as shown below.

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.

Optional Attributes

Attribute Description
implementationPackage If specified, implementation classes will be placed in this package. In the default generators, the property "enableSubPackages" controls how the actual package is calculated. If true, then the calculated package will be the implementationPackage 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 implementationPackage attribute. MBG will create folders as required for the generated packages.

Child Elements

Supported Properties

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

Property Name Property Values
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 DAO interface and class 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.

exampleMethodVisibility This property is used to set the visibility of the different "ByExample" methods - selectByExample, deleteByExample, etc. If not specified, the methods will be public and will be declared in the interface. This property allows you to hide these methods if you only want to use them to implement other specialized methods.
public This is the default value
The generated methods in the implementation class will be public, and the methods will be declared in the interface.
private The generated methods in the implementation class will be private, and the methods will not be declared in the interface
protected The generated methods in the implementation class will be protected, and the methods will not be declared in the interface
default The generated methods in the implementation class will have default (package) visibility, and the methods will not be declared in the interface

Important note: this property is ignored if the target runtime is MyBatis3.

methodNameCalculator This property is used to select a method name calculator. A method name calculator can be used to provide different names for the DAO methods. You can select one of the predefined values, or you can specify the fully qualified name of a class that implements the org.mybatis.generator.api.DAOMethodNameCalculator interface if neither of the supplied options are appropriate in your environment.
default This is the default value
The generated methods names will be very simple ("insert", "updateByPrimaryKey", etc.)
extended The generated method names will include the name of the domain object associated with the method ("insertWidget", "updateWidgetByPrimaryKey", etc.)

Important note: this property is ignored if the target runtime is MyBatis3.

rootInterface 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).

Example

This element specifies that we always want to place generated interfaces and objects in the "'test.model" 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.

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