MyBatis 碰到的问题

MyBatis 碰到的问题

Mybatis之foreach遍历Map

参考

    1. 接口中的方法
void batchInsertMap(@Param("studentMap") Map<Integer,Student> studentMap);
    1. 对应Sql,使用entrySet(),注意 indexkey,itemvalue
<insert id="batchInsertMap" parameterType="java.util.Map">
    INSERT INTO student(id,username)
    VALUES
    <foreach collection="studentMap.entrySet()" index="key" item="value" separator=",">
        (#{key},#{value.username})
    </foreach>
</insert>

Mybatis中进行批量更新

参考

    1. 接口中的方法
void batchUpdateMap(@Param("studentMap") Map<Integer,String> studentMap);
    1. 对应SQL
<insert id="batchUpdateMap" parameterType="java.util.Map">
        update student
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="username = case" suffix="end,">
                <foreach collection="studentMap.entrySet()" index="key" item="value">
                    when id= #{key} then #{value}
                </foreach>
            </trim>
            <trim prefix="salary = case" suffix="end,">
                <foreach collection="studentMap.entrySet()" index="key" item="value">
                    when id= #{key} then 100
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="studentMap.entrySet()" index="key" separator="," open="(" close=")">
            #{key}
        </foreach>
</insert>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。