Mybatis之foreach遍历Map

时间:2022-10-26 10:27:30

话不多说,直接上代码:

方式一:利用map的entrySet()方法

 
  <insert id="XXX" parameterType="java.util.Map">
    INSERT INTO table(a, b)
    VALUES
    <foreach collection="param.entrySet()" open="(" separator="," close=")" index="key" item="val">
        #{key}, #{val}
    </foreach>
  </insert>
</mapper>

方式二:

  <insert id="XXX" parameterType="java.util.Map">
    INSERT INTO table
    <foreach collection="params.keys" item="key" open="(" separator="," close=")">
        获取值:#{param[key]}
        键:#{key}
    </foreach>
    VALUES
    <foreach collection="param.value" item="val" open="(" separator="," close=")">
       值:#{val}
    </foreach>
  </insert>

如有错误,欢迎指正!