mybatis+oracle,mysql,sql server批处理


                     mybatis+oracle,mysql,sql server批处理

目录

1 oracle 批处理

1.1 批量插入

1.2 批量更新

2 sql server

2.1批量插入

2.2 批量更新

 3 mybatis+mysql批处理   

3.1 批量插入

3.2 批量更新

3.3 通过List ids 将表中某一列修改为固定值

    3.4 通过调用存储过程批量处理    proc_user为存储过程


1 oracle 批处理

1.1 批量插入


<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="false">  
    insert into user
            (id, name )
    <foreach collection="list" item="item" index="index" separator="union all" >  
      (select     #{item.id,jdbcType=DECIMAL},
               #{item.name,jdbcType=VARCHAR}
               from dual )
    </foreach>    
</insert>


1.2 批量更新


 <update id="batchUpdate" parameterType="java.util.List">
 <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
   update user
    <set>      
      <if test="item.name != null">
        name = #{item.name,jdbcType=VARCHAR},
      </if>
    </set>
        where  id = #{item.id,jdbcType=DECIMAL}
        </foreach>
  </update>

 

2 sql server


2.1批量插入


<insert id="batchInsert" parameterType="java.util.List">  
    insert into user
        (id,name)
    values  
    <foreach collection="list" item="item" index="index" separator="," >  
       (#{item.id},#{item.name})  
    </foreach>  
</insert>


2.2 批量更新


<update id="batchUpdate" parameterType="java.util.List">  
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">  
        UPDATE user  
            <set>
                name=#{item.name}           
            </set>  
            where id=#{item.id}  
        </foreach>  
 </update>  

 
3 mybatis+mysql批处理   


注意:连接数据库url 后面添加allowMultiQueries=true  如jdbc:mysql://localhost:3306/test?allowMultiQueries=true


3.1 批量插入

 


    <insert id="batchInsert" parameterType="java.util.List">  
        insert into user
            (id,name)
        values  
        <foreach collection="list" item="item" index="index" separator="," >  
           (#{item.id},#{item.name})  
        </foreach>  
    </insert>


3.2 批量更新


  <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open=""  close="" separator=";">
            update  user
             <set>
                name=#{item.name}  
             </set>
             where id=#{item.id}  
        </foreach>
    </update>


3.3 通过List<Integer> ids 将表中某一列修改为固定值

 


    <update id="batchUpdate" parameterType="java.util.List">        
            update  middle_process_user_closed
                set collected=1            
              where
              id
              <foreach collection="list" item="item" index="index" open="in ("  close=")" separator=",">
                #{item,jdbcType=INTEGER}             
        </foreach>
    </update>


    3.4 通过调用存储过程批量处理    proc_user为存储过程

 


    <select id="callPro" statementType="CALLABLE" resultType="java.util.Map">
      CALL proc_user()
    </select>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值