MyBatis 3.2.6插入時候獲取自增主鍵方法有二(第一種方法親測可行,第二種方法測試沒有通過,有測試通過的小夥伴可以告知一下啦,謝謝咯!!!)
以MySQL5.5爲例:
方法1:
<insert id="insert" parameterType="Person">
<selectKey keyProperty="id" resultType="long">
select LAST_INSERT_ID()
</selectKey>
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
方法2:
<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
insert into person(name,pswd) values(#{name},#{pswd})
</insert>
插入前實體id屬性爲0;
插入後實體id屬性爲保存後自增的id;