mybatis使用foreach处理List中的Map

时间:2025-02-16 10:55:21

问题:

参数的数据结构是一个ArrayList<Map<String, Integer>>,需要以String,Integer为条件批量更新数据库的数据.

将参数封装到叫做JsonData的qv中,JsonData的关键代码是

    private ArrayList<Map<String, Integer>> usersPlatforms;

    public ArrayList<Map<String, Integer>> getUsersPlatforms() {
        return usersPlatforms;
    }

    public void setUsersPlatforms(ArrayList<Map<String, Integer>> usersPlatforms) {
         = usersPlatforms;
    }

Mapper中的方法是:

updateXxxx(JsonData jsonData);

的sql是:

<update  parameterType="JsonData">
        UPDATE xxx SET `xx` = 10
        <where>
            <foreach collection="usersPlatforms" item="userPlatform" open="" close="" separator="OR">
                <foreach collection="" item="key" open=" user_id = " close="" separator="">
                    #{key}
                </foreach>
                <foreach collection="" item="value" open=" AND platform = " close="" separator="">
                    #{value}
                </foreach>
            </foreach>
        </where>
    </update>

 

相关文章