MyBatis 3.2.6插入時候獲取自增主鍵方法有二ide
以MySQL5.5爲例:it
方法1:class
<insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
insert into person(name,pswd) values(#{name},#{pswd})
</insert>select
方法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>di
插入前實體id屬性爲0;view
插入後實體id屬性爲保存後自增的id;vi