当前位置:   article > 正文

MybatisPlus根据特定条件批量更新指定字段_querywrapper批量更新

querywrapper批量更新

先上代码

  1. private boolean updateBatchByQueryWrapper(Collection<Route> entityList, Function<Route, QueryWrapper> queryWrapperFunction) {
  2. String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE);
  3. System.out.println("sqlStatement = " + sqlStatement);
  4. return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
  5. MapperMethod.ParamMap param = new MapperMethod.ParamMap();
  6. param.put(Constants.ENTITY, entity);
  7. param.put(Constants.WRAPPER, queryWrapperFunction.apply(entity));
  8. System.out.println("sqlStatement = " + sqlStatement);
  9. sqlSession.update(sqlStatement, param);
  10. });
  11. }
  12. @Override
  13. public void test() {
  14. Route route = new Route();
  15. route.setParentIndexes("1,2,3");
  16. route.setProductId(955);
  17. route.setId(378);
  18. Route route2 = new Route();
  19. route2.setParentIndexes("1,2");
  20. route2.setId(379);
  21. List<Route> list = new ArrayList<>(2);
  22. list.add(route);list.add(route2);
  23. this.updateBatchByQueryWrapper(list,route3->new QueryWrapper<>().eq("id",route3.getId()));
  24. }

乍一看看感觉没问题,但是如果Route bean中有

@TableField(updateStrategy = FieldStrategy.IGNORED)

那么null值,直接更新了,这明显就出bug了,但是又不能取消,因为确实有场景要用。

查了相关资料,可用  on duplicate  也可以用 foreach 并开启&allowMultiQueries=true ,也可以自定义 SQL注入器扩展

结合老项目实际情况。选择foreach 上代码

  1. <update id="updateBatch" parameterType="java.util.List">
  2. <foreach collection="list" item="item" index="index" open="" close="" separator=";">
  3. update route
  4. <set>
  5. parent_indexes=#{item.parentIndexes}
  6. </set>
  7. where id = ${item.id}
  8. </foreach>
  9. </update>

 

 结果

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号