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>
相关推荐
Мартин.2 天前
[Meachines] [Medium] DevOops XXE-XML-RSS+cPickle反序列化+git-leak权限提升
xml·git
珺毅同学2 天前
XML标签格式转换为YOLO TXT格式
xml·人工智能·yolo
一个幽默的程序员3 天前
Postman 如何发送 XML 格式的 API 请求?
xml·测试工具·postman
Smilecoc3 天前
EXCEL报错:无法共享此工作薄,因表包含excel表或xml映射的解决方法
xml·excel
demonlg01123 天前
Go 语言标准库中encoding/xml模块详细功能介绍与示例
xml·开发语言·golang
逍遥德4 天前
pom.xml与.yml,java配置参数传递
xml·java·spring boot·后端·系统架构
一头大学牲5 天前
Data Serialization(数据序列化)
xml·服务器·网络·json·yaml·数据序列化
阑梦清川7 天前
Mybatis操作数据库(注解+xml两个方式)
xml·数据库·mybatis
2301_818732067 天前
springboot项目,mapper.xml里面,jdbcType报错 已解决
xml·java·spring boot·spring·intellij-idea·mybatis·idea
鸽鸽程序猿7 天前
【JavaEE】Mybatis XML配置文件实现增删改查
xml·java-ee·mybatis