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 ().