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 天前
WPF XAML+布局+控件
xml·开发语言·c#·视觉检测·wpf·机器视觉·视图
鸽芷咕3 天前
KingbaseES数据类型完全指南:从基础CHAR到JSON/XML/几何类型
xml·oracle·json
java1234_小锋7 天前
MyBatis中XML映射有哪些标签?
xml·tomcat·mybatis
大腕先生7 天前
通用分页超详细介绍(附带源代码解析&页面展示效果)
xml·java·linux·服务器·开发语言·前端·idea
许彰午10 天前
Spring Boot + Vue 实现 XML 动态表单:固定字段 + 自由扩展方案
xml·vue.js·spring boot
untE EADO11 天前
Tomcat的server.xml配置详解
xml·java·tomcat
zuowei288911 天前
spring实例化对象的几种方式(使用XML配置文件)
xml·java·spring
weixin_5206498711 天前
xml json ini 文件语法
xml·java·json
Full Stack Developme11 天前
Hutool XML 操作教程
xml·windows·python