Mybatis 動態批量修改


封面學校夜景
xdm,祝大家節日快樂!!👨💻

今天聽《路過人間》演唱會Live限定版,愛上了一句歌詞。

說來慚愧,人對愛只學會,視死如歸。

1.業務需求

如下:

前台傳給我一個 documentIdList<UpdateDocumentAnswer> 對象給我。

執行條件:通過這個documentIdList<UpdateDocumentAnswer>中對UpdateDocumentAnswer.id,修改document_answer表的數據。

簡單說:就是希望通過一條update語句,根據不同的條件改變多條需要改變的數據。

image-20211024125850091

思考一:

我們先按照我們最簡單的思維思考:

即拆成一句一句執行,for循環執行多條 update 語句。

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

這樣寫的話,我們的mapper層接收的參數,應該為:

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

實現是可以實現的,但是因為需要執行多條update語句,效率是真的不敢恭維。

如果大家有嘗試過,都會知道,for循環執行sql語句是真的要不得的。一條普通的sql,我們都要優化完再優化,更別說一個方法要執行多條sql語句了。

所有就啥勒??

814268e3ly1fhlpr4j24rj20hs0840ts

推薦大家使用 百度、Bing、Google進行搜索👨💻

我們想到過這種問題,大概率別人也會遇上的,搜一搜,確實有答案低。

所以我們接着進入思考二吧。🏍

思考二:

還記得文章前面所說:就是希望通過一條update語句,根據不同的條件改變多條需要改變的數據。

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

就是會搜到一個下面的這樣的sql語句。

update 表名 set 
列1=
	case
		when 條件1 then 值1
		when 條件2 then 值2 end,
列2=
	case
		when 條件1 then 值1
		when 條件2 then 值2 end,
where 條件

說實話,看到這條語句的那一刻,感覺自己又沒有學過mysql了,連crud工程師都算不上(捂臉)。

解釋:

我們要 修改列1, 當when 條件1 滿足時,則將 列1 修改為 then 后面跟着的 值1,when 條件2 滿足,則將列1修改為then 后面跟着的值2。

這樣一樣,我們就可以執行多條語句了啊。

2.實現

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

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

mapper.xml的實現如下:

<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 '內容1' 
		when id=2 then '內容2' end, 
comments = 
	case 
		when id=1 then '評論1' 
		when id=2 then '評論2' end 
WHERE document_id=2 and id in ( 1 , 2 )

執行規則: 上面這種方式,更新的記錄的數量取決於list集合的數量,且每條記錄中的值和對應記錄的ID是一一對應的。

結束了,周日更文一篇。

后語

我們一起加油吧

你好,我是博主寧在春主頁

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

我們:待別日相見時,都已有所成

歡迎大家一起討論問題😁,躺了🛌


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM