服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Mybatis批量修改联合主键数据的两种方法

Mybatis批量修改联合主键数据的两种方法

2022-11-10 11:44猪思马记 Java教程

最近遇上需要批量修改有联合主键的表数据,找很多资料都不是太合适,最终自己摸索总结了两种方式可以批量修改数据,对Mybatis批量修改数据相关知识感兴趣的朋友一起看看吧

最近遇上需要批量修改有联合主键的表数据,网上找了很多文章,最终都没找到比较合适的方法,有些只能支持少量数据批量修改,超过十几条就不行了。

最终自己摸索总结了两种方式可以批量修改数据。

第一种:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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>

直接结果集来两个in查询,最终可以满足。

第二种:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<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里面进行对比判断。

到此这篇关于Mybatis批量修改联合主键数据的两种方法的文章就介绍到这了,更多相关Mybatis批量修改数据内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/m-home/p/16130028.html

延伸 · 阅读

精彩推荐