1. Java代码循环执行sql,逐条更新

        这种方式就是将需要更新的数据,循环调用update方法去更新数据,实现代码如下:

public void test() {
        // 需要更新的集合
        List<HashMap<String, Object>> updateMap = new ArrayList<>();
        HashMap<String, Object> param = new HashMap<>(3);
        param.put("name", "test");
        param.put("price", 12.1);
        param.put("id", 1223);
        updateMap.add(param);
        // 循环执行更新
        updateMap.stream().forEach(map -> {
            sqlSession.update("update.updatePrice", map);
        });
    }

XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis//DTD Mapper 3.0//EN"
        "http://mybatis/dtd/mybatis-3-mapper.dtd">

<mapper namespace="update">
    <!--更新price表-->
    <update id="updatePrice">
        update price_info
        <set>
            <if test="name != null and name !=''">
                name = #{name},
            </if>
            <if test="price != null">
                sex = #{price}
            </if>
        </set>
        where id = #{id}
    </update>
</mapper>

这种方法显而易见的最大问题就是每次都要去操作数据库,数据量大了可能会造成sql阻塞但是效率还是挺高。

2. 通过传入list进行遍历

 public void test() {
        // 需要更新的集合
        List<HashMap<String, Object>> updateMap = new ArrayList<>();
        HashMap<String, Object> param = new HashMap<>(3);
        param.put("id", "1234");
        param.put("status", "2");
        param.put("price", "22");
        updateMap.add(param);
        sqlSession.update("supplierSku.updatePrice", updateMap);
    }

XML文件如下: 

<update id="updatePrice">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update price_info
            <set>

                <if test="item.status != null and item.status != ''">
                    status = #{item.status},
                </if>
                <if test="item.price != null and item.price != ''">
                    price = #{item.price},
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

其实这种和第一种方式一样,只是把循环list放到了XML中去处理。 

3. 通过case,when变相实现批量更新

    public void test() {
        // 需要更新的集合
        List<HashMap<String, Object>> updateMap = new ArrayList<>();
        HashMap<String, Object> param = new HashMap<>(3);
        param.put("id", "1234");
        param.put("status", "2");
        param.put("price_end_date", new Date());
        updateMap.add(param);
        sqlSession.update("supplierSku.updateSupplierSkuPrice", updateMap);
    }

XML文件如下:

<update id="updateSupplierSkuPrice" parameterType="java.util.List">
        update price_info
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.status!=null and i.status!= ''">
                        when id=#{i.id} then #{i.status}
                    </if>
                </foreach>
            </trim>
            <trim prefix="price_end_date =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.price_end_date!=null">
                        when id=#{i.id} then #{i.price_end_date}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="i" index="index" >
            id=#{i.id}
        </foreach>
    </update>

使用case,when最后会变成一条sql语句,但是每次都得去遍历list集合,数据量大了会影响效率问题

PS:只有string类型的数据才能使用 xxx  != ' '   当其他类型使用就会报类型转换错误

更多推荐

【MyBatis】关于MyBatis批量更新的几种方式