• mybatis传入List实现批量更新


    如果要在一个update里面执行多条更新语句,只需要在jdbc:url后面跟上allowMultiQueries=true的参数,比如:

    jdbc:mysql://127.0.0.1:3306/test?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=utf-8

    1.用for循环通过循环传过来的参数集合,循环出N条sql,

    2.用mysql的case when 条件判断变相的进行批量更新  

    <!-- 批量更新第一种方法,通过接收传进来的参数list进行循环着组装sql -->
    <update id="batchUpdate" parameterType="java.util.Map">
        <!-- 接收list参数,循环着组装sql语句,注意for循环的写法
             separator=";" 代表着每次循环完,在sql后面放一个分号
             item="cus" 循环List的每条的结果集
             collection="list" list 即为 map传过来的参数key -->
        <foreach collection="list" separator=";" item="cus">
            update t_customer set
            c_name = #{cus.name},
            c_age = #{cus.age},
            c_sex = #{cus.sex},
            c_ceroNo = #{cus.ceroNo},
            c_ceroType = #{cus.ceroType}
            where id = #{cus.id}
        </foreach>
    </update>
    
    <!-- 批量更新第二种方法,通过 case when语句变相的进行批量更新 -->
    <update id="batchUpdateCaseWhen" parameterType="java.util.Map">
        update t_customer
        <trim prefix="set" suffixOverrides=",">
            <!-- 拼接case when 这是一种写法 -->
            <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->
            <!--when #{cus.id} then #{cus.age}-->
            <!--</foreach>-->
    
            <!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->
            <trim prefix="c_name =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.name!=null">
                        when id=#{cus.id} then #{cus.name}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_age =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.age!=null">
                        when id=#{cus.id} then #{cus.age}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_sex =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.sex!=null">
                        when id=#{cus.id} then #{cus.sex}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_ceroNo =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.ceroNo!=null">
                        when id=#{cus.id} then #{cus.ceroNo}
                    </if>
                </foreach>
            </trim>
            <trim prefix="c_ceroType =case" suffix="end,">
                <foreach collection="list" item="cus">
                    <if test="cus.ceroType!=null">
                        when id=#{cus.id} then #{cus.ceroType}
                    </if>
                </foreach>
            </trim>
        </trim>
        <where>
            <foreach collection="list" separator="or" item="cus">
                id = #{cus.id}
            </foreach>
        </where>
    </update>
  • 相关阅读:
    C# asp:Repeater DataSource List<T>
    MySQL DATE_FORMATE函数内置字符集的坑_转小叶子爹
    MySQL count(distinct) 逻辑的一个bug
    org.hibernate.PersistentObjectException: detached entity passed to persist:
    CGLIB Enhancement failed
    firstResult/maxResults specified on polymorphic query;
    Last packet sent to the server was 0 ms ago.
    MySql Error Code: 2006 – MySQl
    InnoDB: Error: auto-extending data file ./ibdata1 is of a different size
    mysql 大数据量分页处理
  • 原文地址:https://www.cnblogs.com/grasp/p/10130145.html
Copyright © 2020-2023  润新知