MyBatis returns the primary key value of the record that was just inserted when inserting

Source: Internet
Author: User
XML code    <insert id= "Insert"  parametertype= "Cn.***.beans. Logobject " >       <selectkey resulttype=" Java.lang.Integer "  order= "before"  keyproperty= "id" >            select logs_seq.nextval as id from dual       </ selectkey>       INSERT INTO S_T_LOGS  (            ID,            user_ id,            USER_NAME,             USER_IP,             operation_time,           DESCRIPTION,            resource_id)         VALUES  (            #{id},            #{userId},             #{userName},             #{userIp},            #{ operationtime},           #{description},            #{resourceid})    </insert>  

The Orcale. The ID field in the Logobject object that is passed in is assigned a value of sequence. This achieves the effect of returning the primary key value of the record you just inserted.

  XML code    <insert id= "Insert"  parametertype= "Cn.***.beans. Logobject " >       <selectkey resulttype=" Java.lang.Integer "  order= "after"  keyproperty= "id" >              select last_insert_id ()  AS ID         </ selectkey>       INSERT INTO S_T_LOGS  (            ID,            user_ id,            USER_NAME,             USER_IP,             operation_time,           DESCRIPTION,            resource_id)         VALUES  (            #{id},            #{userid },            #{userName},             #{userIp},             #{operationTime},           #{description},             #{resourceid})    </insert>  

In MySQL. Get the value of the auto-growing ID just inserted by last_insert_id ().

Note: The value of the Order property in Selectkey is different, before first selects the primary key, sets the value of Keyproperty, and then executes the INSERT statement.

After is the INSERT statement executed first, and then the Selectkey is executed.

Turn from: http://530247683.iteye.com/blog/1162234;

Add another note to the individual:

The MyBatis Insert returns the number of rows affected, and the ID needs to be obtained with an insert (Object obj) parameter, such as Obj.getid ().

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.