Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Mybatis bulk inserts and returns the primary key id

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how Mybatis inserts and returns primary key id in batches". In daily operation, I believe many people have doubts about how Mybatis inserts and returns primary key id in batches. Xiaobian consulted all kinds of data and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how Mybatis inserts and returns primary key id in batches"! Next, please follow the small series to learn together!

scene

When doing the mall, sku table was split, sku basic information and sku inventory table. Because inventory changes frequently, it can lead to line locks.

This is when adding new products, because when adding new products, there will be multiple pieces of sku data inserted in batches, so there will be batch insertion of sku basic information and batch insertion of sku inventory information.

Among them, when it is necessary to insert the basic information of sku in batches, the primary key id is returned, so that skuId can be inserted when sku inserts inventory information in batches;

error

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]

analyze the reasons

If the database supports auto-generated key fields (e.g. MySQL and SQL Server), then simply set useGeneratedKeys="true" and keyProperty to the property name of the Java object, keyColumn is the column name in the database (it must be set when the primary key column is not the first column in the table).

There are multiple parameters in the parameter, mybatis does not know to whom the id in keyProperty = "id" is assigned

(I made a mistake here)

I read other blogs and said it was a version problem, suggesting 3.3.1 or above.

troubleshoot problems

Database is MySQL, useGeneratedKeys="true" is set, and keyProperty = id is the property name of the Java object, id is the primary key column and is in the first column

就是这里出错,keyProperty="id",导致不知道id返回到哪一个参数中

原来:

insert into goods_sku (goods_id,images,indexes,spec,price,size,bkge_scale,team_scale,direct_scale,enable,create_time,update_time) values (#{param2},#{item.images},#{item.indexes},#{item.spec},#{item.price},#{item.size},#{item.bkgeScale},#{item.teamScale},#{item.directScale},#{item.enable},#{param3},#{param3})

进行修改:

insert into goods_sku (goods_id,images,indexes,spec,price,size,bkge_scale,team_scale,direct_scale,enable,create_time,update_time) values (#{param2},#{item.images},#{item.indexes},#{item.spec},#{item.price},#{item.size},#{item.bkgeScale},#{item.teamScale},#{item.directScale},#{item.enable},#{param3},#{param3})

依赖版本:

附上完整的Mapper以及Xml文件

GoodsSkuMapper.java

int insertBatch(@Param("goodsSkuDTOs") List goodsSkuDTOs, @Param("goodsId") Long goodsId,@Param("date") Date date);

GoodsSkuMapper.xml

insert into goods_sku (goods_id,images,indexes,spec,price,size,bkge_scale,team_scale,direct_scale,enable,create_time,update_time) values (#{param2},#{item.images},#{item.indexes},#{item.spec},#{item.price},#{item.size},#{item.bkgeScale},#{item.teamScale},#{item.directScale},#{item.enable},#{param3},#{param3}) 到此,关于"Mybatis如何批量插入并返回主键id"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report