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

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

服务器之家 - 编程语言 - Java教程 - mybatis如何批量修改数据

mybatis如何批量修改数据

2022-09-20 20:22白雪茫茫 Java教程

这篇文章主要介绍了mybatis如何批量修改数据方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

批量修改主要有两种方式

第一种

可以通过for循环一条一条修改数据,这样会影响效率,因此我不推荐,所以在这里我也不多说。

第二种

通过修改mybatis中mapper.xml文件,如下:

?
1
2
3
4
5
6
7
8
9
<update id="updateRoleMenus" parameterType="java.util.List"
      <foreach collection="list" item="item" index="index" open="" close="" separator=";"
       update TB_ROLE_MENU  
          <set
          FID=#{item.fid}
          </set
          where ROLEID = #{item.roleid} 
      </foreach
    </update>

mysql及mybatis批量更新数据update

mysql批量更新update

使用case when语句,数据表如下:

mybatis如何批量修改数据

case1:

其中age为非空字段。sql如下

?
1
2
3
4
5
6
update test1
set age=case
when id=2 then 1
when id =3 then 2
end
where id in (2,3,4)

对id为2,3,4的设置age字段,id为2的设置为1,3的设置为2,结果为:

Column ‘age’ cannot be null.

原因是由于id=4没有被上面的when匹配到,因此会被默认设为空null。即未被匹配到的记录并不会保持原来的数值,而是会被设置为null。

case2:

如果想设默认值,可写为:

?
1
2
3
4
5
6
7
update test1
set age=case
when id=2 then 1
when id =3 then 2
else 30
end
where id in (2,3,4)

结果是

mybatis如何批量修改数据

可见id=4的设为了30,也就是通过else default_value可以对未被匹配到的行设置默认值。

case3:

如果想对未匹配到的行设置未原来的值,则可写为:

?
1
2
3
4
5
6
7
update test1
set age=case
when id=2 then 1
when id =3 then 2
when id =4 then test1.age
end
where id in (2,3,4)

这样就可以通过test1.age来使id=4的行保持原值。在mybatis中可看到这种写法的用处。

mybatis实现批量更新update

对应上面的各种case,来写xml。

通常在写代码的时候,有时候不更新的字段就不会设置到对象里,比如Person对象分别有id,name,age三个属性对应数据库的三个字段,如果不想更新id=4的age时,就通常不设置age的值,也就是age=null,这样在case1里,就会被误设为null,详细见如下代码。

case1:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
update test1
<set>
    <trim prefix="age=case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            <if test="item.age !=null">
                when id=#{item.id} then #{item.age}
            </if>
        </foreach>
    </trim>
    <trim prefix="name=case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            when id=#{item.id} then #{item.name}
        </foreach>
    </trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
    #{item.id}
</foreach>

当传入的list中person对象有三个,分别对应id=2,3,4,若不想更新4的age,而只想更新4的姓名,则id=4的person对象age就会被设为null,这样传入该sql时,id=4的数据不会被when匹配,而又没有设置默认值,则原数据会被删除并设为null。

case2:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
update test1
<set>
    <trim prefix="age=case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            <if test="item.age !=null">
                when id=#{item.id} then #{item.age}
            </if>
        </foreach>
        else 30
    </trim>
    <trim prefix="name=case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            when id=#{item.id} then #{item.name}
        </foreach>
    </trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
    #{item.id}
</foreach>

该代码由于设置了else 30,因此会id=4的数据在不设置age的情况下会被更新为30。

case3:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
update test1
<set>
    <trim prefix="age=case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            <if test="item.age !=null">
                when id=#{item.id} then #{item.age}
            </if>
            <if test="item.age ==null">
                when id=#{item.id} then test1.age
            </if>
        </foreach>
    </trim>
    <trim prefix="name=case" suffix="end,">
        <foreach collection="list" item="item" index="index">
            when id=#{item.id} then #{item.name}
        </foreach>
    </trim>
</set>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
    #{item.id}
</foreach>

通过if标签,若传入的属性为null,则保持数据库原值。

补充:

更新多条数据同一字段为同一值:

?
1
UPDATE test1 SET age=24 WHERE id in(2,3,4);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/chenmin_test/article/details/84190252

延伸 · 阅读

精彩推荐