Mybatis批量插入并返回主鍵id的方法
場景
在做商城的時(shí)候,sku表進(jìn)行了拆分,sku的基本信息以及sku的庫存表。因?yàn)閹齑鏁?jīng)常的變動(dòng),會導(dǎo)致行鎖。
這里就是新增的時(shí)候,因?yàn)樵谛略錾唐返臅r(shí)候,會有多條sku的數(shù)據(jù)進(jìn)行批量的插入,那么有批量插入sku基本信息以及批量插入sku的庫存信息。
其中,就需要批量插入sku的基本信息的時(shí)候,返回主鍵id,這就能夠在sku批量插入庫存信息的時(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ù)庫是否支持自動(dòng)生成密鑰字段(例如MySQL和SQL Server),那么就只需設(shè)置useGeneratedKeys=“true” 并將 keyProperty設(shè)置為Java對象的屬性名,keyColumn是數(shù)據(jù)庫中的列名(當(dāng)主鍵列不是表中的第一列的時(shí)候,它必須設(shè)置的) 。
- 傳參有多個(gè)個(gè)參數(shù),mybatis并不知道keyProperty = "id"中的 id 賦值給誰
- (我就是這里出錯(cuò))
- 我看其他的博客還有說是版本的問題,建議3.3.1以上的。
排查問題
數(shù)據(jù)庫是MySQL,設(shè)置了 useGeneratedKeys=“true” ,且 keyProperty = id是Java對象的屬性名,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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis+MySQL 返回插入的主鍵ID的方法
- Mybatis批量插入返回插入成功后的主鍵id操作
- 詳解mybatis插入數(shù)據(jù)后返回自增主鍵ID的問題
- mybatis新增save結(jié)束后自動(dòng)返回主鍵id詳解
- Mybatis執(zhí)行插入語句后并返回主鍵ID問題
- mybatis插入后返回主鍵id的3種方式圖解
- mybatis插入數(shù)據(jù)不返回主鍵id的可能原因及解決方式
- mybatis插入數(shù)據(jù)后返回自增主鍵ID的兩種實(shí)現(xiàn)方式
- Mybatis新增數(shù)據(jù)并返回主鍵id的兩種方法實(shí)現(xiàn)
相關(guān)文章
SpringBoot簡單使用SpringData的jdbc和durid
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識,文章圍繞著SpringBoot簡單使用SpringData的jdbc和durid,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06java線程并發(fā)blockingqueue類使用示例
BlockingQueue是一種特殊的Queue,若BlockingQueue是空的,從BlockingQueue取東西的操作將會被阻斷進(jìn)入等待狀態(tài)直到BlocingkQueue進(jìn)了新貨才會被喚醒,下面是用BlockingQueue來實(shí)現(xiàn)Producer和Consumer的例子2014-01-01Spring Security實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄"記住我"功能
登錄過程中經(jīng)常使用的“記住我”功能,也就是我們經(jīng)常會在各種網(wǎng)站登陸時(shí)見到的"兩周內(nèi)免登錄",“三天內(nèi)免登錄”的功能。今天小編給大家分享基于Spring Security實(shí)現(xiàn)兩周內(nèi)自動(dòng)登錄"記住我"功能,感興趣的朋友一起看看吧2019-11-11使用Java生成32位16進(jìn)制密鑰的代碼實(shí)現(xiàn)
在許多加密和安全應(yīng)用中,生成隨機(jī)的密鑰是至關(guān)重要的一步,密鑰通常以16進(jìn)制形式表示,并且具有特定的長度,在這篇博客中,我們將探討如何使用Java生成一個(gè)32位長度的16進(jìn)制密鑰,并展示詳細(xì)的代碼示例和運(yùn)行結(jié)果,需要的朋友可以參考下2024-08-08在SpringBoot當(dāng)中使用Thymeleaf視圖解析器的詳細(xì)教程
Thymeleaf是一款開源的模板引擎,它允許前端開發(fā)者使用HTML與XML編寫動(dòng)態(tài)網(wǎng)頁,hymeleaf的主要特點(diǎn)是將表達(dá)式語言嵌入到HTML結(jié)構(gòu)中,它支持Spring框架,使得在Spring MVC應(yīng)用中集成非常方便,本文給大家介紹了在SpringBoot當(dāng)中使用Thymeleaf視圖解析器的詳細(xì)教程2024-09-09JAVA基于靜態(tài)數(shù)組實(shí)現(xiàn)棧的基本原理與用法詳解
這篇文章主要介紹了JAVA基于靜態(tài)數(shù)組實(shí)現(xiàn)棧的基本原理與用法,結(jié)合實(shí)例形式詳細(xì)分析了JAVA基于靜態(tài)數(shù)組實(shí)現(xiàn)棧相關(guān)原理、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03