MyBatis 采用注解方式批量更新数据 @Mapper @Update (包含2种方法)

2 篇文章 0 订阅
1 篇文章 0 订阅
批量更新数据方法(1)

//注释db_filed_name :表示的是的数据库字段名字  entity_name :表示的是你的实体字段 table_name:表示你的表名

    @Update("<script><foreach collection = 'objs' item ='item' open='' close='' separator=';'>update table_name set db_filed_name =#{item.entity_name} where  db_filed_name =#{item.entity_name} and db_filed_name=#{item.entity_name}</foreach></script>")
    int updateBatch(@Param("objs") List<Object> objs );


批量更新数据方法(2)


    @Update({"<script> update table_name  " +
            "<trim prefix ='set' prefixOverrides=',' > " +
                "<trim prefix ='db_filed_name = case' suffix='end'>" +
                    "<foreach collection ='objs' item ='item' index = 'index'> " +
                        "when db_filed_name = #{item.entity_name} then #{item.entity_name} " +
                    "</foreach>" +
                "</trim> " +
            "</trim> " +
                "where  db_filed_name in " +
            "<foreach collection ='objs' item ='items' index ='index' separator=',' open='(' close=')'  > " +
                "#{items.entity_name} " +
            "</foreach> </script>" })
    int updateBatchName(@Param("objs") List<Object> objs);


学习总结:
由于mybatis没有提供@Mapper中@Update更新数据详细demo,现自己动手写一个关于批量更新的数据方式,做参考,
此批量更新这2种sql执行效率有待测试,但是可以明确一点的是批量更新比单条更新更快(PS:亲测过),根据测试方法2效率基于平均(32条数据平均80毫秒)在unix 4核 16G mysql 5.7。




  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值