mybatis批量插入方式

mybatis批量操作

1)更新的方式:

 

-- 批量更新
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
UPDATE BS_MENU
<trim prefix="SET" suffixOverrides=",">
<if test="item.menuName != null and item.menuName !=''">
MENU_NAME = #{item.menuName},
</if>
<if test="item.updateTime != null">
UPDATE_TIME = #{item.updateTime},
</if>
</trim>
WHERE MENU_ID = #{item.menuId,jdbcType=VARCHAR}
</foreach>

 

UPDATE BS_MENU
<set>
    <if test="status != null">
        STATUS = #{status,jdbcType=DECIMAL},
    </if>
</set>
where MENU_NAME IN
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
    #{item}
</foreach>

2)批量新增

<insert id="saveBatch" parameterType="list">
    insert INTO BS_MENU (MENU_ID,MENU_NAME, UPDATE_TIME)
    <foreach collection="list" item="item" index="index" separator="union all">
        select
        #{item.menuId},
        #{item.menuName},
        to_date(#{item.updateTime}, 'yyyy-mm-dd hh24:mi:ss')
        from dual
    </foreach>
</insert>

 

posted @ 2022-08-03 22:23  星空异皓`  阅读(208)  评论(0编辑  收藏  举报
/*页脚固定*/