Mybatis 插入數據並返回剛剛插入的數據id


1.在Mybatis Mapper文件中添加屬性“useGeneratedKeys”和“keyProperty”,其中keyProperty是Java對象的屬性名,而不是表格的字段名

2.Mybatis執行完插入語句后,自動將自增長值賦值給對象systemBean的屬性id。因此,可通過systemBean對應的getter方法獲取!

示列

<insert id="insert" parameterType="Merchant.model.BaoShopPic" useGeneratedKeys="true" keyProperty="picId" >
insert into bao_shop_pic
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="picId != null">
pic_id,
</if>
<if test="shopId != null">
shop_id,
</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="picId != null">
#{picId},
</if>
<if test="shopId != null">
#{shopId},
</if>
</trim>
</insert>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM