mybatis mysql自增主鍵返回
阿新 • • 發佈:2019-01-22
對於自增主鍵的返回
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
<!-- selectKey實現將主鍵返回,將主鍵返回到user物件中
keyProperty:返回的主鍵儲存在pojo中的哪個屬性
order:selectKey的執行順序,是相對與insert語句來說,由於mysql的自增原理執行完insert語句之後才將主鍵生成,所以這裡selectKey的執行順序為after
resultType:返回的主鍵是什麼型別
LAST_INSERT_ID():是mysql的函式,返回auto_increment自增列新記錄id值。
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into user(username,birthday,sex,address)
values(#{username},#{birthday},#{sex},#{address});
</insert>