MyBatis循环获取Map中的key和value的方法

时间:2022-10-17 19:15:48

有时候需要简单地把一个Map中所有的key和value获取出来,拼到sql语句中。MyBatis提供的一种方法是遍历Map中的entrySet,然后把key扔进index里面,value扔进item中。具体的一个使用的例子如下:

	<insert id="operationName" parameterType="map">
INSERT INTO table_name(hot_word, cnt)
VALUES
<foreach item="value" index="key" collection="mapData.entrySet()" open="(" separator="),(" close=")">
#{key}, #{value}
</foreach>
ON DUPLICATE KEY UPDATE
cnt=VALUES(cnt)
</insert>