Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2006-2008 the original author or authors.
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package org.springframework.batch.item.database;
 import java.util.List;
 import  com.ibatis.sqlmap.client.SqlMapClient;
 import  com.ibatis.sqlmap.client.SqlMapExecutor;
 import  com.ibatis.sqlmap.engine.execution.BatchException;
 import  com.ibatis.sqlmap.engine.execution.BatchResult;

ItemWriter that uses the batching features from SqlMapClientTemplate to execute a batch of statements for all items provided.
The user must provide an iBATIS statement id that points to the SQL statement defined in the iBATIS SqlMap configuration.
It is expected that write(List) is called inside a transaction.
The writer is thread safe after its properties are set (normal singleton behavior), so it can be used to write in multiple concurrent transactions.

Thomas Risberg
 public class IbatisBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
 	protected static final Log logger = LogFactory.getLog(IbatisBatchItemWriter.class);
 	private String statementId;
 	private boolean assertUpdates = true;

Public setter for the flag that determines whether an assertion is made that all items cause at least one row to be updated.

assertUpdates the flag to set. Defaults to true;
 	public void setAssertUpdates(boolean assertUpdates) {
 		this. = assertUpdates;

Public setter for SqlMapClient for injection purposes.

sqlMapClient the SqlMapClient
 	public void setSqlMapClient(SqlMapClient sqlMapClient) {
 		if ( == null) {
 			this. = new SqlMapClientTemplate(sqlMapClient);

Public setter for the SqlMapClientTemplate.

sqlMapClientTemplate the SqlMapClientTemplate
 	public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
 		this. = sqlMapClientTemplate;

Public setter for the statement id identifying the statement in the SqlMap configuration file.

statementId the id for the statement
 	public void setStatementId(String statementId) {
 		this. = statementId;

Check mandatory properties - there must be an SqlMapClient and a statementId.
	public void afterPropertiesSet() {
		Assert.notNull("A SqlMapClient or a SqlMapClientTemplate is required.");
		Assert.notNull("A statementId is required.");
	/* (non-Javadoc)
	 * @see org.springframework.batch.item.ItemWriter#write(java.util.List)
	public void write(final List<? extends T> items) {
		if (!items.isEmpty()) {
				.debug("Executing batch with " + items.size() + " items.");
			List<BatchResult> results = (List<BatchResult>) .execute(
						public Object doInSqlMapClient(SqlMapExecutor executor)
								throws SQLException {
							for (T item : items) {
							try {
								return executor.executeBatchDetailed();
catch (BatchException e) {
								throw e.getBatchUpdateException();
				if (results.size() != 1) {
					throw new InvalidDataAccessResourceUsageException("Batch execution returned invalid results. " +
							"Expected 1 but number of BatchResult objects returned was " + results.size());
				int[] updateCounts = results.get(0).getUpdateCounts();  
				for (int i = 0; i < updateCounts.lengthi++) {
					int value = updateCounts[i];
					if (value == 0) {
						throw new EmptyResultDataAccessException("Item " + i + " of " + updateCounts.length
" did not update any rows: [" + items.get(i) + "]", 1);
New to GrepCode? Check out our FAQ X