Mybatis 中的sql批量修改方法实现
在项目中遇到需要批量更新的功能,原本想的是在Java中用循环访问数据库去更新,但是心里总觉得这样做会不会太频繁了,太耗费资源了,效率也很低,查了下mybatis的批量操作,原来确实有<foreach>标签可以做到。
dao 层接口:
publicclassDemo{
privateintid;
privateStringname;
privateStringsex;
}
<prename="code"class="html">publicintupdate(@Param("list")List<Demo>list);</pre><br>
<br>
<p></p>
<pre></pre>
<br>
xml文件:
<p></p>
<p><updateid="update"parameterType="java.util.List"><br>
</p>
<p>updatebpm_infosetmessage_id=1whereidin<br>
<spanstyle="white-space:pre"></span><foreachcollection="list"index="index"item="item"open="("separator=","close=")"><br>
<spanstyle="white-space:pre"></span>#{item.id}<br>
<spanstyle="white-space:pre"></span></foreach><br>
</update><br>
</p>
<p><br>
</p>
<p>以上这种做法适用情况是:根据传入的List参数集合中的每一个id遍历去更新指定字段。。</p>
<p><br>
</p>
<p>其中:</p>
<p>1.collection中要对应接口中集合的名称</p>
<p>2.item是集合的别名</p>
<p><br>
</p>
以上所述是小编给大家介绍的Mybatis中的sql批量修改方法实现,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!