Skip to content

MyBatis useGeneratedKeys configuration

useGeneratedKeys is an attribute used within MyBatis configuration (typically in the <insert> statement) to instruct the database to return any auto-generated keys, such as auto-incremented IDs, resulting from the update operation.^[001-todo-test.md]

Configuration

To enable this behavior, the attribute must be explicitly set to true.^[001-todo-test.md] It is frequently paired with the keyProperty attribute, which specifies the target property in the Java object where the generated value should be mapped (e.g., keyProperty="id").^[001-todo-test.md]

Example

The following snippet demonstrates the configuration within an SQL mapping context:

<insert id="insert" useGeneratedKeys="true" keyProperty="id">
  ...
</insert>
^[001-todo-test.md]

  • [[MyBatis]]
  • [[Database]]
  • [[SQL]]

Sources

^[001-todo-test.md]