`
WChao226
  • 浏览: 27302 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

mybatis批量更新

阅读更多
List<TbDepartment>将整个集合的对象根据对应的ID进行更新到tb_department表。

mapper.xml
<!-- 批量更新After表数据 -->
<update id="updateBatch"  parameterType="java.util.List">
   update TB_DEPARTMENT 
   <trim prefix="set" suffixOverrides=",">
      <trim prefix="s1 = case" suffix="end,">
      <foreach collection="list" item="i" index="index">
        when id=#{i.id,jdbcType=INTEGER} then #{i.s1,jdbcType=VARCHAR}
      </foreach>
      </trim>
      <trim prefix="s2 = case" suffix="end,">
      <foreach collection="list" item="i" index="index">
        when id=#{i.id,jdbcType=INTEGER} then #{i.s2,jdbcType=VARCHAR}
      </foreach>
      </trim>      
      <trim prefix="s3 = case" suffix="end,">
      <foreach collection="list" item="i" index="index">
        when id=#{i.id,jdbcType=INTEGER} then #{i.s3,jdbcType=VARCHAR}
      </foreach>
      </trim>
   </trim>
   where
   <foreach collection="list" separator="or" item="i" index="index" >
       id=#{i.id,jdbcType=INTEGER}
   </foreach>
</update>


java调用代码:
List<TbDepartment> list = new ArrayList<TbDepartment>();
sqlSessionTemplate.update("updateBatch", list);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics