How to get the self-incrementing primary key when ibatis inserts

(1) The object is inserted as a parameter

 <insert id="insert" parameterType="Person">

         insert ignore into tb_person_${tbIndex} (uid,name )

         values(#{person.uid,jdbcType=INTEGER},#{person.name,jdbcType=VARCHAR})

</insert>

At this time, if there is an id field corresponding to the attribute in the table, after the insert action returns, the self-incrementing primary key will be written to the id at the same time.

 

(2) When the object is not used as the parameterType, the keyProperty attribute can be added

 <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
        insert into person(uid,name) values(#{uid},#{name})
    </insert>

 

(3) When the object is not used as the parameterType, use selectKey to get

<insert id="insert" parameterType="Person">

        <selectKey keyProperty="id" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        insert into person(uid,name) values(#{uid},#{name})

    </insert>

 

However, there is a problem with method (3). When there is a large amount of concurrency, if there are other middleware between the program and the DB, the result obtained by selectKey and insert may not be the same DBconnection, and the final id may not be the same. the id of the insert operation

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327037123&siteId=291194637