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>
相关推荐
烤麻辣烫13 小时前
黑马程序员苍穹外卖后端概览
xml·java·数据库·spring·intellij-idea
暗碳13 小时前
安卓abx二进制xml文件转换普通xml文件
android·xml
jinxinyuuuus16 小时前
文件格式转换工具:XLSX的XML流式解析、数据类型推断与CSV编码规范
xml
czlczl200209251 天前
从抛出异常到返回 JSON/XML:SpringBoot 异常处理全链路解析
xml·spring boot·json
jiayong231 天前
MyBatis XML Mapper 特殊字符处理方案
xml·mybatis
e***19352 天前
【mybatis】基本操作:详解Spring通过注解和XML的方式来操作mybatis
xml·spring·mybatis
编程修仙2 天前
第七篇 java的注解以及使用反射实现自定义注解功能
xml·java·开发语言·spring
暗碳2 天前
ai分析aweme-app.xml,default_config .xml文件
xml·人工智能
缺点内向2 天前
如何在 C# 中高效的将 XML 转换为 PDF
xml·后端·pdf·c#·.net
z***D6482 天前
使用 Logback 的最佳实践:`logback.xml` 与 `logback-spring.xml` 的区别与用法
xml·spring·logback