View Javadoc
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  
18  import java.util.Optional;
19  
20  import org.apache.ibatis.session.SqlSessionFactory;
21  import org.mybatis.spring.SqlSessionTemplate;
22  import org.mybatis.spring.batch.MyBatisBatchItemWriter;
23  import org.springframework.core.convert.converter.Converter;
24  
25  /**
26   * A builder for the {@link MyBatisBatchItemWriter}.
27   *
28   * @author Kazuki Shimizu
29   *
30   * @since 2.0.0
31   *
32   * @see MyBatisBatchItemWriter
33   */
34  public class MyBatisBatchItemWriterBuilder<T> {
35  
36    private SqlSessionTemplate sqlSessionTemplate;
37    private SqlSessionFactory sqlSessionFactory;
38    private String statementId;
39    private Boolean assertUpdates;
40    private Converter<T, ?> itemToParameterConverter;
41  
42    /**
43     * Set the {@link SqlSessionTemplate} to be used by writer for database access.
44     *
45     * @param sqlSessionTemplate
46     *          the {@link SqlSessionTemplate} to be used by writer for database access
47     *
48     * @return this instance for method chaining
49     *
50     * @see MyBatisBatchItemWriter#setSqlSessionTemplate(SqlSessionTemplate)
51     */
52    public MyBatisBatchItemWriterBuilder<T> sqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
53      this.sqlSessionTemplate = sqlSessionTemplate;
54      return this;
55    }
56  
57    /**
58     * Set the {@link SqlSessionFactory} to be used by writer for database access.
59     *
60     * @param sqlSessionFactory
61     *          the {@link SqlSessionFactory} to be used by writer for database access
62     *
63     * @return this instance for method chaining
64     *
65     * @see MyBatisBatchItemWriter#setSqlSessionFactory(SqlSessionFactory)
66     */
67    public MyBatisBatchItemWriterBuilder<T> sqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
68      this.sqlSessionFactory = sqlSessionFactory;
69      return this;
70    }
71  
72    /**
73     * Set the statement id identifying the statement in the SqlMap configuration file.
74     *
75     * @param statementId
76     *          the id for the statement
77     *
78     * @return this instance for method chaining
79     *
80     * @see MyBatisBatchItemWriter#setStatementId(String)
81     */
82    public MyBatisBatchItemWriterBuilder<T> statementId(String statementId) {
83      this.statementId = statementId;
84      return this;
85    }
86  
87    /**
88     * The flag that determines whether an assertion is made that all items cause at least one row to be updated.
89     *
90     * @param assertUpdates
91     *          the flag to set. Defaults to true
92     *
93     * @return this instance for method chaining
94     *
95     * @see MyBatisBatchItemWriter#setAssertUpdates(boolean)
96     */
97    public MyBatisBatchItemWriterBuilder<T> assertUpdates(boolean assertUpdates) {
98      this.assertUpdates = assertUpdates;
99      return this;
100   }
101 
102   /**
103    * Set a converter that converting item to parameter object.
104    *
105    * @param itemToParameterConverter
106    *          a converter that converting item to parameter object
107    *
108    * @return this instance for method chaining
109    *
110    * @see MyBatisBatchItemWriter#setItemToParameterConverter(Converter)
111    */
112   public MyBatisBatchItemWriterBuilder<T> itemToParameterConverter(Converter<T, ?> itemToParameterConverter) {
113     this.itemToParameterConverter = itemToParameterConverter;
114     return this;
115   }
116 
117   /**
118    * Returns a fully built {@link MyBatisBatchItemWriter}.
119    *
120    * @return the writer
121    */
122   public MyBatisBatchItemWriter<T> build() {
123     var writer = new MyBatisBatchItemWriter<T>();
124     writer.setSqlSessionTemplate(this.sqlSessionTemplate);
125     writer.setSqlSessionFactory(this.sqlSessionFactory);
126     writer.setStatementId(this.statementId);
127     Optional.ofNullable(this.assertUpdates).ifPresent(writer::setAssertUpdates);
128     Optional.ofNullable(this.itemToParameterConverter).ifPresent(writer::setItemToParameterConverter);
129     return writer;
130   }
131 
132 }