mybatis批量更新

 

 

<update id="synData" parameterType="java.util.List">
        insert into aaa(id,register_time)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            ('${item.id}','${item.registerTime}')
        </foreach>
        ON DUPLICATE KEY UPDATE
        id=VALUES(id),register_time = VALUES(register_time) -- 括号放列字段名
    </update>

 

 

 

 

 UPDATE course
    SET name = CASE id 
        WHEN 1 THEN 'name1'
        WHEN 2 THEN 'name2'
        WHEN 3 THEN 'name3'
    END, 
    title = CASE id 
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
    END
WHERE id IN (1,2,3)

 

<update>
    update course
            <trim prefix="set" suffixOverrides=",">
             <trim prefix="name=case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                         <if test="item.name!=null">
                          when id=#{item.id} then #{item.name}
                         </if>
                 </foreach>
              </trim>
              <trim prefix="title =case" suffix="end,">
                 <foreach collection="list" item="item" index="index">
                         <if test="item.title!=null">
                          when id=#{item.id} then #{item.title}
                         </if>
                 </foreach>
              </trim>
             </trim>
            where
            <foreach collection="list" separator="or" item="item" index="index">
              id=#{item.id}
          </foreach>
</update>

原文:https://blog.csdn.net/yjaspire/article/details/81316885

 

<insert id="updateBatch" parameterType="java.util.List">        insert into standard_relation(id,relation_type, standard_from_uuid,        standard_to_uuid, relation_score, stat,        last_process_id, is_deleted, gmt_created,        gmt_modified,relation_desc)VALUES        <foreach collection="list" item="item" index="index" separator=",">            (#{item.id,jdbcType=BIGINT},#{item.relationType,jdbcType=VARCHAR}, #{item.standardFromUuid,jdbcType=VARCHAR},            #{item.standardToUuid,jdbcType=VARCHAR}, #{item.relationScore,jdbcType=DECIMAL}, #{item.stat,jdbcType=TINYINT},            #{item.lastProcessId,jdbcType=BIGINT}, #{item.isDeleted,jdbcType=TINYINT}, #{item.gmtCreated,jdbcType=TIMESTAMP},            #{item.gmtModified,jdbcType=TIMESTAMP},#{item.relationDesc,jdbcType=VARCHAR})        </foreach>        ON DUPLICATE KEY UPDATE        id=VALUES(id),relation_type = VALUES(relation_type),standard_from_uuid = VALUES(standard_from_uuid),standard_to_uuid = VALUES(standard_to_uuid),        relation_score = VALUES(relation_score),stat = VALUES(stat),last_process_id = VALUES(last_process_id),        is_deleted = VALUES(is_deleted),gmt_created = VALUES(gmt_created),        gmt_modified = VALUES(gmt_modified),relation_desc = VALUES(relation_desc)    </insert>————————————————版权声明:本文为CSDN博主「PreciousLife」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/q957967519/article/details/88669552

posted @ 2020-08-13 10:47  君子笑而不语  阅读(263)  评论(0编辑  收藏  举报