MyBatisCursorItemReaderBuilder.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.MyBatisCursorItemReader;

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

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

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

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

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

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

  97.   /**
  98.    * Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport} should be persisted within
  99.    * the {@link org.springframework.batch.item.ExecutionContext} for restart purposes.
  100.    *
  101.    * @param saveState
  102.    *          defaults to true
  103.    *
  104.    * @return The current instance of the builder.
  105.    *
  106.    * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setSaveState(boolean)
  107.    */
  108.   public MyBatisCursorItemReaderBuilder<T> saveState(boolean saveState) {
  109.     this.saveState = saveState;
  110.     return this;
  111.   }

  112.   /**
  113.    * Configure the max number of items to be read.
  114.    *
  115.    * @param maxItemCount
  116.    *          the max items to be read
  117.    *
  118.    * @return The current instance of the builder.
  119.    *
  120.    * @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
  121.    */
  122.   public MyBatisCursorItemReaderBuilder<T> maxItemCount(int maxItemCount) {
  123.     this.maxItemCount = maxItemCount;
  124.     return this;
  125.   }

  126.   /**
  127.    * Returns a fully built {@link MyBatisCursorItemReader}.
  128.    *
  129.    * @return the reader
  130.    */
  131.   public MyBatisCursorItemReader<T> build() {
  132.     var reader = new MyBatisCursorItemReader<T>();
  133.     reader.setSqlSessionFactory(this.sqlSessionFactory);
  134.     reader.setQueryId(this.queryId);
  135.     reader.setParameterValues(this.parameterValues);
  136.     reader.setParameterValuesSupplier(this.parameterValuesSupplier);
  137.     Optional.ofNullable(this.saveState).ifPresent(reader::setSaveState);
  138.     Optional.ofNullable(this.maxItemCount).ifPresent(reader::setMaxItemCount);
  139.     return reader;
  140.   }

  141. }