1 | /* |
2 | * Copyright 2006-2008 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 org.springframework.util.ClassUtils; |
20 | import org.springframework.util.Assert; |
21 | import org.springframework.batch.item.ExecutionContext; |
22 | import org.springframework.orm.ibatis.SqlMapClientTemplate; |
23 | |
24 | import java.util.Map; |
25 | import java.util.HashMap; |
26 | import java.util.concurrent.CopyOnWriteArrayList; |
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 | public void afterPropertiesSet() throws Exception { |
111 | super.afterPropertiesSet(); |
112 | Assert.notNull(sqlMapClient); |
113 | sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient); |
114 | Assert.notNull(queryId); |
115 | } |
116 | |
117 | @Override |
118 | @SuppressWarnings("unchecked") |
119 | protected void doReadPage() { |
120 | Map<String, Object> parameters = new HashMap<String, Object>(); |
121 | if (parameterValues != null) { |
122 | parameters.putAll(parameterValues); |
123 | } |
124 | parameters.put("_page", getPage()); |
125 | parameters.put("_pagesize", getPageSize()); |
126 | parameters.put("_skiprows", getPage() * getPageSize()); |
127 | if (results == null) { |
128 | results = new CopyOnWriteArrayList<T>(); |
129 | } |
130 | else { |
131 | results.clear(); |
132 | } |
133 | results.addAll(sqlMapClientTemplate.queryForList(queryId, parameters)); |
134 | } |
135 | |
136 | @Override |
137 | protected void doJumpToPage(int itemIndex) { |
138 | } |
139 | |
140 | } |