mybatis使用oracle和mysql中批量更新

一、mybatis執行批量更新batch update 的方法(mysql數據庫)

1、數據庫連接必須配置:&allowMultiQueries=true(切記一定要加上這個屬性,否則會有問題,切記!切記!切記!)
我的配置如下:jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
2、批量修改並加判斷條件(修改字段可選)
<!-- 批量更新 -->
    <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>

二、mybatis執行批量更新batch update 的方法(oracle數據庫)

1、批量修改並加判斷條件(修改字段可選)

<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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章