利用Mybatis怎么对数据库进行批量修改

发布时间:2020-12-07 16:46:52 作者:Leah
来源:亿速云 阅读:1339

这篇文章将为大家详细讲解有关利用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怎么对数据库进行批量修改就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. MyBatis中怎么对Spring进行整合
  2. 使用spring怎么对mybatis进行整合

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:Struts2中的异常处理机制是什么

下一篇:如何正确的使用OGNL表达式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》