mybatis批量新增

批量新增两种方式:

①:使用case when end形式 

update  capital_invoice_item 
set
status=
case id then 1 then 11
case id then 2 then 22
end
where id in (1,2)
UPDATE capital_invoice_item
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="status= case id" suffix="end,">
                <foreach collection="list" item="item">
                    <if test="item.status != null">
                      WHEN #{item.id} then #{item.status}
                    </if>
                </foreach>
            </trim>
        </trim>
        WHERE id IN
        <foreach collection="list" item="item" open="(" close=")" separator=",">
          #{item.id}
        </foreach>

prefix:前缀

suffixOverrides:去掉最后一个","

suffix:后缀

foreach:循环标签 

collection:需要迭代的内容

item表示集合中每一个元素进行迭代时的别名,

index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,

open表示该语句以什么开始, separator表示在每次进行迭代之间以什么符号作为分隔 符,

close表示以什么结束

 ②:循环插入

    <update id="">
        <foreach collection="list" item="item">
            UPDATE bill_files
            set a= #{item.aa},
            b = #{item.bb}
            WHERE ....
        </foreach>
    </update>

猜你喜欢

转载自blog.csdn.net/qq_34724270/article/details/83376559