The <context> Element

The <context> element is used to specify the environment for generating a set of objects. Child elements are used to specify the database to connect to, the type of objects to generate, and the tables to introspect. Multiple <context> elements can be listed inside an <generatorConfiguration> element to allow generating objects from different databases, or with different generation parameters, in the same run of MyBatis Generator (MBG).

Required Attributes

Attribute Description
id A unique identifier for this context. This value will be used in some error messages.

Optional Attributes

Attribute Description
defaultModelType This property is used to set the default for generated model types. The model type defines how MBG will generate domain classes. With some model types MBG will generate a single domain class for each table, with others MBG may generate different classes depending on the structure of the table. The property supports these values:
conditional This is the default value
This model is similar to the hierarchical model except that a separate class will not be generated if that separate class would only contain one field. So if a table has only one primary key field, that field will be merged into the base record class.
flat This model generates only one domain class for any table. The class will hold all fields in the table.
hierarchical This model will generate a primary key class if the table has a primary key, another class that holds any BLOB columns in the table, and another class that holds the remaining fields. There is an appropriate inheritance relationship between the classes.
targetRuntime This property is used to specify the runtime target for generated code. The property supports these special values:
MyBatis3 This is the default value
With the value, MBG will generate objects that are compatible with MyBatis versions 3.0 and higher, and JSE 5.0 and higher (e.g. the Java model and mapper interfaces will use generic types). The "by example" methods in these generated objects support virtually unlimited dynamic where clauses. Additionally, the Java objects generated with these generators support many JSE 5.0 features including parameterized types and annotations.
MyBatis3Simple This is the default value
With the value, MBG will generate objects that are compatible with MyBatis versions 3.0 and higher, and JSE 5.0 and higher (e.g. the Java model and mapper interfaces will use generic types). The mappers generated with this target runtime are very basic CRUD operations only with no "by example" methods and very little dynamic SQL. The Java objects generated with these generators support many JSE 5.0 features including parameterized types and annotations.
Ibatis2Java2 With this value, MBG will generate objects that are compatible with iBATIS versions 2.2.0 and higher (but not iBATIS 3), and all levels of Java 2. The "by example" methods in these generated objects support virtually unlimited dynamic where clauses. The objects generated with this set of generators are not 100% compatible with the objects generated with the original version of Abator, or one of the other sets of code generators.
Ibatis2Java5 With the value, MBG will generate objects that are compatible with iBATIS versions 2.2.0 and higher (but not iBATIS 3), and JSE 5.0 and higher (e.g. the Java model and DAO classes will use generic types). The "by example" methods in these generated objects support virtually unlimited dynamic where clauses. Additionally, the Java objects generated with these generators support many JSE 5.0 features including parameterized types and annotations. The objects generated with this set of generators are not 100% compatible with the objects generated with the original version of Abator, or one of the other sets of code generators.

If you want to create a different code generator in entirety, then use this value to specify the fully qualified name of a class that extends org.mybatis.generator.api.IntrospectedTable. With this, you can create your own code generator and plug it in to the code generation engine. See the Extending MyBatis Generator page for more information.

introspectedColumnImpl Use this value to specify the fully qualified name of a class that extends org.mybatis.generator.api.IntrospectedColumn. This is used if you want to change the behavior of the code generators when calculating column information. See the Extending MyBatis Generator page for more information.

Child Elements

Supported Properties

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

Property Name Property Values
autoDelimitKeywords If true, then MBG will delimit SQL keywords if they are used as column names in tables. MBG maintains a list of SQL keywords for many different databases. However, the list may not be totally comprehensive. If a particular keyword is not on MBG's list, you may force the column to be delimited with a <columnOverride>.

See the source code for the class org.mybatis.generator.internal.db.SqlReservedWords for a list of keywords recognized by MBG.

The default value is false.

beginningDelimiter The value to use as the beginning identifier delimiter for SQL identifiers that require delimiters. MBG will automatically delimit SQL identifiers if the identifier contains a space. MBG will also delimit SQL identifiers if specifically requested in a <table> or <columnOverride> configuration.

The default value is double quotes (").

endingDelimiter The value to use as the ending identifier delimiter for SQL identifiers that require delimiters. MBG will automatically delimit SQL identifiers if the identifier contains a space. MBG will also delimit SQL identifiers if specifically requested in a <table> or <columnOverride> configuration.

The default value is double quotes (").

javaFileEncoding Use this property to specify an encoding to use when working with Java files. Newly generated Java files will be written to the file system in this encoding, and existing Java files will be read with this encoding when performing a merge. If not specified, then the platform default encoding will be used.

See java.nio.charset.Charset for information on valid encodings.

javaFormatter Use this property to specify the full class name of a user provided formater for generated Java files. The class must implement org.mybatis.generator.api.JavaFormatter and must have a default (no argument) constructor. Each context holds a single instance of the Java formatter. The default Java formatter is org.mybatis.generator.api.dom.DefaultJavaFormatter. The default formatter uses the formatting built into the Java DOM classes.
xmlFormatter Use this property to specify the full class name of a user provided formater for generated XML files. The class must implement org.mybatis.generator.api.XmlFormatter and must have a default (no argument) constructor. Each context holds a single instance of the XML formatter. The default XML formatter is org.mybatis.generator.api.dom.DefaultXmlFormatter. The default formatter uses the formatting built into the XML DOM classes.