在项目中遇到需要批量更新的功能,原本想的是在Java中用循环访问数据库去更新,但是心里总觉得这样做会不会太频繁了,太耗费资源了,效率也很低,查了下mybatis的批量操作,原来确实有<foreach>标签可以做到。
dao 层接口:
?
1
2
3
4
5
|
public class Demo{
private int id;
private String name;
private String sex;
}
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<pre name= "code" class = "html" > public int update( @Param ( "list" ) List<Demo> list);</pre><br>
<br>
<p></p>
<pre></pre>
<br>
xml 文件:
<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>以上这种做法适用情况是:根据传入的List参数集合中的每一个id遍历去更新指定字段。。</p>
<p><br>
</p>
<p>其中:</p>
<p> 1 .collection 中要对应接口中集合的名称</p>
<p> 2 .item 是集合的别名</p>
<p><br>
</p>
|
以上所述是小编给大家介绍的Mybatis 中的sql批量修改方法实现,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://blog.csdn.net/sinat_34864196/article/details/54703210
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 70
-
2025-06-04 43
-
2025-05-27 51
-
2025-06-04 22
-
2025-05-29 84
热门评论