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>
相关推荐
wkj0018 小时前
springboot + mybatis 需要写 .xml吗
xml·spring boot·mybatis
llkk星期五12 小时前
Mujoco xml < sensor>
xml·机器人
weixin_4565881521 小时前
【Maven】特殊pom.xml配置文件 - BOM
xml·java·maven
百锦再1 天前
Kotlin学习基础知识大全(上)
android·xml·学习·微信·kotlin·studio·mobile
Minyy111 天前
SpringBoot程序的创建以及特点,配置文件,LogBack记录日志,配置过滤器、拦截器、全局异常
xml·java·spring boot·后端·spring·mybatis·logback
北极的企鹅882 天前
XML内容解析成实体类
xml·java·开发语言
yuren_xia2 天前
Spring XML 配置
xml·java·spring
BOB-wangbaohai2 天前
Flowable7.x学习笔记(十四)查看部署流程Bpmn2.0-xml
xml·笔记·学习
聪明的墨菲特i2 天前
SQL进阶知识:九、高级数据类型
xml·数据库·sql·mysql·json·空间数据类型
小刘同学++3 天前
Qt 处理 XML 数据
xml·qt