admin

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

admin web技术 1970-01-01 833浏览 0
注:本文主要用来记录oracle和mysql数据库在使用mybatis的情况下批量增、删、改(更新)的常用写法

一、批量插入

1、oracle写法:

<insert >
        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 >
        delete from emp where empno in
        <foreach item=”empnoItem” collection=”array” open=”(” separator=”,” close=”)”>
            #{empnoItem}
        </foreach>
    </delete>

2、删除list列表数据

 <delete >
        delete from emp where empno in
        <foreach item=”item” collection=”list” open=”(” separator=”,” close=”)”>
            #{item}
        </foreach>
    </delete>

3、删除查询到的数据

<delete >
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写法:

发表评论