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