Mybatis批量修改的操作代码
1.修改的字段值都是一样的,id不同
<update id="batchUpdate" parameterType="String">
update cbp_order
set status=1
where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</update>
---参数说明---
collection:表示类型,就写成array,如果是集合,就写成list
item : 是一个变量名,自己随便起名
2.这种方式,可以一次执行多条SQL语句
<update id="batchUpdate" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update test
<set>
test=#{item.test}+1
</set>
where id = #{item.id}
</foreach>
</update>
3.整体批量更新
<update id="updateBatch" parameterType="java.util.List">
update mydata_table
<trim prefix="set" suffixOverrides=",">
<trim prefix="status =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.status !=null and item.status != -1">
when id=#{item.id} then #{item.status}
</if>
<if test="item.status == null or item.status == -1">
when id=#{item.id} then mydata_table.status//原数据
</if>
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=BIGINT}
</foreach>
</update>
----<trim>属性说明-------
1.prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容
2.如果同时有prefixOverrides,suffixOverrides 表示会用prefix,suffix覆盖Overrides中的内容。
3.如果只有prefixOverrides,suffixOverrides 表示删除开头的或结尾的xxxOverides指定的内容。
总结
以上所述是小编给大家介绍的Mybatis批量修改的操作代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
手把手教你用SpringBoot将文件打包成zip存放或导出
相信各位看官在工作中都会遇到过要把多个文件打包成一个压缩文件然后导出,或者将文件打包成Zip存放,这就来上代码,废话不多说,需要的朋友可以参考下2021-06-06
Springboot如何利用拦截器拦截请求信息收集到日志详解
一些系统经常需要关注用户请求的具体信息,如用户信息、请求参数、响应结果等等,在SpringBoot应用中可通过拦截器的方式统一处理,下面这篇文章主要给大家介绍了关于Springboot如何利用拦截器拦截请求信息收集到日志的相关资料,需要的朋友可以参考下2021-08-08


最新评论