mybatis使用case when按照條件進(jìn)行更新方式
mybatis使用case when按照條件更新
示例代碼
<update id="updateNrPurchaseOrderDetailByGoodsBarcode"> update nr_purchase_order_detail set stored_num = <foreach collection="barcodeList" item="item" index="index" separator=" " open="case goods_barcode" close="end"> when #{item.barcode} then stored_num + #{item.receiveNum} </foreach> ,unstored_num = <foreach collection="barcodeList" item="item" index="index" separator=" " open="case goods_barcode" close="end"> when #{item.barcode} then actual_goods_num - stored_num </foreach> where goods_barcode in <foreach collection="barcodeList" item="item" index="index" separator="," open="(" close=")"> #{item.barcode} </foreach> and parent_id = #{id} </update>
<!--caseWhen形式更新--> <update id="testCaseWhen" parameterType="java.util.Map"> UPDATE ${user} <set> <trim prefix="id= CASE id" suffix="END,"> <foreach collection="list" item="param"> <if test="param.id != null and id != ''"> WHEN #{param.id} THEN #{param.id} </if> </foreach> </trim> <trim prefix="username = CASE id" suffix="END,"> <foreach collection="list" item="param"> <if test="param.username != null and param.username != ''"> WHEN #{param.} THEN #{param.username} </if> </foreach> </trim> <trim prefix="password = CASE id" suffix="END,"> <foreach collection="list" item="param"> <if test="param.password != null and param.password != ''"> WHEN #{param.id} THEN #{param.password} </if> </foreach> </trim> </set> <where> id in <foreach collection="list" item="param" separator="," open="(" close=")"> #{param.id} </foreach> </where> </update>
說(shuō)明
上述第一個(gè)示例中,按照條碼進(jìn)行批量更新,將所有的條碼對(duì)應(yīng)的入庫(kù)數(shù)量和剩余數(shù)量進(jìn)行更新。
第二個(gè)示例中,又使用了set標(biāo)簽和trim標(biāo)簽,感覺(jué)更為復(fù)雜。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis控制臺(tái)打印SQL執(zhí)行信息的方法詳解
SQL性能監(jiān)控是一個(gè)程序必要的功能,通常我們可以使用數(shù)據(jù)庫(kù)自帶的客戶端工具進(jìn)行SQL性能分析,本章節(jié)只實(shí)現(xiàn)Mybatis執(zhí)行時(shí)對(duì)執(zhí)行SQL進(jìn)行攔截,控制臺(tái)打印執(zhí)行SQL包括參數(shù)、執(zhí)行方法以及執(zhí)行時(shí)間,需要的朋友可以參考下2024-11-11基于mybatis查詢結(jié)果映射不到對(duì)象的處理
這篇文章主要介紹了mybatis查詢結(jié)果映射不到對(duì)象的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08jmeter中json提取器如何提取多個(gè)參數(shù)值
關(guān)于jmeter中的正則表達(dá)式及json提取器可以提取響應(yīng)值,但是實(shí)際可以需要上個(gè)接口的多個(gè)響應(yīng)值,本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下2021-11-11Java基礎(chǔ)字符編碼與內(nèi)存流詳細(xì)解讀
這篇文章主要給大家介紹了關(guān)于Java中方法使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控的方法
這篇文章主要介紹了SpringBoot整合Spring?Boot?Admin實(shí)現(xiàn)服務(wù)監(jiān)控,內(nèi)容包括Server端服務(wù)開(kāi)發(fā),Client端服務(wù)開(kāi)發(fā)其中Spring Boot Admin還可以對(duì)其監(jiān)控的服務(wù)提供告警功能,如服務(wù)宕機(jī)時(shí),可以及時(shí)以郵件方式通知運(yùn)維人員,感興趣的朋友跟隨小編一起看看吧2022-03-03