mybatis批量添加,修改

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/c782699991/article/details/79400636

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>
這個可以動態批量修改,還是很方便好用的
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章