mybatis insert is to return the primary key id

<!-- Insert data: return the id value of the record -->

<insertid="insertOneTest"parameterType="org.chench.test.mybatis.model.Test"useGeneratedKeys="true"keyProperty="id"keyColumn="id">

insert into test(name,descr,url,create_time,update_time) values(#{name},#{descr},#{url},now(),now())

</insert>

 

Detailed explanation
First of all, in order to get the record primary key id when adding a record, you must <insert>add 3 attributes to the xml configuration:

<insert useGeneratedKeys="true" keyProperty="id" keyColumn="id"></insert>

useGeneratedKeys: must be set to true, otherwise the primary key id cannot be obtained.
keyProperty: Set to the name of the primary key id property of the POJO object.
keyColumn: Set to the primary key id field name of the database record.

Secondly, the newly added primary key id is not directly returned when the adding operation is performed, but after the adding operation is performed, the primary key id field of the newly added record is set as the primary key id property of the POJO object.
It can be returned by accessing the primary key id property of the POJO object.

 

Reprinted from: https://www.cnblogs.com/nuccich/p/7687281.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324887860&siteId=291194637