zoukankan      html  css  js  c++  java
  • mybatis 批量更新

    https://blog.csdn.net/xu1916659422/article/details/77971696

    注意第一种方法要想成功,需要在db链接url后面带一个参数  &allowMultiQueries=true

    即:  jdbc:mysql://localhost:3306/mysqlTest?characterEncoding=utf-8&allowMultiQueries=true

        <!-- 这次用resultmap接收输出结果  -->
        <select id="findByName" parameterType="string" resultMap="customerMap">
            select * from t_customer where c_name like concat('%', #{name},'%') order by c_ceroNo limit 0,100
        </select>
    
    
        <!-- 批量更新第一种方法,通过接收传进来的参数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>
  • 相关阅读:
    (转)Computer Vision Open Source Algorithm Implementations
    关于项目依赖项
    fatal error CVT1100: duplicate resource. type:MANIFEST, name:1, language:0×0409
    转:error LNK2005
    C++ string类常用函数
    [转载]同步synchronized方法和代码块
    Java语言基础常用对象API(二)泛型、Map集合
    Java语言基础IO流(输入输出流) 字符流
    Java语言基础常用对象API(二)集合框架
    Java语言基础IO流(输入输出流) 字节流、转换流
  • 原文地址:https://www.cnblogs.com/lxh520/p/8794381.html
Copyright © 2011-2022 走看看