国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Mybatis批量插入返回插入成功后的主鍵id操作

瀏覽:4日期:2023-10-19 15:21:06

我們都知道Mybatis在插入單條數據的時候有兩種方式返回自增主鍵:

1、對于支持生成自增主鍵的數據庫:增加 useGenerateKeys和keyProperty ,<insert>標簽屬性。

2、不支持生成自增主鍵的數據庫:使用<selectKey>。

但是怎么對批量插入數據返回自增主鍵的解決方式網上看到的還是比較少,至少百度的結果比較少。

Mybatis官網資料提供如下:

First, if your database supports auto-generated key fields (e.g. MySQL and SQL Server), then you can simply set useGeneratedKeys='true' and set the keyProperty to the target property and you’re done. For example, if the Authortable above had used an auto-generated column type for the id, the statement would be modified as follows:

<insert useGeneratedKeys='true' keyProperty='id'> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})</insert> useGeneratedKeys='true' keyProperty='id'> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})</insert>

If your database also supports multi-row insert, you can pass a list or an array of Authors and retrieve the auto-generated keys.

<insert useGeneratedKeys='true' keyProperty='id'> insert into Author (username, password, email, bio) values <foreach item='item' collection='list' separator=','> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach></insert> useGeneratedKeys='true' keyProperty='id'> insert into Author (username, password, email, bio) values <foreach item='item' collection='list' separator=','> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach></insert>

從官網資料可以看出Mybatis是支持批量插入時返回自增主鍵的。

但是在本地測試的時候使用上述方式確實不能返回自增id,而且還報錯(不認識keyProperty中指定的Id屬性),然后在網上找相關資料。終于在Stackoverflow上面找到了一些信息。

解決辦法:

1、升級Mybatis版本到3.3.1。官方在這個版本中加入了批量新增返回主鍵id的功能

2、在Dao中不能使用@param注解。

3、Mapper.xml中使用list變量(parameterType='java.util.List')接受Dao中的參數集合。

下面是具體代碼過程,可供參考

mapper.xml層代碼

<!-- 批量新增 --> <insert parameterType='java.util.List' useGeneratedKeys='true' keyProperty='id' > INSERT INTO <include refid='t_shop_resource' /> (relation_id, summary_id, relation_type) VALUES <foreach collection='list' index='index' item='shopResource' separator=','> ( #{shopResource.relationId}, #{shopResource.summaryId}, #{shopResource.relationType} ) </foreach> </insert>

dao實現層代碼

public List<ShopResource> batchinsertCallId(List<ShopResource> shopResourceList) { this.getSqlSession().insert(getStatement(SQL_BATCH_INSERT_CALL_ID), shopResourceList); return shopResourceList;// 重點介紹 }

補充:MyBatis 插入的同時獲取主鍵id

有時候進行一些多步操作的時候就需要得到最新插入一條記錄的id號,那么如何在插入的同時返回id號

Mapper代碼:

<insert parameterType='com.yj.pojo.Feeds' useGeneratedKeys='true' keyProperty='id'> insert into feeds (id, title, content, pic, video, auther_id, comments, favours, likes, cover_select, views, set_time, kind) values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{pic,jdbcType=VARCHAR}, #{video,jdbcType=VARCHAR}, #{autherId,jdbcType=VARCHAR}, #{comments,jdbcType=INTEGER}, #{favours,jdbcType=INTEGER}, #{likes,jdbcType=INTEGER}, #{coverSelect,jdbcType=INTEGER}, #{views,jdbcType=INTEGER}, #{setTime,jdbcType=VARCHAR}, #{kind,jdbcType=VARCHAR}) </insert>service層:

當設置了useGeneratedKeys='true' keyProperty='id'后,它會在你插入數據庫的同時,將這個對象的id值改為最新的那個id,然后我們只需要取出他就可以了

Mybatis批量插入返回插入成功后的主鍵id操作

最終效果:

數據庫

Mybatis批量插入返回插入成功后的主鍵id操作

輸出

Mybatis批量插入返回插入成功后的主鍵id操作

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 日本人一级毛片免费视频 | 国内精品久久久久久久久蜜桃 | 日韩免费在线 | 国产在线欧美精品 | 亚洲成人精品久久 | 免费黄色三级网站 | 成人性动漫高清免费观看网址 | 日韩中文字幕在线亚洲一区 | 天天精品在线 | 国内xxxx乱子另类 | 国产精品国产 | 黄频免费影院 | 欧美高清视频手机在在线 | 国内外成人免费视频 | 欧美日韩国产一区二区三区播放 | 国产乱码精品一区二区三上 | 亚洲一区二区三区成人 | 国产男女视频在线观看 | 国产精品高清在线 | 三级中文字幕永久在线视频 | 精品国产一区二区三区不卡 | 亚洲视频中文字幕在线 | 狼人久久尹人香蕉尹人 | 日本三级2021最新理论在线观看 | 色综合视频一区二区观看 | 玖玖精品在线观看 | 国产成人精品一区二区视频 | 日韩美一区二区 | 日韩免费精品一级毛片 | 久久久久久网址 | 欧美大屁股精品毛片视频 | 国产无毛 | 一级一级毛片免费播放 | 亚洲免费视频一区二区三区 | a级片免费| 欧美 自拍| 国产精品亚洲视频 | 日韩精品在线一区 | 国产女乱淫真高清免费视频 | 欧美8888| 99久久99热精品免费观看国产 |