mybatis操作oracle数据库通过入参List实现批量新增和修改

批量新增

注意:由于是oracle 数据库,批量新增的时候和其他数据不一样,批量新增的时候必须遍历查询通过 UNION ALL 连接成临时表再进行批量添加,我这里入参是List<Map<String,Object>>

<insert id="insertBatch" parameterType="java.util.List" useGeneratedKeys="false">
        insert into phf_budget(pk_id,fk_report_record_id,subject_id,budget_number,unit_price,money_total,creator_user_id)
        select *
        from(
        <foreach collection="list" item="map" index="index" separator="UNION ALL">
            SELECT
                SYS_GUID() as pk_id,
                <choose>
                    <when test="fkReportRecordId != null">
                        #{fkReportRecordId}
                    </when>
                    <otherwise>
                        ''
                    </otherwise>
                </choose>
                as fk_report_record_id,
                <choose>
                    <when test="map.subjectId != null">
                        #{map.subjectId}
                    </when>
                    <otherwise>
                        ''
                    </otherwise>
                </choose>
                as subject_id,
                <choose>
                    <when test="map.budgetNumber != null and map.budgetNumber != ''">
                        #{map.budgetNumber}
                    </when>
                    <otherwise>
                        '0'
                    </otherwise>
                </choose>
                as budget_number,
                <choose>
                    <when test="map.unitPrice != null and map.unitPrice != null">
                        #{map.unitPrice}
                    </when>
                    <otherwise>
                        '0'
                    </otherwise>
                </choose>
                as unit_price,
                <choose>
                    <when test="map.moneyTotal != null and map.moneyTotal != null">
                        #{map.moneyTotal}
                    </when>
                    <otherwise>
                        '0'
                    </otherwise>
                </choose>
                as money_total,
                <choose>
                    <when test="userId != null">
                        #{userId}
                    </when>
                    <otherwise>
                        ''
                    </otherwise>
                </choose>
                as creator_user_id
            from dual
        </foreach>
        )
    </insert>

批量更新

orcale 批量更新的时候在<foreach>标签未必须加 begin --- end

<update id="updateBatch" parameterType="java.util.List">
        <if test="list!=null">
            begin
            <foreach collection="list" item="item" index= "index" open="" close="" separator =";">
                update phf_budget
                <set>
                    <if test="item.budgetNumber != null">budget_number = #{item.budgetNumber},</if>
                    <if test="item.unitPrice != null">unit_price = #{item.unitPrice},</if>
                    <if test="item.moneyTotal != null">money_total = #{item.moneyTotal},</if>
                    last_modification_time = SYSDATE,
                    <if test="userId != null">last_modifier_user_id = #{userId},</if>
                </set>
                <where>
                    pk_id=#{item.pkId}
                </where>
            </foreach>
            ;end;
        </if>
    </update>

 

posted @ 2021-08-18 15:09  过氧化氢  阅读(601)  评论(0编辑  收藏  举报