preface

Mybatis has been used for several years. It is also relatively simple to write the operation of adding and deleting mybatis in batches. Only the batch update is a special hole, which is hereby recorded.

Note: This article is mainly used to record the oracle and mysql database in the case of mybatis batch add, delete, change (update) common writing method

First, batch insert

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>
Copy the code

Mysql > create 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> 
Copy the code

2. Batch delete

If collection= “array”, the table name parameter is array. When collection= “list”, the table name parameter is collection;

1, Delete array array

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

2, Delete list data

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

3. Delete the queried data

<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>
Copy the code

Third, batch update

1. Oracle

Note: This can be updated, but the update operation returns -1, does not return the number of updated rows

<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>
Copy the code

Mysql > create mysql

AllowMultiQueries =true

For example: JDBC: mysql: / / 192.168.1.236:3306 / test? useUnicode=true& 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>
Copy the code