mapper.xml传入参数为Map的正确做法

  1. 错误用法
xml 复制代码
    <update id="updatePropertiesBatch" parameterType="java.util.Map">
        <foreach collection="codePropertiesMap.entrySet()" item="entry" separator=",">
            update payment_channel
            set properties = #{entry.value}
            where code = #{entry.key}
        </foreach>
    </update>

这样会报错:

复制代码
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'key' in 'class java.lang.String'

	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
	at com.sun.proxy.$Proxy74.update(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:287)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:65)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:96)
	at com.sun.proxy.$Proxy75.updatePropertiesBatch(Unknown Source)
  1. 正确用法
xml 复制代码
    <update id="updatePropertiesBatch" parameterType="java.util.Map">
        <foreach collection="codePropertiesMap" item="value" index="key"  separator=";">
            update payment_channel
            set properties = #{value}
            where code = #{key}
        </foreach>
    </update>

使用批量更新需要加入allowMultiQueries=true

  1. 批量更新--没有设置allowMultiQueries=true时
xml 复制代码
<update id="updatePropertiesBatch" parameterType="java.util.Map">
        UPDATE payment_channel
        SET properties = CASE code
        <foreach collection="codePropertiesMap" index="key" item="value" separator=" " open="" close="">
            WHEN #{key} THEN #{value}
        </foreach>
        ELSE properties -- 如果没有匹配的code,则保持原值不变
        END,
        update_time = NOW() -- 更新时间戳
        WHERE code IN
        <foreach collection="codePropertiesMap" index="key" open="(" separator="," close=")">
            #{key}
        </foreach>
    </update>
相关推荐
ejinxian2 小时前
protobuf 、JSON、XML传输格式
xml·json·protobuf
yue00811 小时前
C# XML文件的读写V2.0
xml·开发语言·c#
emma羊羊2 天前
【weblogic】XML反序列化漏洞
xml·安全
研來如此4 天前
XML与HTML
xml·html
一个W牛4 天前
报文比对工具(xml和sop)
xml·前端·javascript
Java&Develop4 天前
使用 JDOM 库生成 XML 文件并返回 File
xml
关关长语4 天前
Dotnet使用System.Xml.Serialization处理Xml序列化
xml·c#·.net
Dolphin_Home4 天前
轻量实用的 XML 与 JSON / 对象互转工具类(Jackson 实现)
xml·java·json
huluang5 天前
XML文档差异分析工具:深入解析Word XML结构变化
xml·word
未孤_有青山6 天前
库卡机器人通讯-EtherKRL-XML格式
xml·c#