mybatis批量增、删、改(更新)操作oracle和mysql批量写法小记

前言:用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>







posted @ 2017-02-26 19:26  eguid  阅读(862)  评论(0编辑  收藏  举报