【转】oracle、mysql批量操作

转自:https://blog.csdn.net/lwpczy1/article/details/79158535

mybatis映射中可以通过<foreach></foreach>标签来实现Oracle的批量插入、更新和删除
    <foreach>标签中主要有以下属性:
    collection、item、index、open、separate、close
    collection:该属性必须指定,指代Dao层接口传递的数据类型,主要有三种:
        ①:list集合类型;collection=”list“
        ②:array数组类型;collection=”array“
        ③:map映射类型;collection=”map“
    item:别名,表示集合中每一个元素迭代时的别名,获取数据时必须指定用别名来指定,不然会报错。
    index:迭代下标,即迭代过程中的位置。
    open:表示语句以什么开始。
    separate:表示每次迭代之间以什么符号作为分割。
    close:表示语句以什么结束。

一、批量插入
    Oracle中可以使用java中的for循环逐条插入数据库,但是这种效率比较低,不适合一次性插入大量的数据,所以可以利用Oracle中的“dual”表    实现批量处理,并且效率高
    比如有这样一张表

1、collection为list类型

实例

方式1:
  <insert id="insertclobtest2" parameterType="com.inspur.tax.sjaqgl.sjflfjgl.data.ClobEntity">
        INSERT ALL
        <foreach collection="list" item="item" index="index">
            INTO ainsertclob (id, blobtest) VALUES
            (#{item.id}, #{item.blobtest})
        </foreach>
        select 1 from dual
 
    </insert>
方式2:
 
    <insert id="insertclobtest3" parameterType="com.inspur.tax.sjaqgl.sjflfjgl.data.ClobEntity">
 
        INSERT INTO ainsertclob (id, blobtest)
        <foreach collection="list" item="item" index="index" separator="union all">
            SELECT #{item.id} ,#{item.blobtest} from dual
        </foreach>
    </insert>

二、批量更新
    1、collection为list类型 

或者

 /**
     * @Desc :  批量更新大批量子订单详情信息
     * @Author : ZRP 
     * @Date : 2018/1/26 15:24
     */
    int batchUpdateSplitSinglePickCurrency(@Param(value = "list") List<MultiOrderCurrency> list) throws Exception;


<update id="batchUpdateSplitSinglePickCurrency" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
            UPDATE ZC_TR_MULTI_ORDER_CURRENCY
            <set>
                <if test="item.sysCorderCode != null">
                    SYS_CORDER_CODE = #{item.sysCorderCode,jdbcType=VARCHAR},
                </if>

                <if test="item.sysPorderCode != null">
                    SYS_PORDER_CODE = #{item.sysPorderCode,jdbcType=VARCHAR},
                </if>

                <if test="item.bizPorderCode != null">
                    BIZ_PORDER_CODE = #{item.bizPorderCode,jdbcType=VARCHAR},
                </if>

                <if test="item.originalOrderCode != null">
                    ORIGINAL_ORDER_CODE = #{item.originalOrderCode,jdbcType=VARCHAR},
                </if>

                <if test="item.splitUserId != null">
                    SPLIT_USER_ID = #{item.splitUserId,jdbcType=VARCHAR},
                </if>

                <if test="item.createDate != null">
                    CREATE_DATE = #{item.createDate},
                </if>

                <if test="item.updateDate != null">
                    UPDATE_DATE = #{item.updateDate},
                </if>
            </set>
            where id = #{item.id,jdbcType=VARCHAR}
        </foreach>
    </update>

 需要注意的地方:

<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">

如果是mysql数据库 

数据库连接必须配置:&allowMultiQueries=true(切记一定要加上这个属性,否则会有问题,切记!切记!切记!)

我的配置如下:jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true
/**
     * 批量修改赛程
     * 
     * @param matchs
     * @throws DaoException
     */
    void updateMatchs(@Param(value = "matchs")List<MatchBasic> matchs);


<!-- 批量更新 -->
    <update id="updateMatchs" parameterType="java.util.List">
        <foreach collection="matchs" item="item" index="index" open="" close="" separator=";">
            update t_match
            <set>
                <if test="item.title !=null">
                    TITLE = #{item.title,jdbcType=VARCHAR},
                </if>
                <if test="item.homeScore !=null">
                    HOME_SCORE = #{item.homeScore,jdbcType=INTEGER},
                </if>
                <if test="item.visitScore !=null">
                    VISTT_SCORE = #{item.visitScore,jdbcType=INTEGER},
                </if>
                <if test="item.liveSource !=null">
                    LIVE_SOURCE = #{item.liveSource,jdbcType=VARCHAR},
                </if>
                <if test="item.liveURL !=null">
                    LIVE_URL = #{item.liveURL,jdbcType=VARCHAR},
                </if>
                <if test="item.isHotMatch !=null">
                    IS_HOT_MATCH = #{item.isHotMatch,jdbcType=VARCHAR}
                </if>
            </set>
        where HOME_TEAM_ID = #{item.homeTeamId,jdbcType=VARCHAR} and
        VISIT_TEAM_ID = #{item.visitTeamId,jdbcType=VARCHAR} and
        MATCH_TIME = #{item.matchTime,jdbcType=BIGINT}
        </foreach>
    </update>

 

三、批量删除
    1、collection为array类型 

注意画圈的部分。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值