mybatis批量更新的两种实现方式(转)

一:当要更新的内容是不样的

mapper.xml文件,后台传入一个对象集合,另外如果是mysql数据库,一点在配置文件上加上&allowMultiQueries=true,这样才可以执行多条sql,以下为mysql

 

[html]  view plain  copy
 
  1. <update id="batchUpdate" parameterType="java.util.List">   
  2.       <foreach separator=";" index="index" item="item" collection="list" close="" open="">   
  3.       update sys_group set level = #{item.level,jdbcType=INTEGER}  
  4.        where group_id = #{item.groupId,jdbcType=INTEGER}  
  5.       </foreach>   
  6.   </update>  

 

如果是oracle数据库则写法有不同:

 

[html]  view plain  copy
 
  1. <update id="batchUpdateRepayPlan" parameterType="java.util.List">  
  2.      begin     
  3.       <foreach separator=";" index="index" item="item" collection="list" close="" open="">     
  4.       update t_ba_repay_plan   
  5.       <set>  
  6.        REPAY_INTEREST = #{item.interest}  
  7.       </set>    
  8.        where IOU_CODE = #{item.iouCode}    
  9.       </foreach>   
  10.       ;end;   
  11. </update>    



 

 

二:当更新的内容是一样的

mapper.xml文件,后台传入一个int集合,这是mysql版本

 

[html]  view plain  copy
 
  1. <update id="batchUpdate1" parameterType="java.util.List">   
  2.        
  3.       update sys_group set level = null where level in  
  4.        <foreach separator="," index="index" item="item" collection="list" close=")" open="(">   
  5.          #{item}  
  6.       </foreach>   
  7.         
  8.   </update>  

orcale版本同上。

猜你喜欢

转载自frank1998819.iteye.com/blog/2422650
今日推荐