mybatis 批量更新/插入

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pansanday/article/details/81772798

一. 批量更新

接口

void batchUpdate(List<OrderItem> oiList);

XML文件

<update id="batchUpdate" parameterType="java.util.List">
    <foreach collection="list" item="oiList" index="index" open="" close="" separator=";">
        update order_item
        <set>
            child_order_no = #{oiList.childOrderNo}
        </set>
        where id_ = #{oiList.id}
    </foreach>
</update>

最终执行的SQL语句如下:

Preparing: update order_item SET child_order_no = ? where id_ = ? ; update order_item SET child_order_no = ? where id_ = ? ; update order_item SET child_order_no = ? where id_ = ?

以分号分隔两个更新的SQL语句

二. 批量插入

接口

void batchInsert(@Param("japList") List<ActivityPublish> japList, @Param("actId") String actId);

XML文件

<insert id="batchInsert" parameterType="java.util.List">
    delete from activity_publish where act_id = #{actId};

    insert into activity_publish
    (id_, tenant_id, act_id, create_by, create_time, update_by, update_time)
    values
    <foreach collection="japList" item="item" index="index" separator=",">
        (
        #{item.id}, #{item.tenantId},#{item.actId},
        #{item.createBy}, #{item.createTime},#{item.updateBy}, #{item.updateTime}
        )
    </foreach>
</insert>

这里我执行了两个操作,先是根据actId删除相应的数据,再进行批量的插入,两个SQL语句可以一起执行。

foreach 这句有点像java的 foreach,例如:

for (ActivityPublish item : japList) {

     System.out.println(item.id);

}

猜你喜欢

转载自blog.csdn.net/pansanday/article/details/81772798