av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

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

1、對(duì)于支持生成自增主鍵的數(shù)據(jù)庫:增加 useGenerateKeys和keyProperty ,<insert>標(biāo)簽屬性。

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

但是怎么對(duì)批量插入數(shù)據(jù)返回自增主鍵的解決方式網(wǎng)上看到的還是比較少,至少百度的結(jié)果比較少。

Mybatis官網(wǎng)資料提供如下:

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>

從官網(wǎng)資料可以看出Mybatis是支持批量插入時(shí)返回自增主鍵的。

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

解決辦法:

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

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

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

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

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實(shí)現(xiàn)層代碼

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

補(bǔ)充:MyBatis 插入的同時(shí)獲取主鍵id

有時(shí)候進(jìn)行一些多步操作的時(shí)候就需要得到最新插入一條記錄的id號(hào),那么如何在插入的同時(shí)返回id號(hào)

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層:

當(dāng)設(shè)置了useGeneratedKeys='true' keyProperty='id'后,它會(huì)在你插入數(shù)據(jù)庫的同時(shí),將這個(gè)對(duì)象的id值改為最新的那個(gè)id,然后我們只需要取出他就可以了

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

最終效果:

數(shù)據(jù)庫

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

輸出

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 国产精品久久免费观看 | 在线精品一区二区 | 国产一区二区三区在线免费 | 羞羞视频一区二区 | 欧美一区二区三区在线 | 亚洲精品欧美 | 欧美激情亚洲 | 亚洲精品2 | 久久免费视频网 | 91视频大全 | 国产精品激情在线 | 精品久久一区 | 成人av影院 | 亚洲综合色自拍一区 | 国产精品日韩欧美一区二区三区 | 亚洲精品久久久一区二区三区 | 欧美a在线看 | 精品国产免费人成在线观看 | 亚洲一区二区电影网 | 美女国内精品自产拍在线播放 | 二区三区av | 9999精品视频 | 羞羞免费网站 | 91福利影院| 国产一区二区三区四区在线观看 | 国产欧美精品一区二区三区 | 日韩电影免费在线观看中文字幕 | www国产精| 欧美一区二区视频 | 在线观看成人 | 成人综合在线视频 | 国产一区三区视频 | 99久久国产免费 | 99久久精品国产一区二区三区 | 欧美日韩高清 | 日韩欧美国产成人一区二区 | 色视频网站免费 | 看av在线| 日韩午夜 | 欧美一区二区三区大片 | 欧美日一区 |