Gets the self incrementing primary key method when MyBatis inserts

  • 2020-05-12 02:39:41
  • OfStack

MyBatis 3.2.6 there are two ways to get a self-incrementing primary key at insertion. Let's take MySQL5.5 as an example and introduce the way mybatis gets the self-adducing primary key in two ways. Let's take a look at it.

Take MySQL 5.5 as an example:

Method 1:


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

Method 2:


<insert id="insert" parameterType="Person">
<selectKey keyProperty="id" resultType="long">
select LAST_INSERT_ID()
</selectKey>
insert into person(name,pswd) values(#{name},#{pswd})
</insert>

The id property of the entity before insertion is 0;

The property of id after insertion is id after saving.


Related articles: