1 /*
2 * Copyright 2010-2025 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 * https://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 java.util.Map;
19 import java.util.Optional;
20 import java.util.function.Supplier;
21
22 import org.apache.ibatis.session.SqlSessionFactory;
23 import org.mybatis.spring.batch.MyBatisPagingItemReader;
24
25 /**
26 * A builder for the {@link MyBatisPagingItemReader}.
27 *
28 * @author Kazuki Shimizu
29 *
30 * @param <T>
31 * the generic type
32 *
33 * @see MyBatisPagingItemReader
34 *
35 * @since 2.0.0
36 */
37 public class MyBatisPagingItemReaderBuilder<T> {
38
39 private SqlSessionFactory sqlSessionFactory;
40 private String queryId;
41 private Map<String, Object> parameterValues;
42 private Supplier<Map<String, Object>> parameterValuesSupplier;
43 private Integer pageSize;
44 private Boolean saveState;
45 private Integer maxItemCount;
46
47 /**
48 * Set the {@link SqlSessionFactory} to be used by writer for database access.
49 *
50 * @param sqlSessionFactory
51 * the {@link SqlSessionFactory} to be used by writer for database access
52 *
53 * @return this instance for method chaining
54 *
55 * @see MyBatisPagingItemReader#setSqlSessionFactory(SqlSessionFactory)
56 */
57 public MyBatisPagingItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
58 this.sqlSessionFactory = sqlSessionFactory;
59 return this;
60 }
61
62 /**
63 * Set the query id identifying the statement in the SqlMap configuration file.
64 *
65 * @param queryId
66 * the id for the query
67 *
68 * @return this instance for method chaining
69 *
70 * @see MyBatisPagingItemReader#setQueryId(String)
71 */
72 public MyBatisPagingItemReaderBuilder<T> queryId(String queryId) {
73 this.queryId = queryId;
74 return this;
75 }
76
77 /**
78 * Set the parameter values to be used for the query execution.
79 *
80 * @param parameterValues
81 * the parameter values to be used for the query execution
82 *
83 * @return this instance for method chaining
84 *
85 * @see MyBatisPagingItemReader#setParameterValues(Map)
86 */
87 public MyBatisPagingItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
88 this.parameterValues = parameterValues;
89 return this;
90 }
91
92 /**
93 * Set the parameter supplier to be used to get parameters for the query execution.
94 *
95 * @param parameterValuesSupplier
96 * the parameter supplier to be used to get parameters for the query execution
97 *
98 * @return this instance for method chaining
99 *
100 * @see MyBatisPagingItemReader#setParameterValuesSupplier(Supplier)
101 *
102 * @since 2.1.0
103 */
104 public MyBatisPagingItemReaderBuilder<T> parameterValuesSupplier(
105 Supplier<Map<String, Object>> parameterValuesSupplier) {
106 this.parameterValuesSupplier = parameterValuesSupplier;
107 return this;
108 }
109
110 /**
111 * The number of records to request per page/query. Defaults to 10. Must be greater than zero.
112 *
113 * @param pageSize
114 * number of items
115 *
116 * @return this instance for method chaining
117 *
118 * @see org.springframework.batch.infrastructure.item.database.AbstractPagingItemReader#setPageSize(int)
119 */
120 public MyBatisPagingItemReaderBuilder<T> pageSize(int pageSize) {
121 this.pageSize = pageSize;
122 return this;
123 }
124
125 /**
126 * Configure if the state of the {@link org.springframework.batch.infrastructure.item.ItemStreamSupport} should be
127 * persisted within the {@link org.springframework.batch.infrastructure.item.ExecutionContext} for restart purposes.
128 *
129 * @param saveState
130 * defaults to true
131 *
132 * @return The current instance of the builder.
133 *
134 * @see org.springframework.batch.infrastructure.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
135 */
136 public MyBatisPagingItemReaderBuilder<T> saveState(boolean saveState) {
137 this.saveState = saveState;
138 return this;
139 }
140
141 /**
142 * Configure the max number of items to be read.
143 *
144 * @param maxItemCount
145 * the max items to be read
146 *
147 * @return The current instance of the builder.
148 *
149 * @see org.springframework.batch.infrastructure.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
150 */
151 public MyBatisPagingItemReaderBuilder<T> maxItemCount(int maxItemCount) {
152 this.maxItemCount = maxItemCount;
153 return this;
154 }
155
156 /**
157 * Returns a fully built {@link MyBatisPagingItemReader}.
158 *
159 * @return the reader
160 */
161 public MyBatisPagingItemReader<T> build() {
162 var reader = new MyBatisPagingItemReader<T>();
163 reader.setSqlSessionFactory(this.sqlSessionFactory);
164 reader.setQueryId(this.queryId);
165 reader.setParameterValues(this.parameterValues);
166 reader.setParameterValuesSupplier(this.parameterValuesSupplier);
167 Optional.ofNullable(this.pageSize).ifPresent(reader::setPageSize);
168 Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
169 Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
170 return reader;
171 }
172
173 }