Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法
最近遇上需要批量修改有聯(lián)合主鍵的表數(shù)據(jù),網(wǎng)上找了很多文章,最終都沒找到比較合適的方法,有些只能支持少量數(shù)據(jù)批量修改,超過十幾條就不行了。
最終自己摸索總結(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é)果集來兩個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后面不填對比字段,在if里面進行對比判斷。
到此這篇關(guān)于Mybatis批量修改聯(lián)合主鍵數(shù)據(jù)的兩種方法的文章就介紹到這了,更多相關(guān)Mybatis批量修改數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Web程序?qū)崿F(xiàn)返回JSON字符串的方法總結(jié)
Java Web服務器端只要把Java對象數(shù)據(jù)轉(zhuǎn)成JSON字符串,把JSON字符串以文本的形式通過response輸出即可,2016-05-05
Springboot中如何使用過濾器校驗PSOT類型請求參數(shù)內(nèi)容

