Mybatis批量更新数据

来源:互联网 发布:惊悚电影推荐知乎 编辑:程序博客网 时间:2024/05/19 00:10

第一种方式

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <update id="updateBatch" parameterType="Map">    
  2.         update aa   set     
  3.             a=#{fptm},    
  4.             b=#{csoftrain}    
  5.         where c in     
  6.         <foreach collection="cs" index="index" item="item" pen="("separator=","close=")">    
  7.             #{item}    
  8.         </foreach>  
  9. </update>  

但是这种方式修改的字段值都是一样的。

第二种方式

修改数据库连接配置:&allowMultiQueries=true

比如:jdbc:MySQL://192.168.1.236:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <update id="batchUpdate"  parameterType="java.util.List">  
  2.         
  3.           <foreach collection="list" item="item" index="index" open="" close="" separator=";">  
  4.                 update test   
  5.                 <set>  
  6.                   test=${item.test}+1  
  7.                 </set>  
  8.                 where id = ${item.id}  
  9.          </foreach>  
  10.             
  11.     </update>  

这种方式,可以一次执行多条SQL语句




oracle和mysql数据库的批量update在mybatis中配置不太一样:

oracle数据库:


<update id="batchUpdate"  parameterType="java.util.List">
     
       <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";">
                update test 
                <set>
                  test=${item.test}+1
                </set>
                where id = ${item.id}
       </foreach>
         
    </update>


mysql数据库:

    <update id="batchUpdate"  parameterType="java.util.List">
     
          <foreach collection="list" item="item" index="index" open="" close="" separator=";">
                update test 
                <set>
                  test=${item.test}+1
                </set>
                where id = ${item.id}
         </foreach>
         
    </update>


mysql数据库采用一下写法即可执行,但是数据库连接必须配置:&allowMultiQueries=true

例如:jdbc:mysql://192.168.1.236:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true

0 0
原创粉丝点击