MyBatisPagingItemReaderBuilder.java

  1. /*
  2.  * Copyright 2010-2024 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. import java.util.Map;
  18. import java.util.Optional;
  19. import java.util.function.Supplier;

  20. import org.apache.ibatis.session.SqlSessionFactory;
  21. import org.mybatis.spring.batch.MyBatisPagingItemReader;

  22. /**
  23.  * A builder for the {@link MyBatisPagingItemReader}.
  24.  *
  25.  * @author Kazuki Shimizu
  26.  *
  27.  * @since 2.0.0
  28.  *
  29.  * @see MyBatisPagingItemReader
  30.  */
  31. public class MyBatisPagingItemReaderBuilder<T> {

  32.   private SqlSessionFactory sqlSessionFactory;
  33.   private String queryId;
  34.   private Map<String, Object> parameterValues;
  35.   private Supplier<Map<String, Object>> parameterValuesSupplier;
  36.   private Integer pageSize;
  37.   private Boolean saveState;
  38.   private Integer maxItemCount;

  39.   /**
  40.    * Set the {@link SqlSessionFactory} to be used by writer for database access.
  41.    *
  42.    * @param sqlSessionFactory
  43.    *          the {@link SqlSessionFactory} to be used by writer for database access
  44.    *
  45.    * @return this instance for method chaining
  46.    *
  47.    * @see MyBatisPagingItemReader#setSqlSessionFactory(SqlSessionFactory)
  48.    */
  49.   public MyBatisPagingItemReaderBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  50.     this.sqlSessionFactory = sqlSessionFactory;
  51.     return this;
  52.   }

  53.   /**
  54.    * Set the query id identifying the statement in the SqlMap configuration file.
  55.    *
  56.    * @param queryId
  57.    *          the id for the query
  58.    *
  59.    * @return this instance for method chaining
  60.    *
  61.    * @see MyBatisPagingItemReader#setQueryId(String)
  62.    */
  63.   public MyBatisPagingItemReaderBuilder<T> queryId(String queryId) {
  64.     this.queryId = queryId;
  65.     return this;
  66.   }

  67.   /**
  68.    * Set the parameter values to be used for the query execution.
  69.    *
  70.    * @param parameterValues
  71.    *          the parameter values to be used for the query execution
  72.    *
  73.    * @return this instance for method chaining
  74.    *
  75.    * @see MyBatisPagingItemReader#setParameterValues(Map)
  76.    */
  77.   public MyBatisPagingItemReaderBuilder<T> parameterValues(Map<String, Object> parameterValues) {
  78.     this.parameterValues = parameterValues;
  79.     return this;
  80.   }

  81.   /**
  82.    * Set the parameter supplier to be used to get parameters for the query execution.
  83.    *
  84.    * @param parameterValuesSupplier
  85.    *          the parameter supplier to be used to get parameters for the query execution
  86.    *
  87.    * @return this instance for method chaining
  88.    *
  89.    * @see MyBatisPagingItemReader#setParameterValuesSupplier(Supplier)
  90.    *
  91.    * @since 2.1.0
  92.    */
  93.   public MyBatisPagingItemReaderBuilder<T> parameterValuesSupplier(
  94.       Supplier<Map<String, Object>> parameterValuesSupplier) {
  95.     this.parameterValuesSupplier = parameterValuesSupplier;
  96.     return this;
  97.   }

  98.   /**
  99.    * The number of records to request per page/query. Defaults to 10. Must be greater than zero.
  100.    *
  101.    * @param pageSize
  102.    *          number of items
  103.    *
  104.    * @return this instance for method chaining
  105.    *
  106.    * @see org.springframework.batch.item.database.AbstractPagingItemReader#setPageSize(int)
  107.    */
  108.   public MyBatisPagingItemReaderBuilder<T> pageSize(int pageSize) {
  109.     this.pageSize = pageSize;
  110.     return this;
  111.   }

  112.   /**
  113.    * Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should be persisted within
  114.    * the {@link org.springframework.batch.item.ExecutionContext} for restart purposes.
  115.    *
  116.    * @param saveState
  117.    *          defaults to true
  118.    *
  119.    * @return The current instance of the builder.
  120.    *
  121.    * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
  122.    */
  123.   public MyBatisPagingItemReaderBuilder<T> saveState(boolean saveState) {
  124.     this.saveState = saveState;
  125.     return this;
  126.   }

  127.   /**
  128.    * Configure the max number of items to be read.
  129.    *
  130.    * @param maxItemCount
  131.    *          the max items to be read
  132.    *
  133.    * @return The current instance of the builder.
  134.    *
  135.    * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
  136.    */
  137.   public MyBatisPagingItemReaderBuilder<T> maxItemCount(int maxItemCount) {
  138.     this.maxItemCount = maxItemCount;
  139.     return this;
  140.   }

  141.   /**
  142.    * Returns a fully built {@link MyBatisPagingItemReader}.
  143.    *
  144.    * @return the reader
  145.    */
  146.   public MyBatisPagingItemReader<T> build() {
  147.     var reader = new MyBatisPagingItemReader<T>();
  148.     reader.setSqlSessionFactory(this.sqlSessionFactory);
  149.     reader.setQueryId(this.queryId);
  150.     reader.setParameterValues(this.parameterValues);
  151.     reader.setParameterValuesSupplier(this.parameterValuesSupplier);
  152.     Optional.ofNullable(this.pageSize).ifPresent(reader::setPageSize);
  153.     Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
  154.     Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
  155.     return reader;
  156.   }

  157. }