The sql batch modification method implementation in Mybatis

  • 2020-06-01 09:46:57
  • OfStack

When I encountered the function of batch update in the project, I originally wanted to use the database to update in Java through cyclic access, but I always thought that it would be too frequent, too resource-consuming and inefficient. I checked the batch operation of mybatis and found that there was indeed such a function < foreach > Tags can do that.

dao layer interface:


public class Demo{ 
  private int id; 
  private String name; 
  private String sex; 
}

<pre name="code" class="html">public int update(@Param("list") List<Demo> list);</pre><br> 
<br> 
<p></p> 
<pre></pre> 
<br> 
xml  File:  
<p></p> 
<p><update id="update" parameterType="java.util.List"><br> 
</p> 
<p>update bpm_info set message_id= 1 where id in <br> 
  <span style="white-space:pre"></span><foreach collection="list" index="index" item="item" open="(" separator="," close=")"><br> 
  <span style="white-space:pre"></span>#{item.id}<br> 
  <span style="white-space:pre"></span></foreach><br> 
</update><br> 
</p> 
<p><br> 
</p> 
<p> This applies when: according to the incoming List Each of the parameters in the set 1 a id Iterate to update the specified field. </p> 
<p><br> 
</p> 
<p> Among them: </p> 
<p>1.collection  To correspond to the name of the collection in the interface </p> 
<p>2.item  Is the alias for the collection </p> 
<p><br> 
</p> 

Related articles: