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
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * 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 java.util.Map;
 
 

org.springframework.batch.item.ItemReader for reading database records using JDBC in a paging fashion.

It executes the SQL built by the PagingQueryProvider to retrieve requested data. The query is executed using paged requests of a size specified in AbstractPagingItemReader.setPageSize(int). Additional pages are requested when needed as org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read() method is called, returning an object corresponding to current position.

The performance of the paging depends on the database specific features available to limit the number of returned rows.

Setting a fairly large page size and using a commit interval that matches the page size should provide better performance.

The implementation is thread-safe in between calls to org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext), but remember to use saveState=false if used in a multi-threaded client (no restart available).

Author(s):
Thomas Risberg
Dave Syer
Since:
2.0
 
 public class JdbcPagingItemReader<T> extends AbstractPagingItemReader<T> implements InitializingBean {
 
 	public static final int VALUE_NOT_SET = -1;
 
 	private DataSource dataSource;
 
 
 
 
 	private RowMapper rowMapper;
 
 	private String firstPageSql;
 
 
 
 	private int fetchSize = ;
 
 	public JdbcPagingItemReader() {
 	}
	public void setDataSource(DataSource dataSource) {
		this. = dataSource;
	}

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for this ResultSet object. If the fetch size specified is zero, the JDBC driver ignores the value.

Parameters:
fetchSize the number of rows to fetch
See also:
java.sql.ResultSet.setFetchSize(int)
	public void setFetchSize(int fetchSize) {
		this. = fetchSize;
	}

A PagingQueryProvider. Supplies all the platform dependent query generation capabilities needed by the reader.

Parameters:
queryProvider the PagingQueryProvider to use
	public void setQueryProvider(PagingQueryProvider queryProvider) {
		this. = queryProvider;
	}

The row mapper implementation to be used by this reader

	public void setRowMapper(RowMapper rowMapper) {
		this. = rowMapper;
	}

The parameter values to be used for the query execution. If you use named parameters then the key should be the name used in the query clause. If you use "?" placeholders then the key should be the relative index that the parameter appears in the query string built using the select, from and where clauses specified.

Parameters:
parameterValues the values keyed by the parameter named/index used in the query string.
	public void setParameterValues(Map<StringObjectparameterValues) {
		this. = parameterValues;
	}

	public void afterPropertiesSet() throws Exception {
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
			jdbcTemplate.setFetchSize();
		}
		jdbcTemplate.setMaxRows(getPageSize());
		this. = new SimpleJdbcTemplate(jdbcTemplate);
	}
	protected void doReadPage() {
		if (getPage() == 0) {
				.debug("SQL used for reading first page: [" +  + "]");
			}
			if ( != null && .size() > 0) {
							getParameterMap(null), rowCallback);
				}
				else {
							getParameterList(null).toArray(), rowCallback);
				}
			}
			else {
			}
		}
		else {
				.debug("SQL used for reading remaining pages: [" +  + "]");
			}
			}
			else {
			}
		}
		if ( == null) {
		}
		else {
		}
		.addAll(rowCallback.getResults());
	}
	protected void doJumpToPage(int itemIndex) {
		if (getPage() > 0) {
			String jumpToItemSql;
			jumpToItemSql = .generateJumpToItemQuery(itemIndexgetPageSize());
				.debug("SQL used for jumping: [" + jumpToItemSql + "]");
			}
			RowMapper startMapper = new RowMapper() {
				public Object mapRow(ResultSet rsint ithrows SQLException {
					return rs.getObject(1);
				}
			};
			}
			else {
			}
		}
	}
	private Map<StringObjectgetParameterMap(Map<StringObjectvaluesObject sortKeyValue) {
		Map<StringObjectparameterMap = new LinkedHashMap<StringObject>();
		if (values != null) {
			parameterMap.putAll(values);
		}
		if (sortKeyValue != null) {
			parameterMap.put("_sortKey"sortKeyValue);
		}
			.debug("Using parameterMap:" + parameterMap);
		}
		return parameterMap;
	}
	private List<ObjectgetParameterList(Map<StringObjectvaluesObject sortKeyValue) {
		if (values != null) {
			sm.putAll(values);
		}
		List<ObjectparameterList = new ArrayList<Object>();
		parameterList.addAll(sm.values());
		if (sortKeyValue != null) {
			parameterList.add(sortKeyValue);
		}
			.debug("Using parameterList:" + parameterList);
		}
		return parameterList;
	}
	private class PagingRowCallbackHandler implements RowCallbackHandler {
		private final List<T> results = new ArrayList<T>();
		public List<T> getResults() {
			return ;
		}
		@SuppressWarnings("unchecked")
		public void processRow(ResultSet rsthrows SQLException {
		}
	}
New to GrepCode? Check out our FAQ X