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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM