class JdbcBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {

	protected static final Log logger = LogFactory.getLog(JdbcBatchItemWriter.class);

	protected NamedParameterJdbcOperations namedParameterJdbcTemplate;

	protected ItemPreparedStatementSetter<T> itemPreparedStatementSetter;

	protected ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider;

	protected String sql;

	protected boolean assertUpdates = true;

	protected int parameterCount;

	protected boolean usingNamedParameters;

	/**
	 * Public setter for the flag that determines whether an assertion is made that all
	 * items cause at least one row to be updated.
	 * @param assertUpdates the flag to set. Defaults to true;
	 */
	public void setAssertUpdates(boolean assertUpdates) {
		this.assertUpdates = assertUpdates;
	}

	/**
	 * Public setter for the query string to execute on write. The parameters should
	 * correspond to those known to the {@link ItemPreparedStatementSetter}.
	 * @param sql the query to set
	 */
	public void setSql(String sql) {
		this.sql = sql;
	}

	/**
	 * Public setter for the {@link ItemPreparedStatementSetter}.
	 * @param preparedStatementSetter the {@link ItemPreparedStatementSetter} to set. This
	 * is required when using traditional '?' placeholders for the SQL statement.
	 */
	public void setItemPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter) {
		this.itemPreparedStatementSetter = preparedStatementSetter;
	}

	/**
	 * Public setter for the {@link ItemSqlParameterSourceProvider}.
	 * @param itemSqlParameterSourceProvider the {@link ItemSqlParameterSourceProvider} to
	 * set. This is required when using named parameters for the SQL statement and the
	 * type to be written does not implement {@link Map}.
	 */
	public void setItemSqlParameterSourceProvider(ItemSqlParameterSourceProvider<T> itemSqlParameterSourceProvider) {
		this.itemSqlParameterSourceProvider = itemSqlParameterSourceProvider;
	}

	/**
	 * Public setter for the data source for injection purposes.
	 * @param dataSource {@link javax.sql.DataSource} to use for querying against
	 */
	public void setDataSource(DataSource dataSource) {
		if (namedParameterJdbcTemplate == null) {
			this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
		}
	}

	/**
	 * Public setter for the {@link NamedParameterJdbcOperations}.
	 * @param namedParameterJdbcTemplate the {@link NamedParameterJdbcOperations} to set
	 */
	public void setJdbcTemplate(NamedParameterJdbcOperations namedParameterJdbcTemplate) {
		this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
	}

	/**
	 * Check mandatory properties - there must be a NamedParameterJdbcOperations and an
	 * SQL statement plus a parameter source.
	 */
	@Override
	public void afterPropertiesSet() {<FILL_FUNCTION_BODY>}

	@SuppressWarnings("unchecked")
	@Override
	public void write(final Chunk<? extends T> chunk) throws Exception {

		if (!chunk.isEmpty()) {

			if (logger.isDebugEnabled()) {
				logger.debug("Executing batch with " + chunk.size() + " items.");
			}

			int[] updateCounts;

			if (usingNamedParameters) {
				if (chunk.getItems().get(0) instanceof Map && this.itemSqlParameterSourceProvider == null) {
					updateCounts = namedParameterJdbcTemplate.batchUpdate(sql,
							chunk.getItems().toArray(new Map[chunk.size()]));
				}
				else {
					SqlParameterSource[] batchArgs = new SqlParameterSource[chunk.size()];
					int i = 0;
					for (T item : chunk) {
						batchArgs[i++] = itemSqlParameterSourceProvider.createSqlParameterSource(item);
					}
					updateCounts = namedParameterJdbcTemplate.batchUpdate(sql, batchArgs);
				}
			}
			else {
				updateCounts = namedParameterJdbcTemplate.getJdbcOperations()
					.execute(sql, (PreparedStatementCallback<int[]>) ps -> {
						for (T item : chunk) {
							itemPreparedStatementSetter.setValues(item, ps);
							ps.addBatch();
						}
						return ps.executeBatch();
					});
			}

			if (assertUpdates) {
				for (int i = 0; i < updateCounts.length; i++) {
					int value = updateCounts[i];
					if (value == 0) {
						throw new EmptyResultDataAccessException("Item " + i + " of " + updateCounts.length
								+ " did not update any rows: [" + chunk.getItems().get(i) + "]", 1);
					}
				}
			}

			processUpdateCounts(updateCounts);
		}
	}

	/**
	 * Extension point to post process the update counts for each item.
	 * @param updateCounts the array of update counts for each item
	 * @since 5.1
	 */
	protected void processUpdateCounts(int[] updateCounts) {
		// No Op
	}

}

		Assert.state(namedParameterJdbcTemplate != null, "A DataSource or a NamedParameterJdbcTemplate is required.");
		Assert.state(sql != null, "An SQL statement is required.");
		List<String> namedParameters = new ArrayList<>();
		parameterCount = JdbcParameterUtils.countParameterPlaceholders(sql, namedParameters);
		if (namedParameters.size() > 0) {
			if (parameterCount != namedParameters.size()) {
				throw new InvalidDataAccessApiUsageException(
						"You can't use both named parameters and classic \"?\" placeholders: " + sql);
			}
			usingNamedParameters = true;
		}
		if (!usingNamedParameters) {
			Assert.state(itemPreparedStatementSetter != null,
					"Using SQL statement with '?' placeholders requires an ItemPreparedStatementSetter");
		}
	
<no_super_class>