日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

你学废了 Mybatis 动态批量修改吗?

發(fā)布時(shí)間:2025/3/19 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 你学废了 Mybatis 动态批量修改吗? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


封面:學(xué)校夜景
xdm,祝大家節(jié)日快樂(lè)!!👨?💻

今天聽(tīng)《路過(guò)人間》演唱會(huì)Live限定版,愛(ài)上了一句歌詞。

說(shuō)來(lái)慚愧,人對(duì)愛(ài)只學(xué)會(huì),視死如歸。

1.業(yè)務(wù)需求

如下:

前臺(tái)傳給我一個(gè) documentId和List<UpdateDocumentAnswer> 對(duì)象給我。

執(zhí)行條件:通過(guò)這個(gè)documentId和List<UpdateDocumentAnswer>中對(duì)UpdateDocumentAnswer.id,修改document_answer表的數(shù)據(jù)。

簡(jiǎn)單說(shuō):就是希望通過(guò)一條update語(yǔ)句,根據(jù)不同的條件改變多條需要改變的數(shù)據(jù)。

思考一:

我們先按照我們最簡(jiǎn)單的思維思考:

即拆成一句一句執(zhí)行,for循環(huán)執(zhí)行多條 update 語(yǔ)句。

update document_answer set where document_id=#{documentId} and id=#{answer.id}

這樣寫(xiě)的話,我們的mapper層接收的參數(shù),應(yīng)該為:

int patchByDocumentId(@Param("documentId") Long documentId,@Param("answers") UpdateDocumentAnswer answers);

實(shí)現(xiàn)是可以實(shí)現(xiàn)的,但是因?yàn)樾枰獔?zhí)行多條update語(yǔ)句,效率是真的不敢恭維。

如果大家有嘗試過(guò),都會(huì)知道,for循環(huán)執(zhí)行sql語(yǔ)句是真的要不得的。一條普通的sql,我們都要優(yōu)化完再優(yōu)化,更別說(shuō)一個(gè)方法要執(zhí)行多條sql語(yǔ)句了。

所有就啥勒??

推薦大家使用 百度、Bing、Google進(jìn)行搜索👨?💻

我們想到過(guò)這種問(wèn)題,大概率別人也會(huì)遇上的,搜一搜,確實(shí)有答案低。

所以我們接著進(jìn)入思考二吧。🏍

思考二:

還記得文章前面所說(shuō):就是希望通過(guò)一條update語(yǔ)句,根據(jù)不同的條件改變多條需要改變的數(shù)據(jù)。

我們直接 搜怎么一條update用不同條件修改多條數(shù)據(jù)勒

就是會(huì)搜到一個(gè)下面的這樣的sql語(yǔ)句。

update 表名 set1=casewhen 條件1 then1when 條件2 then2 end,2=casewhen 條件1 then1when 條件2 then2 end, where 條件

說(shuō)實(shí)話,看到這條語(yǔ)句的那一刻,感覺(jué)自己又沒(méi)有學(xué)過(guò)mysql了,連crud工程師都算不上(捂臉)。

解釋:

我們要 修改列1, 當(dāng)when 條件1 滿足時(shí),則將 列1 修改為 then 后面跟著的 值1,when 條件2 滿足,則將列1修改為then 后面跟著的值2。

這樣一樣,我們就可以執(zhí)行多條語(yǔ)句了啊。

2.實(shí)現(xiàn)

我們將之前的mapper層的接口傳入的參數(shù)做一下更改。

int patchByDocumentId(@Param("documentId") Long documentId,@Param("answers") List<UpdateDocumentAnswer> answers);

mapper.xml的實(shí)現(xiàn)如下:

<update id="patchByDocumentId">update document_answer<set><trim prefix="template_question_id = case" suffix="end,"><foreach collection="answers" item="answer"><if test="answer.templateQuestionId != null">when id=#{answer.id} then #{answer.templateQuestionId}</if></foreach></trim><trim prefix="answer = case" suffix="end,"><foreach collection="answers" item="answer"><if test="answer.answer != null">when id=#{answer.id} then #{answer.answer}</if></foreach></trim><trim prefix="comments = case" suffix="end,"><foreach collection="answers" item="answer"><if test="answer.comments != null">when id=#{answer.id} then #{answer.comments}</if></foreach></trim></set><where>document_id=#{documentId}<if test="answers != null">and id in<foreach collection="answers" separator="," item="answer" open="(" close=")">#{answer.id}</foreach></if></where> </update>

生成的sql日志

update document_answer SET template_question_id = case when id=? then ? when id=? then ? end, answer = case when id=? then ? when id=? then ? end, comments = case when id=? then ? when id=? then ? end WHERE document_id=? and id in ( ? , ? )

換上我們想要的值:

update document_answer SET template_question_id = case when id=1 then 2 when id=1 then 3 end, answer = case when id=1 then '內(nèi)容1' when id=2 then '內(nèi)容2' end, comments = case when id=1 then '評(píng)論1' when id=2 then '評(píng)論2' end WHERE document_id=2 and id in ( 1 , 2 )

執(zhí)行規(guī)則: 上面這種方式,更新的記錄的數(shù)量取決于list集合的數(shù)量,且每條記錄中的值和對(duì)應(yīng)記錄的ID是一一對(duì)應(yīng)的。

結(jié)束了,周日更文一篇。

后語(yǔ)

我們一起加油吧

你好,我是博主寧在春:主頁(yè)

希望本篇文章能讓你感到有所收獲!!!

祝 我們:待別日相見(jiàn)時(shí),都已有所成。

歡迎大家一起討論問(wèn)題😁,躺了🛌

總結(jié)

以上是生活随笔為你收集整理的你学废了 Mybatis 动态批量修改吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。