View Javadoc

1   /*
2    * Copyright 2006-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.batch.item.database;
18  
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.concurrent.CopyOnWriteArrayList;
22  
23  import org.springframework.batch.item.ExecutionContext;
24  import org.springframework.orm.ibatis.SqlMapClientTemplate;
25  import org.springframework.util.Assert;
26  import org.springframework.util.ClassUtils;
27  
28  import com.ibatis.sqlmap.client.SqlMapClient;
29  
30  /**
31   * <p>
32   * {@link org.springframework.batch.item.ItemReader} for reading database
33   * records using iBATIS in a paging fashion.
34   * </p>
35   *
36   * <p>
37   * It executes the query specified as the {@link #setQueryId(String)} to
38   * retrieve requested data. The query is executed using paged requests of a size
39   * specified in {@link #setPageSize(int)}. Additional pages are requested when
40   * needed as {@link #read()} method is called, returning an object corresponding
41   * to current position. Some standard query parameters are provided by the
42   * reader and the SQL in the named query must use some or all of these parameters
43   * (depending on the SQL variant) to construct a result set of the required
44   * size. The parameters are:
45   * <ul>
46   * <li><code>_page</code>: the page number to be read (starting at 0)</li>
47   * <li><code>_pagesize</code>: the size of the pages, i.e. the number of rows to
48   * return</li>
49   * <li><code>_skiprows</code>: the product of <code>_page</code> and
50   * <code>_pagesize</code></li>
51   * </ul>
52   * Failure to write the correct platform-specific SQL often results in an
53   * infinite loop in the reader because it keeps asking for the next page and
54   * gets the same result set over and over.
55   * </p>
56   *
57   * <p>
58   * The performance of the paging depends on the iBATIS implementation.
59   * Setting a fairly large page size and using a commit interval that matches the
60   * page size should provide better performance.
61   * </p>
62   *
63   * <p>
64   * The implementation is thread-safe in between calls to
65   * {@link #open(ExecutionContext)}, but remember to use
66   * <code>saveState=false</code> if used in a multi-threaded client (no restart
67   * available).
68   * </p>
69   *
70   * @author Thomas Risberg
71   * @author Dave Syer
72   * @since 2.0
73   */
74  public class IbatisPagingItemReader<T> extends AbstractPagingItemReader<T> {
75  
76  	private SqlMapClient sqlMapClient;
77  
78  	private String queryId;
79  
80  	private SqlMapClientTemplate sqlMapClientTemplate;
81  
82  	private Map<String, Object> parameterValues;
83  
84  	public IbatisPagingItemReader() {
85  		setName(ClassUtils.getShortName(IbatisPagingItemReader.class));
86  	}
87  
88  	public void setSqlMapClient(SqlMapClient sqlMapClient) {
89  		this.sqlMapClient = sqlMapClient;
90  	}
91  
92  	public void setQueryId(String queryId) {
93  		this.queryId = queryId;
94  	}
95  
96  	/**
97  	 * The parameter values to be used for the query execution.
98  	 *
99  	 * @param parameterValues the values keyed by the parameter named used in
100 	 * the query string.
101 	 */
102 	public void setParameterValues(Map<String, Object> parameterValues) {
103 		this.parameterValues = parameterValues;
104 	}
105 
106 	/**
107 	 * Check mandatory properties.
108 	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
109 	 */
110 	@Override
111 	public void afterPropertiesSet() throws Exception {
112 		super.afterPropertiesSet();
113 		Assert.notNull(sqlMapClient);
114 		sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient);
115 		Assert.notNull(queryId);
116 	}
117 
118 	@Override
119 	@SuppressWarnings("unchecked")
120 	protected void doReadPage() {
121 		Map<String, Object> parameters = new HashMap<String, Object>();
122 		if (parameterValues != null) {
123 			parameters.putAll(parameterValues);
124 		}
125 		parameters.put("_page", getPage());
126 		parameters.put("_pagesize", getPageSize());
127 		parameters.put("_skiprows", getPage() * getPageSize());
128 		if (results == null) {
129 			results = new CopyOnWriteArrayList<T>();
130 		}
131 		else {
132 			results.clear();
133 		}
134 		results.addAll(sqlMapClientTemplate.queryForList(queryId, parameters));
135 	}
136 
137 	@Override
138 	protected void doJumpToPage(int itemIndex) {
139 	}
140 
141 }