欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis执行批量更新batch update 的方法(oracle,mysql两种)

程序员文章站 2024-03-09 08:37:23
oracle和mysql数据库的批量update在mybatis中配置不太一样: oracle数据库:

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

oracle数据库:

<code class="hljs tcl" style=""><<span class="hljs-keyword" style="">update</span> id=<span class="hljs-string" style="">"batchupdate"</span> parametertype=<span class="hljs-string" style="">"java.util.list"</span>> 
     <<span class="hljs-keyword" style="">foreach</span> collection=<span class="hljs-string" style="">"list"</span> item=<span class="hljs-string" style="">"item"</span> index=<span class="hljs-string" style="">"index"</span> <span class="hljs-keyword" style="">open</span>=<span class="hljs-string" style="">"begin"</span> <span class="hljs-keyword" style="">close</span>=<span class="hljs-string" style="">"end;"</span> separator=<span class="hljs-string" style="">";"</span>> 
        <span class="hljs-keyword" style="">update</span> test  
        <<span class="hljs-keyword" style="">set</span>> 
         test=${item.test}+<span class="hljs-number" style="">1</span> 
        </<span class="hljs-keyword" style="">set</span>> 
        where id = ${item.id} 
    </<span class="hljs-keyword" style="">foreach</span>> 
  </<span class="hljs-keyword" style="">update</span>></code> 

mysql数据库:

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

例如:jdbc:mysql://192.168.1.236:3306/test?useunicode=true&characterencoding=utf-8&allowmultiqueries=true

<code class="hljs tcl" style=""><<span class="hljs-keyword" style="">update</span> id=<span class="hljs-string" style="">"batchupdate"</span> parametertype=<span class="hljs-string" style="">"java.util.list"</span>> 
     <<span class="hljs-keyword" style="">foreach</span> collection=<span class="hljs-string" style="">"list"</span> item=<span class="hljs-string" style="">"item"</span> index=<span class="hljs-string" style="">"index"</span> <span class="hljs-keyword" style="">open</span>=<span class="hljs-string" style="">""</span> <span class="hljs-keyword" style="">close</span>=<span class="hljs-string" style="">""</span> separator=<span class="hljs-string" style="">";"</span>> 
        <span class="hljs-keyword" style="">update</span> test  
        <<span class="hljs-keyword" style="">set</span>> 
         test=${item.test}+<span class="hljs-number" style="">1</span> 
        </<span class="hljs-keyword" style="">set</span>> 
        where id = ${item.id} 
     </<span class="hljs-keyword" style="">foreach</span>> 
  </<span class="hljs-keyword" style="">update</span>></code> 

以上所述是小编给大家介绍的mybatis执行批量更新batch update 的方法(oracle,mysql两种),希望对大家有所帮助