15
15
*/
16
16
package org .mybatis .spring .batch ;
17
17
18
- import static org .springframework .util .Assert .notNull ;
19
- import static org .springframework .util .ClassUtils .getShortName ;
18
+ import org .apache .ibatis .session .ExecutorType ;
19
+ import org .apache .ibatis .session .SqlSession ;
20
+ import org .apache .ibatis .session .SqlSessionFactory ;
21
+ import org .springframework .batch .item .database .AbstractPagingItemReader ;
20
22
21
23
import java .util .HashMap ;
22
24
import java .util .Map ;
23
25
import java .util .concurrent .CopyOnWriteArrayList ;
24
26
25
- import org .apache .ibatis .session .ExecutorType ;
26
- import org .apache .ibatis .session .SqlSession ;
27
- import org .apache .ibatis .session .SqlSessionFactory ;
28
- import org .mybatis .spring .SqlSessionTemplate ;
29
- import org .springframework .batch .item .database .AbstractPagingItemReader ;
27
+ import static org .springframework .util .Assert .notNull ;
28
+ import static org .springframework .util .ClassUtils .getShortName ;
30
29
31
30
/**
32
31
* {@code org.springframework.batch.item.ItemReader} for reading database
@@ -44,7 +43,7 @@ public class MyBatisPagingItemReader<T> extends AbstractPagingItemReader<T> {
44
43
45
44
private SqlSessionFactory sqlSessionFactory ;
46
45
47
- private SqlSessionTemplate sqlSessionTemplate ;
46
+ private SqlSession sqlSession ;
48
47
49
48
private Map <String , Object > parameterValues ;
50
49
@@ -89,10 +88,23 @@ public void setParameterValues(Map<String, Object> parameterValues) {
89
88
public void afterPropertiesSet () throws Exception {
90
89
super .afterPropertiesSet ();
91
90
notNull (sqlSessionFactory , "A SqlSessionFactory is required." );
92
- sqlSessionTemplate = new SqlSessionTemplate (sqlSessionFactory , ExecutorType .BATCH );
93
91
notNull (queryId , "A queryId is required." );
94
92
}
95
93
94
+ @ Override
95
+ protected void doOpen () throws Exception {
96
+ super .doOpen ();
97
+ sqlSession = sqlSessionFactory .openSession (ExecutorType .BATCH );
98
+ }
99
+
100
+ @ Override
101
+ protected void doClose () throws Exception {
102
+ if (sqlSession != null ) {
103
+ sqlSession .close ();
104
+ }
105
+ super .doClose ();
106
+ }
107
+
96
108
@ Override
97
109
protected void doReadPage () {
98
110
Map <String , Object > parameters = new HashMap <>();
@@ -107,7 +119,7 @@ protected void doReadPage() {
107
119
} else {
108
120
results .clear ();
109
121
}
110
- results .addAll (sqlSessionTemplate .selectList (queryId , parameters ));
122
+ results .addAll (sqlSession .selectList (queryId , parameters ));
111
123
}
112
124
113
125
@ Override
0 commit comments