欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis批量插入和批量更新

程序员文章站 2022-05-25 17:01:20
...

一、批量插入

    mybatis批量插入和批量更新

mybatis批量插入和批量更新

<!-- 批量更新数据 -->
    <update id="update" parameterType="java.util.List">
        update nt_menu
        <trim prefix="set" suffixOverrides=",">  
	        <trim prefix="breakfast =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.breakfast != null">  
	                    when menuId=#{cus.menuId} then #{cus.breakfast}  
	                </if>  
	            </foreach>  
	        </trim>  
	        <trim prefix="lunch =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.lunch != null">  
	                    when menuId=#{cus.menuId} then #{cus.lunch}  
	                </if>  
	            </foreach>  
	        </trim>  
	        <trim prefix="dinner =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.dinner != null">  
	                    when menuId=#{cus.menuId} then #{cus.dinner}  
	                </if>  
	            </foreach>  
	        </trim>  
	        <trim prefix="modifier =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.modifier != null">  
	                    when menuId=#{cus.menuId} then #{cus.modifier}  
	                </if>  
	            </foreach>  
	        </trim>  
    	</trim>  
	    <where>  
	        <foreach collection="list" separator="or" item="cus">  
	            menuId=#{cus.menuId}  
	        </foreach>  
	    </where>  
    </update>

二、批量更新

 <update id="update" parameterType="java.util.List">
        update nt_menu
        <trim prefix="set" suffixOverrides=",">  
	        <trim prefix="breakfast =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.breakfast != null">  
	                    when menuId=#{cus.menuId} then #{cus.breakfast}  
	                </if>  
	            </foreach>  
	        </trim>  
	        <trim prefix="lunch =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.lunch != null">  
	                    when menuId=#{cus.menuId} then #{cus.lunch}  
	                </if>  
	            </foreach>  
	        </trim>  
	        <trim prefix="dinner =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.dinner != null">  
	                    when menuId=#{cus.menuId} then #{cus.dinner}  
	                </if>  
	            </foreach>  
	        </trim>  
	        <trim prefix="modifier =case" suffix="end,">  
	            <foreach collection="list" item="cus">  
	                <if test="cus.modifier != null">  
	                    when menuId=#{cus.menuId} then #{cus.modifier}  
	                </if>  
	            </foreach>  
	        </trim>  
    	</trim>  
	    <where>  
	        <foreach collection="list" separator="or" item="cus">  
	            menuId=#{cus.menuId}  
	        </foreach>  
	    </where>  
    </update>