<update id="updateUser" parameterType="java.util.Map">
update userImage
<trim prefix="set" suffixOverrides=",">
<trim prefix="userImage_code =case" suffix="end,">
<foreach collection="mapData.entrySet()" item="value" index="key">
<if test="value.code!=null">
when id=#{key} then #{value.code}
</if>
</foreach>
</trim>
<trim prefix="userImage_id =case" suffix="end,">
<foreach collection="mapData.entrySet()" item="value" index="key">
<if test="value.id!=null">
when id=#{key} then #{value.id}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="mapData.entrySet()" separator="or" item="value" index="key" >
id=#{key}
</foreach>
</update>
传入数据
public int updateUsert(@Param("mapData")Map<String,UserImage> mapData);
foreach collection=“mapData.entrySet()” item=“value” index="key"是重点
mapData.entrySet()获得map的键-值 对的集合,item是map中的value集合,index是map中的key集合
其中 when id=#{key} then #{value.code} 的意思是
满足条件id=#{key} 则执行 userImage_code = #{value.code}
参考:
Mybatis中case when 配合 trim的使用方法
mybatis批量更新数据三种方法效率对比
MyBatis循环获取Map中的key和value的方法