欢迎来到代码驿站!

JAVA代码

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

Mybatis批量修改的操作代码

时间:2021-10-19 08:56:53|栏目:JAVA代码|点击:

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指定的内容。

总结

上一篇:SpringBoot+Tess4j实现牛逼的OCR识别工具的示例代码

栏    目:JAVA代码

下一篇:java实现合并2个文件中的内容到新文件中

本文标题:Mybatis批量修改的操作代码

本文地址:http://www.codeinn.net/misctech/180515.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有