Skip to content

Commit 776468e

Browse files
authored
Merge pull request #235 from kazuki43zoo/support-builder-for-spring-batch
Add builder classes for ItemReader and ItemWriter
2 parents b106968 + ba09908 commit 776468e

File tree

7 files changed

+886
-0
lines changed

7 files changed

+886
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Copyright 2010-2017 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.mybatis.spring.batch.builder;
17+
18+
import org.apache.ibatis.session.SqlSessionFactory;
19+
import org.mybatis.spring.SqlSessionTemplate;
20+
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
21+
22+
/**
23+
* A builder for the {@link MyBatisBatchItemWriter}.
24+
*
25+
* @author Kazuki Shimizu
26+
* @since 2.0.0
27+
* @see MyBatisBatchItemWriter
28+
*/
29+
public class MyBatisBatchItemWriterBuilder<T> {
30+
31+
private SqlSessionTemplate sqlSessionTemplate;
32+
private SqlSessionFactory sqlSessionFactory;
33+
private String statementId;
34+
private Boolean assertUpdates = true;
35+
36+
/**
37+
* Set the {@link SqlSessionTemplate} to be used by writer for database access.
38+
*
39+
* @param sqlSessionTemplate the {@link SqlSessionTemplate} to be used by writer for database access
40+
* @return this instance for method chaining
41+
* @see MyBatisBatchItemWriter#setSqlSessionTemplate(SqlSessionTemplate)
42+
*/
43+
public MyBatisBatchItemWriterBuilder<T> sqlSessionTemplate(
44+
SqlSessionTemplate sqlSessionTemplate) {
45+
this.sqlSessionTemplate = sqlSessionTemplate;
46+
return this;
47+
}
48+
49+
/**
50+
* Set the {@link SqlSessionFactory} to be used by writer for database access.
51+
*
52+
* @param sqlSessionFactory the {@link SqlSessionFactory} to be used by writer for database access
53+
* @return this instance for method chaining
54+
* @see MyBatisBatchItemWriter#setSqlSessionFactory(SqlSessionFactory)
55+
*/
56+
public MyBatisBatchItemWriterBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
57+
this.sqlSessionFactory = sqlSessionFactory;
58+
return this;
59+
}
60+
61+
/**
62+
* Set the statement id identifying the statement in the SqlMap configuration file.
63+
*
64+
* @param statementId the id for the statement
65+
* @return this instance for method chaining
66+
* @see MyBatisBatchItemWriter#setStatementId(String)
67+
*/
68+
public MyBatisBatchItemWriterBuilder<T> statementId(String statementId) {
69+
this.statementId = statementId;
70+
return this;
71+
}
72+
73+
/**
74+
* The flag that determines whether an assertion is made that all items cause at least one row to
75+
* be updated.
76+
*
77+
* @param assertUpdates the flag to set. Defaults to true
78+
* @return this instance for method chaining
79+
* @see MyBatisBatchItemWriter#setAssertUpdates(boolean)
80+
*/
81+
public MyBatisBatchItemWriterBuilder<T> assertUpdates(boolean assertUpdates) {
82+
this.assertUpdates = assertUpdates;
83+
return this;
84+
}
85+
86+
/**
87+
* Returns a fully built {@link MyBatisBatchItemWriter}.
88+
*
89+
* @return the writer
90+
*/
91+
public MyBatisBatchItemWriter<T> build() {
92+
MyBatisBatchItemWriter<T> writer = new MyBatisBatchItemWriter<>();
93+
writer.setSqlSessionTemplate(this.sqlSessionTemplate);
94+
writer.setSqlSessionFactory(this.sqlSessionFactory);
95+
writer.setStatementId(this.statementId);
96+
if (this.assertUpdates != null) {
97+
writer.setAssertUpdates(this.assertUpdates);
98+
}
99+
return writer;
100+
}
101+
102+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Copyright 2010-2017 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.mybatis.spring.batch.builder;
17+
18+
import org.apache.ibatis.session.SqlSessionFactory;
19+
import org.mybatis.spring.batch.MyBatisCursorItemReader;
20+
21+
import java.util.Map;
22+
23+
/**
24+
* A builder for the {@link MyBatisCursorItemReader}.
25+
*
26+
* @author Kazuki Shimizu
27+
* @since 2.0.0
28+
* @see MyBatisCursorItemReader
29+
*/
30+
public class MyBatisCursorItemReaderBuilder<T> {
31+
32+
private SqlSessionFactory sqlSessionFactory;
33+
private String queryId;
34+
private Map<String, Object> parameterValues;
35+
private Boolean saveState;
36+
private Integer maxItemCount;
37+
38+
/**
39+
* Set the {@link SqlSessionFactory} to be used by reader for database access.
40+
*
41+
* @param sqlSessionFactory the {@link SqlSessionFactory} to be used by writer for database access
42+
* @return this instance for method chaining
43+
* @see MyBatisCursorItemReader#setSqlSessionFactory(SqlSessionFactory)
44+
*/
45+
public MyBatisCursorItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
46+
this.sqlSessionFactory = sqlSessionFactory;
47+
return this;
48+
}
49+
50+
/**
51+
* Set the query id identifying the statement in the SqlMap configuration file.
52+
*
53+
* @param queryId the id for the query
54+
* @return this instance for method chaining
55+
* @see MyBatisCursorItemReader#setQueryId(String)
56+
*/
57+
public MyBatisCursorItemReaderBuilder<T> queryId(String queryId) {
58+
this.queryId = queryId;
59+
return this;
60+
}
61+
62+
/**
63+
* Set the parameter values to be used for the query execution.
64+
*
65+
* @param parameterValues the parameter values to be used for the query execution
66+
* @return this instance for method chaining
67+
* @see MyBatisCursorItemReader#setParameterValues(Map)
68+
*/
69+
public MyBatisCursorItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
70+
this.parameterValues = parameterValues;
71+
return this;
72+
}
73+
74+
/**
75+
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should
76+
* be persisted within the {@link org.springframework.batch.item.ExecutionContext} for restart
77+
* purposes.
78+
*
79+
* @param saveState defaults to true
80+
* @return The current instance of the builder.
81+
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
82+
*/
83+
public MyBatisCursorItemReaderBuilder<T> saveState(boolean saveState) {
84+
this.saveState = saveState;
85+
return this;
86+
}
87+
88+
/**
89+
* Configure the max number of items to be read.
90+
*
91+
* @param maxItemCount the max items to be read
92+
* @return The current instance of the builder.
93+
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
94+
*/
95+
public MyBatisCursorItemReaderBuilder<T> maxItemCount(int maxItemCount) {
96+
this.maxItemCount = maxItemCount;
97+
return this;
98+
}
99+
100+
/**
101+
* Returns a fully built {@link MyBatisCursorItemReader}.
102+
*
103+
* @return the reader
104+
*/
105+
public MyBatisCursorItemReader<T> build() {
106+
MyBatisCursorItemReader<T> reader = new MyBatisCursorItemReader<>();
107+
reader.setSqlSessionFactory(this.sqlSessionFactory);
108+
reader.setQueryId(this.queryId);
109+
reader.setParameterValues(this.parameterValues);
110+
if (this.saveState != null) {
111+
reader.setSaveState(saveState);
112+
}
113+
if (this.maxItemCount != null) {
114+
reader.setMaxItemCount(this.maxItemCount);
115+
}
116+
return reader;
117+
}
118+
119+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* Copyright 2010-2017 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.mybatis.spring.batch.builder;
17+
18+
import org.apache.ibatis.session.SqlSessionFactory;
19+
import org.mybatis.spring.batch.MyBatisPagingItemReader;
20+
21+
import java.util.Map;
22+
23+
/**
24+
* A builder for the {@link MyBatisPagingItemReader}.
25+
*
26+
* @author Kazuki Shimizu
27+
* @since 2.0.0
28+
* @see MyBatisPagingItemReader
29+
*/
30+
public class MyBatisPagingItemReaderBuilder<T> {
31+
32+
private SqlSessionFactory sqlSessionFactory;
33+
private String queryId;
34+
private Map<String, Object> parameterValues;
35+
private Integer pageSize;
36+
private Boolean saveState;
37+
private Integer maxItemCount;
38+
39+
/**
40+
* Set the {@link SqlSessionFactory} to be used by writer for database access.
41+
*
42+
* @param sqlSessionFactory the {@link SqlSessionFactory} to be used by writer for database access
43+
* @return this instance for method chaining
44+
* @see MyBatisPagingItemReader#setSqlSessionFactory(SqlSessionFactory)
45+
*/
46+
public MyBatisPagingItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
47+
this.sqlSessionFactory = sqlSessionFactory;
48+
return this;
49+
}
50+
51+
/**
52+
* Set the query id identifying the statement in the SqlMap configuration file.
53+
*
54+
* @param queryId the id for the query
55+
* @return this instance for method chaining
56+
* @see MyBatisPagingItemReader#setQueryId(String)
57+
*/
58+
public MyBatisPagingItemReaderBuilder<T> queryId(String queryId) {
59+
this.queryId = queryId;
60+
return this;
61+
}
62+
63+
/**
64+
* Set the parameter values to be used for the query execution.
65+
*
66+
* @param parameterValues the parameter values to be used for the query execution
67+
* @return this instance for method chaining
68+
* @see MyBatisPagingItemReader#setParameterValues(Map)
69+
*/
70+
public MyBatisPagingItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
71+
this.parameterValues = parameterValues;
72+
return this;
73+
}
74+
75+
/**
76+
* The number of records to request per page/query. Defaults to 10. Must be greater than zero.
77+
*
78+
* @param pageSize number of items
79+
* @return this instance for method chaining
80+
* @see org.springframework.batch.item.database.AbstractPagingItemReader#setPageSize(int)
81+
*/
82+
public MyBatisPagingItemReaderBuilder<T> pageSize(int pageSize) {
83+
this.pageSize = pageSize;
84+
return this;
85+
}
86+
87+
/**
88+
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should
89+
* be persisted within the {@link org.springframework.batch.item.ExecutionContext} for restart
90+
* purposes.
91+
*
92+
* @param saveState defaults to true
93+
* @return The current instance of the builder.
94+
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
95+
*/
96+
public MyBatisPagingItemReaderBuilder<T> saveState(boolean saveState) {
97+
this.saveState = saveState;
98+
return this;
99+
}
100+
101+
/**
102+
* Configure the max number of items to be read.
103+
*
104+
* @param maxItemCount the max items to be read
105+
* @return The current instance of the builder.
106+
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
107+
*/
108+
public MyBatisPagingItemReaderBuilder<T> maxItemCount(int maxItemCount) {
109+
this.maxItemCount = maxItemCount;
110+
return this;
111+
}
112+
113+
/**
114+
* Returns a fully built {@link MyBatisPagingItemReader}.
115+
*
116+
* @return the reader
117+
*/
118+
public MyBatisPagingItemReader<T> build() {
119+
MyBatisPagingItemReader<T> reader = new MyBatisPagingItemReader<>();
120+
reader.setSqlSessionFactory(this.sqlSessionFactory);
121+
reader.setQueryId(this.queryId);
122+
reader.setParameterValues(this.parameterValues);
123+
if (this.pageSize != null) {
124+
reader.setPageSize(this.pageSize);
125+
}
126+
if (this.saveState != null) {
127+
reader.setSaveState(saveState);
128+
}
129+
if (this.maxItemCount != null) {
130+
reader.setMaxItemCount(this.maxItemCount);
131+
}
132+
return reader;
133+
}
134+
135+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright 2010-2017 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+
* Contains classes to builder classes for {@link org.springframework.batch.item.ItemReader} and
18+
* {@link org.springframework.batch.item.ItemWriter}.
19+
*
20+
* @since 2.0.0
21+
*/
22+
package org.mybatis.spring.batch.builder;

0 commit comments

Comments
 (0)