MyBatis使用Map

mytabis是可以遍历map的,试过很多其他的方法都不行,最终使用如下方法是可以的:

1.mapper.java如下(注意要加@Param注解,否则不行,我就在这里折腾了不少时间):

int updateBatch(@Param("map") Map<Long, String> map);

2.mapper.xml如下(注意collection="map.entrySet()"或者collection="map"都行):

<update id="updateBatch" parameterType="java.util.Map">
    update tv_column
    set name = case pk
    <foreach collection="map.entrySet()" index="key" item="value">
        when #{key} then #{value}
    </foreach>
    end
    where pk in
    <foreach collection="map.entrySet()" index="key" separator="," open="(" close=")">
        #{key}
    </foreach>
</update>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容