1 | /* |
2 | * Copyright 2006-2007 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 | package org.springframework.batch.item.database; |
17 | |
18 | import org.springframework.batch.item.database.support.IbatisKeyCollector; |
19 | import org.springframework.orm.ibatis.SqlMapClientTemplate; |
20 | |
21 | import com.ibatis.sqlmap.client.SqlMapClient; |
22 | |
23 | /** |
24 | * Extension of {@link DrivingQueryItemReader} that maps keys to |
25 | * objects. An iBatis query id must be set to map and return each 'detail record'. |
26 | * |
27 | * @author Lucas Ward |
28 | * @see IbatisKeyCollector |
29 | */ |
30 | public class IbatisDrivingQueryItemReader extends DrivingQueryItemReader { |
31 | |
32 | private String detailsQueryId; |
33 | |
34 | private SqlMapClientTemplate sqlMapClientTemplate; |
35 | |
36 | /** |
37 | * Overridden read() that uses the returned key as arguments to the details query. |
38 | * |
39 | * @see org.springframework.batch.item.database.DrivingQueryItemReader#read() |
40 | */ |
41 | public Object read() { |
42 | Object key = super.read(); |
43 | if (key==null) { |
44 | return null; |
45 | } |
46 | return sqlMapClientTemplate.queryForObject(detailsQueryId, key); |
47 | } |
48 | |
49 | /** |
50 | * @param detailsQueryId id of the iBATIS select statement that will used |
51 | * to retrieve an object for a single primary key from the list |
52 | * returned by driving query |
53 | */ |
54 | public void setDetailsQueryId(String detailsQueryId) { |
55 | this.detailsQueryId = detailsQueryId; |
56 | } |
57 | |
58 | /** |
59 | * Set the {@link SqlMapClientTemplate} to use for this input source. |
60 | * |
61 | * @param sqlMapClient |
62 | */ |
63 | public void setSqlMapClient( |
64 | SqlMapClient sqlMapClient) { |
65 | this.sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient); |
66 | } |
67 | } |