mybatis批量写法

Posted 幸福在靠近

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis批量写法相关的知识,希望对你有一定的参考价值。

批量更新

<update id="updPartsStatus" parameterType="com.autosys.parts.model.BesiParts">
        <foreach collection="besiParts.partIdsList" item="item" index="index" open="begin" close=";end;" separator=";">
            UPDATE TM_BESI_PART
            <set>
                STATUS = #{besiParts.status}
            </set>
            <where>
                ID = #{item}
            </where>
        </foreach>
    </update>

批量添加

<insert id="instBesiparts" parameterType="map">
        INSERT INTO TM_BESI_PART
        (
        ID,
        PART_NO,
        MODEL_CODE,
        PART_NAME,
        PART_NAME_EN,
        PART_TYPE,
        POWER_FLAG,
        PURCASING_CODE,
        REG_PLANT,
        USE_PLANT,
        PRODUCE_CODE,
        PLANT_CODE,
        NEW_FLAG,
        STATUS,
        CREATE_BY,
        CREATE_TIME,
        UPDATE_BY,
        UPDATE_TIME
        )
        <foreach collection="partsList" item="item" index="index" separator="UNION ALL">
            SELECT
            #{item.id, jdbcType=VARCHAR},
            #{item.partNo, jdbcType=VARCHAR},
            #{item.modelCode, jdbcType=VARCHAR},
            #{item.partName, jdbcType=VARCHAR},
            #{item.partNameNn, jdbcType=VARCHAR},
            #{item.partType, jdbcType=VARCHAR},
            #{item.powerFlag, jdbcType=VARCHAR},
            #{item.purcasingCode, jdbcType=VARCHAR},
            #{item.regPlant, jdbcType=VARCHAR},
            #{item.usePlant, jdbcType=VARCHAR},
            #{item.produceCode, jdbcType=VARCHAR},
            #{item.plantCode, jdbcType=VARCHAR},
            #{item.newFlag, jdbcType=VARCHAR},
            #{item.status, jdbcType=VARCHAR},
            #{item.createBy, jdbcType=VARCHAR},
            #{item.createTime, jdbcType=VARCHAR},
            #{item.updateBy, jdbcType=VARCHAR},
            #{item.updateTime, jdbcType=VARCHAR}
            FROM DUAL
        </foreach>
    </insert>

批量Merge

<insert id="instEmParts" parameterType="com.autosys.parts.model.EmParts">
        MERGE INTO TR_PART_EMER T1 USING (
        <foreach collection="emList" item="emParts" index="index" separator="UNION ALL">
            SELECT
            #{emParts.id} as ID,
            #{emParts.partCode} as PART_CODE,
            #{emParts.model} as MODEL,
            #{emParts.userId} as USER_ID,
            #{emParts.createBy} as CREATE_BY,
            sysdate as CREATE_TIME,
            #{emParts.updateBy} as UPDATE_BY,
            sysdate as UPDATE_TIME,
            #{emParts.partId} as PART_ID
            FROM
            dual
        </foreach>
        )
        T2 ON (
        T1.PART_ID = T2.PART_ID)
        WHEN MATCHED THEN
        UPDATE SET T1.USER_ID = T2.USER_ID, T1.UPDATE_TIME = T2.UPDATE_TIME, UPDATE_BY = T2.UPDATE_BY WHERE T1.PART_ID = T2.PART_ID
        WHEN NOT MATCHED THEN
        INSERT (
        ID,
        PART_CODE,
        MODEL,
        USER_ID,
        CREATE_BY,
        CREATE_TIME,
        PART_ID
        )
        VALUES
        (
        T2.ID,
        T2.PART_CODE,
        T2.MODEL,
        T2.USER_ID,
        T2.CREATE_BY,
        T2.CREATE_TIME,
        T2.PART_ID
        )
    </insert>

以上是关于mybatis批量写法的主要内容,如果未能解决你的问题,请参考以下文章

mybatis批量update操作的写法,及批量update报错的问题解决方法

mybatis 注解写法 多层嵌套foreach,调用存储过程,批量插入数据

mybatis批量更新

mybatis的一种批量更新方法我

tk-mybatis criteria写法总结

mybaties批量修改报错