当前位置:   article > 正文

mybatis oracle批量update和insert

mybatis oracle批量update

由于oracle语法和mysql有区别,mybatis动态sql对oracle的批量新增和修改需要注意与mysql语句的区别。

mysql批量新增:

  1. <insert id="stockAdd" parameterType="java.util.List">
  2. insert into
  3. fk_tpledgepoolstock
  4. (pool_id,
  5. pool_type,
  6. company_id,
  7. inter_code,
  8. add_date,
  9. add_time,
  10. begin_date,
  11. end_date,
  12. operator_no,
  13. source_flag,
  14. remark)
  15. values
  16. <foreach collection="list" item="item" index="index" separator=",">
  17. (
  18. #{item.poolId,jdbcType=INTEGER},
  19. #{item.poolType,jdbcType=VARCHAR},
  20. #{item.companyId,jdbcType=INTEGER},
  21. #{item.interCode,jdbcType=INTEGER},
  22. IFNULL(#{item.addDate,jdbcType=INTEGER}, 0),
  23. IFNULL(#{item.addTime,jdbcType=INTEGER}, 0),
  24. IFNULL(#{item.beginDate,jdbcType=INTEGER}, 0),
  25. IFNULL(#{item.endDate,jdbcType=INTEGER}, 0),
  26. #{item.operatorNo,jdbcType=INTEGER},
  27. IFNULL(#{item.sourceFlag,jdbcType=VARCHAR}, ''),
  28. IFNULL(#{item.remark,jdbcType=VARCHAR}, '')
  29. )
  30. </foreach>
  31. </insert>

oracle批量新增:

  1. <insert id="stockAdd" parameterType="java.util.List" databaseId="oracle">
  2. insert into
  3. fk_tpledgepoolstock
  4. (pool_id,
  5. pool_type,
  6. company_id,
  7. inter_code,
  8. add_date,
  9. add_time,
  10. begin_date,
  11. end_date,
  12. operator_no,
  13. source_flag,
  14. remark)
  15. select * from
  16. (<foreach collection="list" item="item" index="index" separator="union all">
  17. select
  18. NVL(#{item.poolId,jdbcType=INTEGER},0) as pool_id,
  19. NVL(#{item.poolType,jdbcType=VARCHAR},' ') as pool_type,
  20. NVL(#{item.companyId,jdbcType=INTEGER},0) as company_id,
  21. NVL(#{item.interCode,jdbcType=INTEGER},0) as inter_code,
  22. NVL(#{item.addDate,jdbcType=INTEGER},0) as add_date,
  23. NVL(#{item.addTime,jdbcType=INTEGER},0) as add_time,
  24. NVL(#{item.beginDate,jdbcType=INTEGER},0) as begin_date,
  25. NVL(#{item.endDate,jdbcType=INTEGER},0) as end_date,
  26. NVL(#{item.operatorNo,jdbcType=INTEGER},0) as operator_no,
  27. NVL(#{item.sourceFlag,jdbcType=VARCHAR},' ') as source_flag,
  28. NVL(#{item.remark,jdbcType=VARCHAR},' ') as remark
  29. from dual
  30. </foreach>)tmp
  31. </insert>

mysql批量修改:

  1. <update id="poolUpdate" parameterType="java.util.List">
  2. <foreach collection="list" item="item" index="index" open="" close="" separator=";">
  3. update
  4. fk_tpledgepool
  5. <set>
  6. <if test="item.poolName != null and item.poolName != ''">
  7. pool_name = #{item.poolName},
  8. </if>
  9. <if test="item.beginDate != null and item.beginDate > 0">
  10. begin_date = #{item.beginDate},
  11. </if>
  12. <if test="item.endDate != null and item.endDate > 0">
  13. end_date = #{item.endDate},
  14. </if>
  15. </set>
  16. where
  17. company_id=#{item.companyId}
  18. and
  19. pool_id=#{item.poolId}
  20. and
  21. pool_type=#{item.poolType}
  22. </foreach>
  23. </update>

oracle批量修改:

  1. <update id="poolUpdate" parameterType="java.util.List" databaseId="oracle">
  2. <foreach collection="list" item="item" index="index" separator=";" open="begin" close=";end;">
  3. update
  4. fk_tpledgepool
  5. <set>
  6. <if test="item.poolName != null and item.poolName != ''">
  7. pool_name = NVL(#{item.poolName,jdbcType=VARCHAR}, ' '),
  8. </if>
  9. <if test="item.beginDate != null and item.beginDate > 0">
  10. begin_date = NVL(#{item.beginDate,jdbcType=INTEGER}, 0),
  11. </if>
  12. <if test="item.endDate != null and item.endDate > 0">
  13. end_date = NVL(#{item.endDate,jdbcType=INTEGER}, 0),
  14. </if>
  15. </set>
  16. where
  17. company_id=#{item.companyId}
  18. and
  19. pool_id=#{item.poolId}
  20. and
  21. pool_type=#{item.poolType}
  22. </foreach>
  23. </update>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/871256
推荐阅读
相关标签
  

闽ICP备14008679号