mybatis 多条件多值批量更新

本文借鉴自Mybatis中进行批量更新(updateBatch)

近来常用,留个记忆

<update id="batchUpdate" parameterType="java.util.List">
    	update demo_table
        <trim prefix="set" suffixOverrides=",">
    	    field1 = 
    	    <foreach collection="list" item="item" open="case " close=" end,">
    		when field2 = #{item.value2} AND field3 = #{item.value3} then #{item.value1}
    	    </foreach>
            field4 =
            <foreach collection="list" item="item" open="case " close=" end,">
    		when field2 = #{item.value2} AND field3 = #{item.value3} then
                <choose>
                    <when test="item.value4 != null">
                        #{item.value4}
                    </when>
                    <otherwise>field4</otherwise>
                </choose>
    	    </foreach>
        </trim>
    	<where>
    		<foreach collection="list" item="item" open="( " separator=") or (" close=" )">
	    		field2 = #{item.value2} AND field3 = #{item.value3}
	    	</foreach>
    	</where>
    </update>

如果要修改的字段是多个的话,可以把判断赋值的地方重复写,然后改掉赋值字段的地方就好

 

 

 

展开阅读全文

没有更多推荐了,返回首页