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