@Override public void batchInsert(List<E> entities, int batchSize) { if (CollectionUtils.isEmpty(entities)) { return; } for (int i = 0, l = entities.size(); i < l; i += batchSize) { List<SqlParameterSource> paramSourceList = createBatchSqlParameterSource( entities.subList(i, Math.min(i + batchSize, l))); jdbcInsert.executeBatch(paramSourceList.toArray(new SqlParameterSource[] {})); } }
public int[] batchProcess(List<Map<String, Object>> batchArgs) { Map<String, Object>[] batch = new Map[batchArgs.size()]; for (int i = 0; i < batch.length; i++) { Map<String, Object> params = batchArgs.get(i); for (String keyName : generateKeys) { params.put(keyName, getKeyValue(keyName)); } batch[i] = params; } return simpleJdbcInsert.executeBatch(batch); }
@Override public List<Integer> insertProjects(List<Project> projects) { MapSqlParameterSource[] paramsList = projects .stream() .map(project -> new MapSqlParameterSource() .addValue("name", project.getName()) .addValue("datestarted", project.getDate())) .toArray(MapSqlParameterSource[]::new); // NOTE: Here we used SimpleJdbcInsert to insert the rows, but you can also use more traditional way // by specifying the statment to be executed (handy when you need to do some customizations of the statement). // See insertDepartments() method for an example. insertIntoProject.executeBatch(paramsList); // FIXME JDBCTemplate cannot return IDs for every row after batch update!! return null; }
public <T> int[] batchProcess(List<T> batchArgs, Class<T> requiredType) { SqlParameterSource[] batch = new SqlParameterSource[batchArgs.size()]; for (int i = 0; i < batch.length; i++) { T object = batchArgs.get(i); BeanWrapper bw = PropertyAccessorFactory .forBeanPropertyAccess(object); initBeanWrapper(bw); for (String keyName : generateKeys) { Object value = getKeyValue(keyName); String propertyName = getPropertyName(keyName, requiredType); bw.setPropertyValue(propertyName, value); } batch[i] = new BeanPropertySqlParameterSource(bw.getWrappedInstance()); } return simpleJdbcInsert.executeBatch(batch); }