@Override public List<BatchResult> flushStatements() { return session.flushStatements(); }
/** * 批量插入 * * @param entityList ignore * @param batchSize ignore * @return ignore */ @Transactional(rollbackFor = Exception.class) @Override public boolean saveBatch(Collection<T> entityList, int batchSize) { String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE); try (SqlSession batchSqlSession = sqlSessionBatch()) { int i = 0; for (T anEntityList : entityList) { batchSqlSession.insert(sqlStatement, anEntityList); 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; }
@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; }
@Override public List<BatchResult> flushOperations() { return sqlSession.flushStatements(); }
@Override public List<BatchResult> flushOperations() { return sqlSession.flushStatements(); }
public List<BatchResult> flushStatements() { return wrappedSession.flushStatements(); }
result = sqlSession.flushStatements(); break; default:
sqlSession -> when(sqlSession.flushStatements()).thenReturn(randomBatchResults), DbSessionImpl::flushStatements, sqlSession -> { verify(sqlSession).flushStatements(); return randomBatchResults; });
@Override public List<BatchResult> flushStatements() { final SqlSession sqlSession = localSqlSession.get(); if (sqlSession == null) { throw new SqlSessionException("Error: Cannot rollback. No managed session is started."); } return sqlSession.flushStatements(); }
@Override public List<BatchResult> flushStatements() { final SqlSession sqlSession = localSqlSession.get(); if (sqlSession == null) { throw new SqlSessionException("Error: Cannot rollback. No managed session is started."); } return sqlSession.flushStatements(); }
@Override public List<BatchResult> flushStatements() { return this.sqlSession.flushStatements(); }
/** * {@inheritDoc} * @since 1.0.2 */ public List<BatchResult> flushStatements() { return this.sqlSessionProxy.flushStatements(); }
@Override public List<BatchResult> flushStatements() { return this.sqlSession.flushStatements(); }
@Override public void insertDepartments(List<Department> departmentsToInsert) { DataRepositoryMapper batchSql = batchOperations.getMapper(DataRepositoryMapper.class); departmentsToInsert.forEach(batchSql::insertDepartment); // we have to flush statements here - the records would be inserted only at the end of the transaction otherwise batchOperations.flushStatements(); }
@Override public List<Integer> insertProjects(List<Project> projects) { DataRepositoryMapper batchSql = batchOperations.getMapper(DataRepositoryMapper.class); projects.forEach(batchSql::insertProject); // we have to flush statements here - the records would be inserted only at the end of the transaction otherwise batchOperations.flushStatements(); // MyBatis can actually return the PIDs from batch operation return projects.stream().map(Project::getPid).collect(Collectors.toList()); }
SqlSession session = sessionFactory.openSession(ExecutorType.BATCH); for (Model model : list) { session.insert("insertStatement", model); } session.flushStatements();
@Override public void updateDepartments(List<Department> departmentsToUpdate) { DataRepositoryMapper batchSql = batchOperations.getMapper(DataRepositoryMapper.class); departmentsToUpdate.forEach(batchSql::updateDepartment); // we have to flush statements here - the records would be updated only at the end of the transaction otherwise batchOperations.flushStatements(); }
result = sqlSession.flushStatements(); break; default:
result = sqlSession.flushStatements(); break; default: