Mybatis批量插入并返回主鍵id的方法
場景
在做商城的時候,sku表進行了拆分,sku的基本信息以及sku的庫存表。因為庫存會經(jīng)常的變動,會導(dǎo)致行鎖。
這里就是新增的時候,因為在新增商品的時候,會有多條sku的數(shù)據(jù)進行批量的插入,那么有批量插入sku基本信息以及批量插入sku的庫存信息。
其中,就需要批量插入sku的基本信息的時候,返回主鍵id,這就能夠在sku批量插入庫存信息的時候能夠插入skuId;
錯誤
nested exception is org.apache.ibatis.executor.ExecutorException:
Error getting generated key or setting result to parameter object.
Cause: org.apache.ibatis.executor.ExecutorException: Could not determine which parameter to assign generated keys to.
Note that when there are multiple parameters, 'keyProperty' must include the parameter name (e.g. 'id'). Specified key properties are [id] and available parameters are [XXX, XXX, param1, param2]
分析原因
- 數(shù)據(jù)庫是否支持自動生成密鑰字段(例如MySQL和SQL Server),那么就只需設(shè)置useGeneratedKeys=“true” 并將 keyProperty設(shè)置為Java對象的屬性名,keyColumn是數(shù)據(jù)庫中的列名(當(dāng)主鍵列不是表中的第一列的時候,它必須設(shè)置的) 。
- 傳參有多個個參數(shù),mybatis并不知道keyProperty = "id"中的 id 賦值給誰
- (我就是這里出錯)
- 我看其他的博客還有說是版本的問題,建議3.3.1以上的。
排查問題
數(shù)據(jù)庫是MySQL,設(shè)置了 useGeneratedKeys=“true” ,且 keyProperty = id是Java對象的屬性名,id是主鍵列且在第一列中

就是這里出錯,keyProperty=“id”,導(dǎo)致不知道id返回到哪一個參數(shù)中
原來:
<insert id="insertBatch" useGeneratedKeys="true" keyProperty="id">
insert into goods_sku (goods_id,images,indexes,spec,price,size,bkge_scale,team_scale,direct_scale,enable,create_time,update_time) values
<foreach collection="param1" item="item" index="index" separator=",">
<if test="item != null">
(#{param2},#{item.images},#{item.indexes},#{item.spec},#{item.price},#{item.size},#{item.bkgeScale},#{item.teamScale},#{item.directScale},#{item.enable},#{param3},#{param3})
</if>
</foreach>
</insert>
進行修改:
<insert id="insertBatch" useGeneratedKeys="true" keyProperty="goodsSkuDTOs.id">
insert into goods_sku
(goods_id,images,indexes,spec,price,size,bkge_scale,team_scale,direct_scale,enable,create_time,update_time) values
<foreach collection="param1" item="item" index="index" separator=",">
<if test="item != null">
(#{param2},#{item.images},#{item.indexes},#{item.spec},#{item.price},#{item.size},#{item.bkgeScale},#{item.teamScale},#{item.directScale},#{item.enable},#{param3},#{param3})
</if>
</foreach>
</insert>
依賴版本:

附上完整的Mapper以及Xml文件
GoodsSkuMapper.java
int insertBatch(@Param("goodsSkuDTOs") List<GoodsSkuDTO> goodsSkuDTOs, @Param("goodsId") Long goodsId,@Param("date") Date date);
GoodsSkuMapper.xml
<insert id="insertBatch" useGeneratedKeys="true" keyProperty="goodsSkuDTOs.id">
insert into goods_sku (goods_id,images,indexes,spec,price,size,bkge_scale,team_scale,direct_scale,enable,create_time,update_time) values
<foreach collection="param1" item="item" index="index" separator=",">
<if test="item != null">
(#{param2},#{item.images},#{item.indexes},#{item.spec},#{item.price},#{item.size},#{item.bkgeScale},#{item.teamScale},#{item.directScale},#{item.enable},#{param3},#{param3})
</if>
</foreach>
</insert>
到此這篇關(guān)于Mybatis批量插入并返回主鍵id的方法的文章就介紹到這了,更多相關(guān)Mybatis批量插入返回主鍵內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot簡單使用SpringData的jdbc和durid
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著SpringBoot簡單使用SpringData的jdbc和durid,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06
java線程并發(fā)blockingqueue類使用示例
BlockingQueue是一種特殊的Queue,若BlockingQueue是空的,從BlockingQueue取東西的操作將會被阻斷進入等待狀態(tài)直到BlocingkQueue進了新貨才會被喚醒,下面是用BlockingQueue來實現(xiàn)Producer和Consumer的例子2014-01-01
Spring Security實現(xiàn)兩周內(nèi)自動登錄"記住我"功能
登錄過程中經(jīng)常使用的“記住我”功能,也就是我們經(jīng)常會在各種網(wǎng)站登陸時見到的"兩周內(nèi)免登錄",“三天內(nèi)免登錄”的功能。今天小編給大家分享基于Spring Security實現(xiàn)兩周內(nèi)自動登錄"記住我"功能,感興趣的朋友一起看看吧2019-11-11
在SpringBoot當(dāng)中使用Thymeleaf視圖解析器的詳細教程
Thymeleaf是一款開源的模板引擎,它允許前端開發(fā)者使用HTML與XML編寫動態(tài)網(wǎng)頁,hymeleaf的主要特點是將表達式語言嵌入到HTML結(jié)構(gòu)中,它支持Spring框架,使得在Spring MVC應(yīng)用中集成非常方便,本文給大家介紹了在SpringBoot當(dāng)中使用Thymeleaf視圖解析器的詳細教程2024-09-09
JAVA基于靜態(tài)數(shù)組實現(xiàn)棧的基本原理與用法詳解
這篇文章主要介紹了JAVA基于靜態(tài)數(shù)組實現(xiàn)棧的基本原理與用法,結(jié)合實例形式詳細分析了JAVA基于靜態(tài)數(shù)組實現(xiàn)棧相關(guān)原理、用法與操作注意事項,需要的朋友可以參考下2020-03-03

