mybatis批量增、刪、改(更新)操作oracle和mysql批量寫法小記

  • 2019 年 11 月 1 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/eguid_1/article/details/57426324

前言:用mybatis也好幾年了,mybatis在批量的增刪操作也寫起來也是比較簡單的,只有批量更新這一塊是特別坑,特此記錄。

註:本文主要用來記錄oracle和mysql資料庫在使用mybatis的情況下批量增、刪、改(更新)的常用寫法

一、批量插入

1、oracle寫法:

<insert id="insertZaixcsList" parameterType="cc.eguid.Zaixcs"> insert into b_dbgl_zaixcs ( zaixcsid, mingc, pingsyid, xinxid, fujid, jieg, pingfjg, pingf, zhuangt, shic, startriq, endriq, pingfriq, datr, pingfr, beiz ) <foreach collection="list" item="item" index="index" separator="union all"> (select #{item.zaixcsid,jdbcType=VARCHAR}, #{item.mingc,jdbcType=VARCHAR}, #{item.pingsyid,jdbcType=VARCHAR},#{item.xinxid,jdbcType=VARCHAR}, #{item.fujid,jdbcType=VARCHAR}, #{item.jieg,jdbcType=VARCHAR}, #{item.pingfjg,jdbcType=VARCHAR}, #{item.pingf,jdbcType=DECIMAL}, #{item.zhuangt,jdbcType=VARCHAR},#{item.shic,jdbcType=DECIMAL}, #{item.startriq,jdbcType=TIMESTAMP}, #{item.endriq,jdbcType=TIMESTAMP}, #{item.pingfriq,jdbcType=TIMESTAMP}, #{item.datr,jdbcType=VARCHAR}, #{item.pingfr,jdbcType=VARCHAR},#{item.beiz,jdbcType=VARCHAR} from dual) </foreach> </insert>

2、mysql寫法:

insert into B_SYS_FUJ (FUJ_ID, RELATE_ID, RELATE_TABLE_NAME, FUJ_LX, WENJLX,WENJM, FJMC, FUJ_PATH,CREATE_USER_ID, CREATE_USER, CREATE_TIME, RELATE_TABLE_ZIDUAN,CONTENTTYPE,ZHUANGT) values <foreach collection="list" item="item" index="index" separator="," > (#{item.fujId,jdbcType=VARCHAR}, #{item.relateId,jdbcType=VARCHAR}, #{item.relateTableName,jdbcType=VARCHAR}, #{item.fujLx,jdbcType=VARCHAR}, #{item.wenjlx,jdbcType=VARCHAR}, #{item.wenjm,jdbcType=VARCHAR}, #{item.fjmc,jdbcType=VARCHAR}, #{item.fujPath,jdbcType=VARCHAR}, #{item.createUserId,jdbcType=VARCHAR}, #{item.createUser,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.relateTableZiduan,jdbcType=VARCHAR}, #{item.contentType,jdbcType=VARCHAR}, #{item.zhuangt,jdbcType=VARCHAR} ) </foreach>

二、批量刪除

註:批量刪除操作oracle與mysql寫法相同

當collection=」array「時,表名參數為數組;

當collection=」list「時,表名參數為集合;

1、刪除數組數組

<delete id="batchDeleteEmpArr" parameterType="int"> delete from emp where empno in <foreach item="empnoItem" collection="array" open="(" separator="," close=")"> #{empnoItem} </foreach> </delete>

2、刪除list列表數據

<delete id="batchDeleteEmpList" parameterType="int"> delete from emp where empno in <foreach item="item" collection="list" open="(" separator="," close=")"> #{item} </foreach> </delete>

3、刪除查詢到的數據

<delete id="deleteByParent" parameterType="string"> delete from QIYDFBZ where BIAOZBID in( SELECT biaozbid FROM B_DBGL_QIYDFBZ CONNECT BY PRIOR FENXID = FUJID start WITH BIAOZBID = #{biaozbid,jdbcType=VARCHAR} ) </delete>

三、批量更新

1、oracle寫法:

註:這樣的寫法雖然可以更新,但是更新操作返回值是-1,並沒有返回更新的行數

<update id="updateBatch" parameterType="com.itssky.aqjg.entity.dbgl.Zaixcs"> begin <foreach collection="list" item="item" index="index" separator=";"> update B_DBGL_ZAIXCS <trim prefix="set" suffixOverrides=","> <if test="item.mingc != null and item.mingc !=''"> MINGC= #{item.mingc,jdbcType=VARCHAR}, </if> <if test="item.pingf != null and item.pingf !=''"> PINGF=#{item.pingf,jdbcType=DECIMAL}, </if> <if test="item.zhuangt != null and item.zhuangt !=''"> ZHUANGT=#{item.zhuangt,jdbcType=VARCHAR}, </if> <if test="item.shic != null and item.shic !=''"> SHIC=#{item.shic,jdbcType=DECIMAL}, </if> <if test="item.startriq != null and item.startriq !=''"> STARTRIQ=#{item.startriq,jdbcType=TIMESTAMP}, </if> </trim> where ZAIXCSID = #{item.zaixcsid,jdbcType=VARCHAR} </foreach> ;end; </update>

2、mysql寫法:

註:mysql資料庫連接需要加上&allowMultiQueries=true 例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true <update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update ZAIXCS <trim prefix="set" suffixOverrides=","> <if test="item.mingc != null and item.mingc !=''"> MINGC= #{item.mingc,jdbcType=VARCHAR}, </if> <if test="item.shic != null and item.shic !=''"> SHIC=#{item.shic,jdbcType=DECIMAL}, </if> <if test="item.startriq != null and item.startriq !=''"> STARTRIQ=#{item.startriq,jdbcType=TIMESTAMP}, </if> <if test="item.beiz != null and item.beiz !=''"> BEIZ=#{item.beiz,jdbcType=VARCHAR}, </if> </trim> where ZAIXCSID = #{item.zaixcsid,jdbcType=VARCHAR} </foreach> </update>