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