当前位置:主页 > 软件编程 > JAVA代码 >

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

时间:2020-11-08 12:56:46 | 栏目:JAVA代码 | 点击:

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&amp;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> 

您可能感兴趣的文章:

相关文章