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>
相关推荐
itachi-uchiha15 小时前
awk处理xml文件&&封装集合变量和调用
xml·shell·awk
武子康3 天前
Java-39 深入浅出 Spring - AOP切面增强 核心概念 通知类型 XML+注解方式 附代码
xml·java·大数据·开发语言·后端·spring
Ll13045252985 天前
基于 COM 的 XML 解析技术(MSXML) 的总结
xml
在代码的海洋中寻找亚特兰蒂斯5 天前
AJAX对于XML和JSON的处理
xml·ajax·json
BinField6 天前
ToolsSet之:XML工具
xml·windows·microsoft
SEO-狼术7 天前
Connect Directly to Oracle XML Data
xml·数据库·oracle
YSoup7 天前
2025年目前最新版本Android Studio自定义xml预览的屏幕分辨率
android·xml·android studio
abcnull8 天前
mybatis的mapper对应的xml写法
xml·sql·spring·mybatis·mapper
Blue桃之夭夭8 天前
HTML、XML、JSON 是什么?有什么区别?又是做什么的?
xml·html·json
小于村8 天前
pom.xml 文件中配置你项目中的外部 jar 包打包方式
xml·java·jar