mybatis批量添加,修改

mybaitis的批量添加修改

添加:

<insert id="savelist" parameterType="java.util.List">
        
        insert into table1(
			a,
                        b,
                        c,
                        d
		) values 
        <foreach collection="list" item="l" index="index" separator=",">
	        (
				#{l.a},
				#{l.b},
				#{l.c},
				#{l.d}
			)
        </foreach>
    </insert>
批量添加时碰到的错误:Parameter '__frch_item_0' not found. Available parameters are [list] 
原因可能如下:

1.查看parameterType的类型是不是Java.util.List类型

2.看foreach的collection属性是不是list

3.看foreach里取的属性值是否写错,大小写是否相同

4.查看foreach里取的属性值实体对象中是否存在

尤其是第四条,我本来还想找能不能动态批量添加,但是并没有找到。如果有哪个大神知道,麻烦评论告诉我,非常感谢


批量修改:

<update id="editlist" parameterType="java.util.Map">  
	    update  table1 
	    <trim prefix="set" suffixOverrides=",">  
	        <!-- 拼接case when 这是一种写法 -->  
	        <!--<foreach collection="list" separator="" item="cus" open="c_age = case id" close="end, ">-->  
	        <!--when #{cus.id} then #{cus.age}-->  
	        <!--</foreach>-->  
	  
	        <!-- 拼接case when 这是另一种写法,这种写着更专业的感觉 -->  
	        <trim prefix="b =case" suffix="end,">  
	            <foreach collection="list" item="l">  
	                <if test="l.b!=null and l.b != ''">  
	                    when id = #{l.id} then #{l.b}  
	                </if>  
	            </foreach>  
	        </trim> 
	        <trim prefix="c =case" suffix="end,">  
	            <foreach collection="list" item="l">  
	                <if test="l.c!=null and l.c != ''">  
	                    when id = #{l.id} then #{l.c}  
	                </if>  
	            </foreach>  
	        </trim> 
	        <trim prefix="d =case" suffix="end,">  
	            <foreach collection="list" item="l">  
	                <if test="l.d!=null and l.d != ''">  
	                    when id = #{l.id} then #{l.d}  
	                </if>  
	            </foreach>  
	        </trim> 
	        
	    <where>  
	        <foreach collection="list" separator="or" item="l">  
				id = #{l.id}
	        </foreach>  
	    </where>  
	</update>
这个可以动态批量修改,还是很方便好用的
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值