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

2023-11-16

批量更新数据方法(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。




本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐