The <generatedKey> Element

The <generatedKey> element is used to specify properties for auto generated keys (from identity field or sequences). If you specify this element, MyBatis Generator (MBG) will generate an appropriate <selectKey> element inside the generated <insert> element in the SQL map. This element is an optional child element of the <table> element.

Required Attributes

Attribute Description
column The column name of the generated column.
sqlStatement The SQL statement that will return the new value. If this is an identity column, then you can use one of the predefined special values, or substitute the proper statement for your database. The predefined special values are as follows:
Cloudscape This will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2 This will translate to: VALUES IDENTITY_VAL_LOCAL()
DB2_MF This will translate to:
SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1

Use this value for DB2 on zOS (Main Frames) and, in some cases, iSeries (AS/400)

Derby This will translate to: VALUES IDENTITY_VAL_LOCAL()
HSQLDB This will translate to: CALL IDENTITY()
Informix This will translate to: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1
MySql This will translate to: SELECT LAST_INSERT_ID()
SqlServer This will translate to: SELECT SCOPE_IDENTITY()
SYBASE This will translate to: SELECT @@IDENTITY
JDBC This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.

Important: This value will only produce valid code when the target runtime is MyBatis3. If used with iBATIS2 target runtimes it will produce code with runtime errors.

Optional Attributes

Attribute Description
identity If true, then the column is flagged as an identity column and the generated <selectKey> element will be placed after the insert (for an identity column). If false, then the generated <selectKey> will be placed before the insert (typically for a sequence).

Important: Even if you specify the type attribute as "post", you should still specify this value as "true" for identity columns. This will flag MBG to remove the column from the insert list.

The default is false.

type If specified, then this value will be added as the type of the generated <selectKey> element. The value of this property should be either "pre" or "post".

Important: if a value is specified for this attribute, then the generated <selectKey> element will always be placed before the insert statement.

Child Elements

None