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

版权声明:本文为原创文章,转载请注明转自Clement-Xu的csdn博客。 https://blog.csdn.net/ClementAD/article/details/55099432

有时候需要简单地把一个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>


猜你喜欢

转载自blog.csdn.net/ClementAD/article/details/55099432