1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.spring.batch;
17
18 import static org.springframework.util.Assert.notNull;
19 import static org.springframework.util.ClassUtils.getShortName;
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.function.Supplier;
26
27 import org.apache.ibatis.cursor.Cursor;
28 import org.apache.ibatis.session.ExecutorType;
29 import org.apache.ibatis.session.SqlSession;
30 import org.apache.ibatis.session.SqlSessionFactory;
31 import org.springframework.batch.infrastructure.item.support.AbstractItemCountingItemStreamItemReader;
32 import org.springframework.beans.factory.InitializingBean;
33
34
35
36
37
38
39
40
41
42 public class MyBatisCursorItemReader<T> extends AbstractItemCountingItemStreamItemReader<T>
43 implements InitializingBean {
44
45 private String queryId;
46
47 private SqlSessionFactory sqlSessionFactory;
48 private SqlSession sqlSession;
49
50 private Map<String, Object> parameterValues;
51 private Supplier<Map<String, Object>> parameterValuesSupplier;
52
53 private Cursor<T> cursor;
54 private Iterator<T> cursorIterator;
55
56
57
58
59 public MyBatisCursorItemReader() {
60 setName(getShortName(MyBatisCursorItemReader.class));
61 }
62
63 @Override
64 protected T doRead() throws Exception {
65 T next = null;
66 if (cursorIterator.hasNext()) {
67 next = cursorIterator.next();
68 }
69 return next;
70 }
71
72 @Override
73 protected void doOpen() throws Exception {
74 Map<String, Object> parameters = new HashMap<>();
75 if (parameterValues != null) {
76 parameters.putAll(parameterValues);
77 }
78
79 Optional.ofNullable(parameterValuesSupplier).map(Supplier::get).ifPresent(parameters::putAll);
80
81 sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
82 cursor = sqlSession.selectCursor(queryId, parameters);
83 cursorIterator = cursor.iterator();
84 }
85
86 @Override
87 protected void doClose() throws Exception {
88 if (cursor != null) {
89 cursor.close();
90 }
91 if (sqlSession != null) {
92 sqlSession.close();
93 }
94 cursorIterator = null;
95 }
96
97
98
99
100
101
102 @Override
103 public void afterPropertiesSet() throws Exception {
104 notNull(sqlSessionFactory, "A SqlSessionFactory is required.");
105 notNull(queryId, "A queryId is required.");
106 }
107
108
109
110
111
112
113
114 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
115 this.sqlSessionFactory = sqlSessionFactory;
116 }
117
118
119
120
121
122
123
124 public void setQueryId(String queryId) {
125 this.queryId = queryId;
126 }
127
128
129
130
131
132
133
134 public void setParameterValues(Map<String, Object> parameterValues) {
135 this.parameterValues = parameterValues;
136 }
137
138
139
140
141
142
143
144
145
146 public void setParameterValuesSupplier(Supplier<Map<String, Object>> parameterValuesSupplier) {
147 this.parameterValuesSupplier = parameterValuesSupplier;
148 }
149 }