MyBatis Insert entity return primary key

Source: Internet
Author: User
Tags bulk insert

Inserts the attribute detailed explanation

ParameterType, fully qualified class name or type alias of the argument

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

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

Usegeneratedkeys, True|false (the default), sets whether to use the Getgenereatedkeys method of JDBC to get the primary key and assign it to the domain model properties set by Keyproperty. MySQL and SQL Server execute the auto-generated key field, so when the database is set up from the growth primary key, it can be obtained through the JDBC Getgeneratedkeys method. But databases such as Oralce, which do not support the Auto-generated key field, cannot get the primary key in this way.

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

Flushcache, Value range True (default) |false, set whether the two level cache and local cache are emptied after the operation is performed

Timeout, defaults to unset (dependent on the JDBC drive settings), sets the maximum time limit for performing the operation, and the timeout throws an exception

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


Second, Mapper interface

Third, perform mapper.xml return primary key


Both approaches satisfy demand.

Note: The Mapper interface return value is still the number of records that were successfully inserted, but the difference is that the primary key value has been assigned to the ID of the domain model entity.



Iv. test Results


Six, BULK Insert


<insert id= "Add" parametertype= "Com.test.model.Course" >
  
Insert into Tb_course (Course_name, Teacher_name,
  Course_week, Course_time, place, 
  gmt_create, gmt_modify)
values (#{coursename,jdbctype=varchar}, #{teachername,jdbctype=varchar},
  #{courseweek,jdbctype=varchar} , #{coursetime,jdbctype=varchar}, #{place,jdbctype=varchar}, 
  #{gmtcreate,jdbctype=timestamp}, #{gmtModify, Jdbctype=timestamp})
</foreach></insert>

Vii. Summary

Two different ways
1, add Property usegeneratedkeys= "true" keyproperty= "id"

2, add code
<selectkey resulttype= "Java.lang.Short" order= "after" keyproperty= "id" >

SELECT last_insert_id () as ID

</selectKey>



It is recommended to use the first type.

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.