There are two ways to obtain the auto-increment primary key when inserting MyBatis 3.2.6. Let’s take MySQL5.5 as an example to introduce to you two methods to obtain the auto-increment primary key in mybatis. Let’s take a look.
Take MySQL5.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 entity id attribute before insertion is 0;
The entity id attribute after insertion is the id that is automatically increased after saving;
The above is the method of obtaining the self-added primary key when inserting MyBatis, which I introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!