当前位置: 移动技术网 > IT编程>开发语言>Java > Mybatis批量修改的操作代码

Mybatis批量修改的操作代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

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批量修改的操作代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网