The <table> Element
The <table> element is used to select a table in the database for introspection. Selected tables will cause the following objects to be generated for each table:
- A MyBatis formatted SQL Map file
- A set of classes that form the "model" of the table including:
- A class to match the primary key of the table (if the table has a primary key).
- A class to match the fields in the table that are not in the primary key, and non BLOB fields. This class will extend the primary key if there is one.
- A class to hold any BLOB fields in the table, if there are any. This class will extend one of the two previous classes depending on the configuration of the table.
- A class that is used to generate dynamic where clauses in the different "by example" methods (selectByExample, deleteByExample).
- (Optionally) A MyBatis mapper interface
At least one <table> element must be specified as a required child element of the <context> element. You can specify unlimited table elements.
Database Identifiers
MyBatis Generator (MBG) tries to deal with the case sensitivity of database identifiers automatically.
In most cases, MBG is able to find tables regardless of what you
specify for catalog
, schema
, and tableName
attributes. MBG's process follows these steps:
- If either of the
catalog
,schema
, ortableName
attributes contain a space, then MBG will look for tables based on the exact case specified. In this case, MBG will automatically delimit the table identifiers in the generated SQL. - Else if the database reports that identifiers are stored in upper case, MBG will automatically convert any table identifier to upper case.
- Else if the database reports that identifiers are stored in lower case, MBG will automatically convert any table identifier to lower case.
- Else MBG will look for tables based on the exact case specified.
In most cases, this process works perfectly. However, there are cases where it will fail. For example, suppose you create a table like this:
create table "myTable" ( ...some columns )
Because the table name is delimited, most databases will create the
table with the exact case specified - even if the database normally stores
identifiers in upper case. In this instance, you should specify the attribute
delimitIdentifiers="true"
in the table configuration.
Required Attributes
Attribute | Description |
---|---|
tableName | The name of the database table (not including the schema or catalog). The specified value can contain SQL wildcards if so desired. |
Optional Attributes
Attribute | Description | ||||||
---|---|---|---|---|---|---|---|
schema | The database schema - not required if your database does not use schemas, or if there is a default schema. The specified value can contain SQL wildcards if so desired. | ||||||
catalog | The database catalog - not required if your database does not use catalogs, or if there is a default catalog. | ||||||
alias | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" If specified, this value will be used to alias the table and all column names in any generated SQL select statement. Column names will be aliased with the pattern alias_actualColumnName. |
||||||
domainObjectName | The base name from which generated object names will be generated.
If not specified, MBG will generate a name automatically based on
the tableName. The name (either specified here, or generated
automatically) will be used to compute generated domain class names
and DAO class names.
You can specify a package fragment in the domain object name. For example,
if you specify |
||||||
mapperName | The name of the MyBatis3 generated mapper class and XML file.
If not specified, the name will be whatever the domain object name is, plus the
word "Mapper".
You can specify a package fragment in the mapper name. For example,
if you specify Since version 1.3.4 |
||||||
sqlProviderName | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" The name of the MyBatis3 generated SQL provider class (which may or may not be generated based on the configuration). If not specified, the name will be whatever the domain object name is, plus the word "SqlProvider". You can specify a package fragment in the SQL provider name. For example,
if you specify Since version 1.3.4 |
||||||
enableInsert | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether an insert statement should be generated. The default is true. |
||||||
enableSelectByPrimaryKey | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether a select by primary key statement should be generated. Regardless of this setting, the statement will not be generated if the table does not have a primary key. The default is true. |
||||||
enableSelectByExample | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether a select by example statement should be generated. This statement enables many different dynamic queries to be generated at run time. The default is true. |
||||||
enableUpdateByPrimaryKey | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether an update by primary key statement should be generated. Regardless of this setting, the statement will not be generated if the table does not have a primary key. The default is true. |
||||||
enableDeleteByPrimaryKey | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether an delete by primary key statement should be generated. Regardless of this setting, the statement will not be generated if the table does not have a primary key. The default is true. |
||||||
enableDeleteByExample | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether a delete by example statement should be generated. This statement enables many different dynamic deletes to be generated at run time. The default is true. |
||||||
enableCountByExample | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether a count by example statement should be generated. This statement will return the number of rows in a table that match an example. The default is true. |
||||||
enableUpdateByExample | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" Signifies whether an update by example statement should be generated. This statement will update rows in a table that match an example. If true, an update by example "selective" statement will also be generated. The "selective" statement will only update columns where the corresponding value in the record parameter is non-null. The default is true. |
||||||
selectByPrimaryKeyQueryId | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" This value will be added to the select list of the select by primary key statement in this form: "'<value>' as QUERYID". This can be useful for identifying queries in DBA tracing tools at run time. If you use thus value, you should specify a unique id for every different query generated by MBG. |
||||||
selectByExampleQueryId | This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" This value will be added to the select list of the select by example statement in this form: "'<value>' as QUERYID". This can be useful for identifying queries in DBA tracing tools at run time. If you use thus value, you should specify a unique id for every different query generated by MBG. |
||||||
modelType |
This attribute is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" This attribute is used to override the default model type if you desire to do so for this table. If not specified, MBG will generate domain objects based on the context default model type. 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:
|
||||||
escapeWildcards | Signifies whether SQL wildcards ('_' and '%') in the schema and tableName should
be escaped when searching for columns. This is required by some drivers if the
schema or tableName includes an SQL wildcard (for example, if a table name is
MY_TABLE, some drivers require that the underscore character be escaped).
The default is false. |
||||||
delimitIdentifiers | Signifies whether MBG should use the exact case specified when searching
for tables and then delimit the identifiers in the generated SQL. See the
discussion above for more details.
The delimiter characters are specified on the <context> element. The default is false unless the |
||||||
delimitAllColumns | Signifies whether MBG should add delimiters to all column names in the generated
SQL. This is an alternative to coding a <columnOverride>
for every column
specifying that the column should be delimited. This is useful for databases
like PostgreSQL that are case sensitive with identifiers.
The delimiter characters are specified on the <context> element. The default is false. |
Child Elements
- <property> (0..N)
- <generatedKey> (0 or 1)
- <domainObjectRenamingRule> (0 or 1)
- <columnRenamingRule> (0 or 1)
- <columnOverride> (0..N)
- <ignoreColumn> (0..N)
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 |
---|---|
constructorBased | This property is ignored is the target runtime is "MyBatis3Kotlin" This property is used to select whether MyBatis Generator will generate a constructor for the class that accepts a value for each field in the class. Also, the SQL result map will be built to use the constructor rather than the "setter" for each field. This property is ignored (and forced "true") if the "immutable" property is set "true". The default value is false. |
dynamicSqlSupportClassName | This property is ignored unless the target runtime is "MyBatis3DynamicSQL" or "MyBatis3Kotlin" The name of the generated MyBatis Dynamic SQL support class. If not specified, the name will be whatever the domain object name is, plus the word "DynamicSqlSupport". You can specify a package fragment in the support class name. For example,
if you specify Since version 1.4.1 |
dynamicSqlTableObjectName | The name to use for the inner class generated in support classes for MyBatis Dynamic SQL. If not specified, the name will be the calculated domain object name (typically the table name). The value if this property will be used as is for the inner class name (case-sensitive). An outer class property name will also be calculated from this name (typically with the initial letter lower-cased). |
ignoreQualifiersAtRuntime | If true, then
MBG will not add the schema or catalog to the table name in the generated SQL.
This is useful if you have tables with the same name in several schemas -
you can use MBG to generate objects based on the table in one schema,
but not include the schema for runtime.
The default value is false. |
immutable | This property is ignored is the target runtime is "MyBatis3Kotlin" This property is used to select whether MBG will generate immutable model classes - this means that the classes will not have "setter" methods and the constructor will accept values for each field in the class. If true, this forces the model classes to be built with paramterized constructors regardless of the value of the "constructorBased" property. The default value is false. |
modelOnly |
This property is used to select whether MBG will only generate
model classes for a table.
If true, then a Java client will not be generated. If an <sqlMapGenerator> is configured and this property is set true then MBG will only generate result map elements in the SQL Mapper XML file for this table. If true, then this value overrides any of the "enable*" attributes on the <table> element - no CRUD methods will be generated. The default value is false. |
rootClass | This attribute is ignored if the target runtime is "MyBatis3Kotlin" This property can be used to specify a root class for all generated Java model objects. MBG will specify this value as the super class of the primary key object, if the table has a primary key, or the record object otherwise. The value specified in this property will override the rootClass property set on the Java Model Generator
configuration if any is set.
Important: If MBG is able to load the root class, it will not override a property in the root class that exactly matches a property that would normally be generated. An exact match of a property is defined as follows
If specified, the value of this property should be a fully qualified class name (like com.mycompany.MyRootClass). |
rootInterface | This property is ignored if the target runtime is "MyBatis3Kotlin" This property can be used to specify a super interface for all generated DAO interface objects. The value specified in this property will override the rootInterface property set on the Java Client Generator
configuration if any is set.
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). |
runtimeCatalog | If you specify a value for this property, than MBG will use that
value as the catalog in the generated SQL rather than the catalog
as configured above.
This is useful if you want to generate code against one catalog, but
want to use a different catalog at runtime. |
runtimeSchema | If you specify a value for this property, than MBG will use that
value as the schema in the generated SQL rather than the schema
as configured above.
This is useful if you want to generate code against one schema, but
want to use a different schema at runtime. |
runtimeTableName | If you specify a value for this property, than MBG will use that
value as the table name in the generated SQL rather than the tableName
as configured above.
This is especially useful on Oracle if you want to generate objects to use
a public synonym. In that case, you will need to generate the objects against the
actual table that the synonym points to, then specify the synonym name in
this property. You should also specify the ignoreQualifiersAtRuntime
property in most cases with public synonyms. |
selectAllOrderByClause | This property can be used to specify an order by clause that will be added to
the selectAll method. This is only applicable if you are using the
MyBatis3Simple target runtime. MBG will prepend order by
to anything you specify in this property, so the property should contain
a column list only (e.g ID1, ID2 or ID1 desc, ID2 asc )
|
trimStrings | This attribute is ignored is the target runtime is "MyBatis3Kotlin" This property is used to select whether MyBatis Generator adds code to trim
the white space from character fields returned from the database.
This can be useful if your database stores data in CHAR fields rather than
VARCHAR fields. When true, MyBatis Generator will insert code to trim character fields.
Can be overridden with the The default value is inherited from the <javaModelGenerator>, otherwise false. |
useActualColumnNames | If true, then
MBG will use column names as returned from the database metadata as the properties
of the generated domain objects. If false (default), MBG will attempt to camel
case the returned names. In either event, the name can be specified explicitly by the
<columnOverride> element in which case this property will be ignored for the
specified column.
For example, suppose a table contains a column START_DATE. If the value of
this property is "true", then MBG will generate the property name
as The default value is false. |
useColumnIndexes | This property is ignored is the target runtime is "MyBatis3DynamicSql" or "MyBatis3Kotlin" If true, then MBG will generate resultMaps that use column index rather than column name in the result mappings. This is useful when a table has column names differentiated only by the case of the name (e.g. "first name" and "First Name"). There is a slight performance benefit with this support also. The default value is false. Important Note: This property is not supported if the target runtime is for MyBatis version 3. |
useCompoundPropertyNames | If true, then
MBG will use generate property names by contatenating the column name with the
column reparks. This can be usefull in databases created by 4th generation languages
where the column names are generated (e.g. FLD2237), but the remarks contain useful
information (e.g. "customer id"). In this case, MBG will generate a property
name of FLD2237_CustomerId.
The default value is false. |
Example
This element specifies that we always want to generate code for a table called MYTABLE in schema MYSCHEMA. We also want to ignore a column called "fred" in the table, and we want to override the column "BEG_DATE" so that the generated property name will be "startDate".
<table tableName="MYTABLE" schema="MYSCHEMA"> <ignoreColumn column="fred"/> <columnOverride column="BEG_DATE" property="startDate"/> </table>