MyBatis the Insert Element property in detail and the value of the primary key ID for inserting data in insert

Source: Internet
Author: User

The properties are as follows:

ParameterType, fully qualified class name or type alias for incoming arguments

KeyColumn, sets the primary key name that is automatically generated by the data table. For a specific database (such as PostgreSQL), if the auto-generated primary key is not the first field, you must set a

Keyproperty, default value unset, for setting Getgeneratedkeys method or Selectkey child element return value will be assigned to which property of the domain model

Usegeneratedkeys, the value range True|false (the default), sets whether to use the JDBC Getgenereatedkeys method to get the primary key and assign the value to the domain model properties of the Keyproperty settings. MySQL and SQL Server execute auto-generated key field, so when the database is set to self-grow primary key, it can be obtained by the JDBC Getgeneratedkeys method. However, databases such as oralce that do not support auto-generated key field cannot get the primary key in this way.

StatementType, Value range statement,prepared (default), callable

Flushcache, the value range True (default) |false, sets whether the two-level cache and the local cache will be emptied after the operation is performed

Timeout, which defaults to unset (depends on the settings of the JDBC Drive), sets the maximum time period to perform the operation, and the timeout throws an exception

DatabaseId, value range Oracle|mysql, etc., representing the database manufacturers, the elements can be "<if test=" _databaseid = ' Oracle ' ">" to specify a different SQL statement for a particular database


Selectkey Sub-elements

Action: Inserts a query statement into the INSERT element and the update element.

The properties are as follows:

Keyproperty, default value unset, for setting Getgeneratedkeys method or Selectkey child element return value will be assigned to which property of the domain model

Resulttype, the fully qualified class name or type alias of the attribute class that the keypropety points to

Order property, Value range before| After, specifies whether to perform the selectkey operation before or after the INSERT statement

StatementType, Value range statement,prepared (default), callable

Note: The Selectkey operation assigns the result of the action query to the corresponding attribute in the ParameterType instance of the Insert element. and supplied to the INSERT statement using the

------------------------------------------------------------------------------------

MyBatis Returns the configuration of the primary key ID when inserting an insert operation

Many times, when inserting data into a database, you need to retain the ID of the inserted data for subsequent update operations or to save the ID to another table as a foreign key.

However, by default, the insert operation returns an int value and does not represent the primary key ID, but instead represents the number of rows affected by the current SQL statement ...

Next, let's see how MyBatis binds the returned ID to the object when using MySQL and Oracle for insert inserts.

MySQL usage:

<insert id= "Insert" parametertype= "Com.test.User"  keyproperty= "UserId" usegeneratedkeys= "true" >

In the above configuration, "Keyproperty" means that the returned ID is to be saved to that property of the object, and "Usegeneratedkeys" indicates that the primary key ID is self-growing mode.

MySQL to do the above configuration is OK, more simple, no longer repeat.

Oracle Usage:

<insert id= "Insert" parametertype= "Com.test.User" >
   <selectkey resulttype= "INTEGER" order= "before" keyproperty= "UserId" >  
       select Seq_user. Nextval as UserId from DUAL
   </selectKey> 
    Insert to User (USER_ID, user_name, Modified, state)
    values (#{userid,jdbctype=integer}, #{username,jdbctype=varchar},  #{modified,jdbctype=timestamp}, #{state,jdbcType= INTEGER})
</insert>

In Oracle usage, it is important to note that the "Usegeneratedkeys" attribute cannot be used because Oracle has no self-growth, only the form of the sequence that mimics itself.

Instead, use the <selectKey> to get and assign the ID to the object's properties, insert the ID as normal when inserting the insert operation.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.