Mybatis批量修改

来源:互联网 发布:孙尚香铭文 知乎 编辑:程序博客网 时间:2024/05/02 05:04

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指定的内容。
原创粉丝点击