Mybatis批量插入并返回主鍵id的方法
場(chǎng)景
在做商城的時(shí)候,sku表進(jìn)行了拆分,sku的基本信息以及sku的庫(kù)存表。因?yàn)閹?kù)存會(huì)經(jīng)常的變動(dòng),會(huì)導(dǎo)致行鎖。
這里就是新增的時(shí)候,因?yàn)樵谛略錾唐返臅r(shí)候,會(huì)有多條sku的數(shù)據(jù)進(jìn)行批量的插入,那么有批量插入sku基本信息以及批量插入sku的庫(kù)存信息。
其中,就需要批量插入sku的基本信息的時(shí)候,返回主鍵id,這就能夠在sku批量插入庫(kù)存信息的時(shí)候能夠插入skuId;
錯(cuò)誤
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ù)庫(kù)是否支持自動(dòng)生成密鑰字段(例如MySQL和SQL Server),那么就只需設(shè)置useGeneratedKeys=“true” 并將 keyProperty設(shè)置為Java對(duì)象的屬性名,keyColumn是數(shù)據(jù)庫(kù)中的列名(當(dāng)主鍵列不是表中的第一列的時(shí)候,它必須設(shè)置的) 。
- 傳參有多個(gè)個(gè)參數(shù),mybatis并不知道keyProperty = "id"中的 id 賦值給誰(shuí)
- (我就是這里出錯(cuò))
- 我看其他的博客還有說是版本的問題,建議3.3.1以上的。
排查問題
數(shù)據(jù)庫(kù)是MySQL,設(shè)置了 useGeneratedKeys=“true” ,且 keyProperty = id是Java對(duì)象的屬性名,id是主鍵列且在第一列中

就是這里出錯(cuò),keyProperty=“id”,導(dǎo)致不知道id返回到哪一個(gè)參數(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>
進(jìn)行修改:
<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis+MySQL 返回插入的主鍵ID的方法
- Mybatis批量插入返回插入成功后的主鍵id操作
- 詳解mybatis插入數(shù)據(jù)后返回自增主鍵ID的問題
- mybatis新增save結(jié)束后自動(dòng)返回主鍵id詳解
- Mybatis執(zhí)行插入語(yǔ)句后并返回主鍵ID問題
- mybatis插入后返回主鍵id的3種方式圖解
- mybatis插入數(shù)據(jù)不返回主鍵id的可能原因及解決方式
- mybatis插入數(shù)據(jù)后返回自增主鍵ID的兩種實(shí)現(xiàn)方式
- Mybatis新增數(shù)據(jù)并返回主鍵id的兩種方法實(shí)現(xiàn)
相關(guān)文章
Spring Cloud Gateway自定義異常處理Exception Handler的方法小結(jié)
這篇文章主要介紹了Spring Cloud Gateway自定義異常處理Exception Handler的方法,本文通過兩種方法結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Java內(nèi)部類的實(shí)現(xiàn)原理與可能的內(nèi)存泄漏說明
這篇文章主要介紹了Java內(nèi)部類的實(shí)現(xiàn)原理與可能的內(nèi)存泄漏說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
簡(jiǎn)單實(shí)現(xiàn)java數(shù)獨(dú)游戲
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)java數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java實(shí)戰(zhàn)之在線租房系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了利用Java實(shí)現(xiàn)的在線租房系統(tǒng),文中用到了SpringBoot、Redis、MySQL、Vue等技術(shù),文中示例代碼講解詳細(xì),需要的可以參考一下2022-02-02

