@Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection<T> entityList, int batchSize) { Assert.notEmpty(entityList, "error: entityList must not be empty"); String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID); try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; for (T anEntityList : entityList) { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, anEntityList); batchSqlSession.update(sqlStatement, param); if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } return true; }
@Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) { Assert.notEmpty(entityList, "error: entityList must not be empty"); Class<?> cls = currentModelClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; for (T entity : entityList) { Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty); if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) { batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity); } else { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param); } // 不知道以后会不会有人说更新失败了还要执行插入 😂😂😂 if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } return true; }
/** * 根据主键ID进行批量修改 * * @param entityList 实体对象列表 * @param batchSize 批量刷新个数 * @param selective 是否滤掉空字段 * @return boolean */ private boolean updateBatchById(List<T> entityList, int batchSize, boolean selective) { if (com.baomidou.mybatisplus.toolkit.CollectionUtils.isEmpty(entityList)) { throw new IllegalArgumentException("Error: entityList must not be empty"); } try (SqlSession batchSqlSession = sqlSessionBatch()) { int size = entityList.size(); SqlMethod sqlMethod = selective ? SqlMethod.UPDATE_BY_ID : SqlMethod.UPDATE_ALL_COLUMN_BY_ID; String sqlStatement = sqlStatement(sqlMethod); for (int i = 0; i < size; i++) { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put("et", entityList.get(i)); batchSqlSession.update(sqlStatement, param); if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } } batchSqlSession.flushStatements(); } catch (Throwable e) { throw new MybatisPlusException("Error: Cannot execute updateBatchById Method. Cause", e); } return true; }
@Override @Transactional public boolean saveOrUpdateBatch(Collection<Resource> entityList) { //批量对象插入 不存在直接返回true if (CollectionUtils.isEmpty(entityList)) { return true; } Map<String, Resource> resourceMap = list2Map(Resource::getId); int i = 0; try (SqlSession batchSqlSession = sqlSessionBatch()) { for (Resource entity : entityList) { String idVal = entity.getId(); Resource resource = resourceMap.get(idVal); if (Objects.nonNull(resource)) { entity.setUpdateTime(resource.getUpdateTime()); MapperMethod.ParamMap<Resource> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param); } else { batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity); } if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } return true; }
} else { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param);
@Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) { Assert.notEmpty(entityList, "error: entityList must not be empty"); Class<?> cls = currentModelClass(); TableInfo tableInfo = TableInfoHelper.getTableInfo(cls); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; for (T entity : entityList) { Object idVal = ReflectionKit.getMethodValue(cls, entity, keyProperty); if (StringUtils.checkValNull(idVal) || Objects.isNull(getById((Serializable) idVal))) { batchSqlSession.insert(sqlStatement(SqlMethod.INSERT_ONE), entity); } else { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); batchSqlSession.update(sqlStatement(SqlMethod.UPDATE_BY_ID), param); } //不知道以后会不会有人说更新失败了还要执行插入 😂😂😂 if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } return true; }
@SuppressWarnings("unchecked") private void handleParamMap(ParamMap parameter, IdColumn idColumn, LastModifiedByColumn modifiedByColumn, LastModifiedDateColumn modifiedDateColumn, DisabledDateColumn disabledDateColumn) { // 修改参数 ParamMap<Object> paramMap = (ParamMap<Object>) parameter; Object id = parameter.get("arg0"); Object modifiedBy = parameter.get("arg1"); paramMap.remove("arg0"); paramMap.remove("arg1"); paramMap.remove("param1"); paramMap.remove("param2"); LocalDateTime now = LocalDateTime.now(); if (modifiedByColumn != null) { paramMap.put(modifiedByColumn.getProperty(), modifiedBy); } if (modifiedDateColumn != null) { paramMap.put(modifiedDateColumn.getProperty(), now); } if (disabledDateColumn != null) { paramMap.put(disabledDateColumn.getProperty(), now); } paramMap.put(idColumn.getProperty(), id); } }
@Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection<T> entityList, int batchSize) { //批量对象插入 不存在直接返回true if (CollectionUtils.isEmpty(entityList)) { return true; } int i = 0; String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID); try (SqlSession batchSqlSession = sqlSessionBatch()) { for (T anEntityList : entityList) { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, anEntityList); batchSqlSession.update(sqlStatement, param); if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } return true; }
@Transactional(rollbackFor = Exception.class) @Override public boolean updateBatchById(Collection<T> entityList, int batchSize) { Assert.notEmpty(entityList, "error: entityList must not be empty"); String sqlStatement = sqlStatement(SqlMethod.UPDATE_BY_ID); try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; for (T anEntityList : entityList) { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, anEntityList); batchSqlSession.update(sqlStatement, param); if (i >= 1 && i % batchSize == 0) { batchSqlSession.flushStatements(); } i++; } batchSqlSession.flushStatements(); } return true; }