Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法
最近遇上需要批量修改有聯(lián)合主鍵的表數(shù)據(jù),網(wǎng)上找了很多文章,最終都沒(méi)找到比較合適的方法,有些只能支持少量數(shù)據(jù)批量修改,超過(guò)十幾條就不行了。
最終自己摸索總結(jié)了兩種方式可以批量修改數(shù)據(jù)。
第一種:
<update id="updateMoreEmpOrg" parameterType="java.util.List">
update hr_emp_org
<trim prefix="set" suffixOverrides=",">
<trim prefix="ISMAN = CASE EMPID" suffix="end,">
<foreach collection="empOrgList" item="item" index="index">
<if test="item.isman != null">
when EMPID = #{item.empid} then #{item.isman}
</if>
</foreach>
</trim>
<trim prefix="UPDATETIME = CASE EMPID" suffix="end,">
<foreach collection="empOrgList" item="item" index="index">
<if test="item.updatetime != null">
when EMPID = #{item.empid} then #{item.updatetime}
</if>
</foreach>
</trim>
<trim prefix="hr_status =case EMPID" suffix="end,">
<foreach collection="empOrgList" item="item" index="index">
<if test="item.hrStatus != null">
when #{item.EMPID} then #{item.hrStatus}
</if>
</foreach>
</trim>
</trim>
where
EMPID in
<foreach collection="empOrgList" item="item" open="(" separator="," close=")">
#{item.empid}
</foreach>
and ORGID in
<foreach collection="empOrgList" item="item" open="(" separator="," close=")">
#{item.orgid}
</foreach>
</update>直接結(jié)果集來(lái)兩個(gè)in查詢,最終可以滿足。
第二種:
<update id="updateMoreEmpPosition" parameterType="java.util.List">
update hr_emp_position
<trim prefix="set" suffixOverrides=",">
<trim prefix="ISMAN =case" suffix="end,">
<foreach collection="empPositionList" item="item" index="index">
<if test="item.isman != null">
when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.isman}
</if>
</foreach>
</trim>
<trim prefix="CREATETIME =case" suffix="end,">
<foreach collection="empPositionList" item="item" index="index">
<if test="item.createtime != null">
when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.createtime}
</if>
</foreach>
</trim>
<trim prefix="UPDATETIME =case" suffix="end,">
<foreach collection="empPositionList" item="item" index="index">
<if test="item.updatetime != null">
when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.updatetime}
</if>
</foreach>
</trim>
<trim prefix="hr_status =case" suffix="end,">
<foreach collection="empPositionList" item="item" index="index">
<if test="item.hrStatus != null">
when EMPID = #{item.empid} and POSITIONID = #{item.positionid} then #{item.hrStatus}
</if>
</foreach>
</trim>
</trim>
where
EMPID in
<foreach collection="empPositionList" item="item" open="(" separator="," close=")">
#{item.empid}
</foreach>
</update>修改條件中trim里面 case后面不填對(duì)比字段,在if里面進(jìn)行對(duì)比判斷。
到此這篇關(guān)于Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法的文章就介紹到這了,更多相關(guān)Mybatis批量修改數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)冒泡排序簡(jiǎn)單示例
冒泡排序(Bubble Sort)是一種簡(jiǎn)單的排序算法,它重復(fù)地走訪過(guò)要排序的數(shù)列,一次比較兩個(gè)元素,如果他們的順序錯(cuò)誤就把他們交換過(guò)來(lái),下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)冒泡排序的相關(guān)資料,需要的朋友可以參考下2023-06-06
教你用java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)(附詳細(xì)代碼)
教學(xué)管理系統(tǒng)很適合初學(xué)者對(duì)于所學(xué)語(yǔ)言的練習(xí),下面這篇文章主要給大家介紹了關(guān)于如何用java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)的相關(guān)資料,文中給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2023-06-06
使用SpringBoot發(fā)送郵箱驗(yàn)證碼的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了使用SpringBoot發(fā)送郵箱驗(yàn)證碼的簡(jiǎn)單實(shí)現(xiàn),咱們今天來(lái)講使用QQ郵箱來(lái)發(fā)送和接收驗(yàn)證碼,首先來(lái)介紹一下它在SpringBoot項(xiàng)目中的具體應(yīng)用,需要的朋友可以參考下2023-04-04
Spring boot2X Consul如何通過(guò)RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用
這篇文章主要介紹了spring boot2X Consul如何通過(guò)RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
深入淺析java web log4j 配置及在web項(xiàng)目中配置Log4j的技巧
這篇文章主要介紹了2015-11-11
Springboot之idea之pom文件圖標(biāo)不對(duì)問(wèn)題
這篇文章主要介紹了Springboot之idea之pom文件圖標(biāo)不對(duì)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04

