mybatis执行批量更新batch update 的方法

533 阅读1分钟

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>

3、java 接口

void updateMatchs(@Param(value = "matchs")List matchs);

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

2、java接口

int batchUpdateSplitSinglePickCurrency(@Param(value = "list") List list) throws Exception; PS:一定要注意文中标红色的地方,今天是我犯的错误,花了我20分钟了...

oracle批量添加

<insert id="batchInsertLimsCompareInfo" parameterType="ArrayList">
    insert into 
        LIMS_COMPARE_INFO
            (ID,
            INIT_SERVER_NO,
            MATCH_GENE_TYPE,
            MATCH_MODE,
            MATCH_TIME,
            SRC_OBJECT_ID,
            MATCH_OBJECT_ID,
            COMPARE_ALGORITHM,
            MATCHED_DETAIL,
            LR_VALUE,
            REVIEW_TYPE,
            DATA_SOURCE,
            CREATE_USER,
            CREATE_DATETIME) 
    <foreach collection="list" item="item" index="index" separator="union all" >
        select 
            #{item.id,jdbcType=VARCHAR},
            #{item.initServerNo,jdbcType=VARCHAR},
            #{item.matchGeneType,jdbcType=VARCHAR},
            #{item.matchMode,jdbcType=VARCHAR},
            #{item.matchTime,jdbcType=TIMESTAMP},
            #{item.srcObjectId,jdbcType=VARCHAR},
            #{item.matchObjectId,jdbcType=VARCHAR},
            #{item.compareAlgorithm,jdbcType=VARCHAR},
            #{item.matchedDetail,jdbcType=DECIMAL},
            #{item.lrValue,jdbcType=VARCHAR},
            #{item.reviewType,jdbcType=VARCHAR},
            #{item.dataSource,jdbcType=VARCHAR},
            #{item.createUser,jdbcType=VARCHAR},
            #{item.createDatetime,jdbcType=TIMESTAMP} 
        from 
        dual 
    </foreach> 
</insert>