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;
17  
18  import static org.springframework.util.Assert.isTrue;
19  import static org.springframework.util.Assert.notNull;
20  
21  import java.util.List;
22  
23  import org.apache.ibatis.executor.BatchResult;
24  import org.apache.ibatis.session.ExecutorType;
25  import org.apache.ibatis.session.SqlSession;
26  import org.apache.ibatis.session.SqlSessionFactory;
27  import org.mybatis.logging.Logger;
28  import org.mybatis.logging.LoggerFactory;
29  import org.mybatis.spring.SqlSessionTemplate;
30  import org.springframework.batch.item.Chunk;
31  import org.springframework.batch.item.ItemWriter;
32  import org.springframework.beans.factory.InitializingBean;
33  import org.springframework.core.convert.converter.Converter;
34  import org.springframework.dao.EmptyResultDataAccessException;
35  import org.springframework.dao.InvalidDataAccessResourceUsageException;
36  
37  /**
38   * {@code ItemWriter} that uses the batching features from {@code SqlSessionTemplate} to execute a batch of statements
39   * for all items provided.
40   * <p>
41   * Provided to facilitate the migration from Spring-Batch iBATIS 2 writers to MyBatis 3.
42   * <p>
43   * The user must provide a MyBatis statement id that points to the SQL statement defined in the MyBatis.
44   * <p>
45   * It is expected that {@link #write(Chunk)} is called inside a transaction. If it is not each statement call will be
46   * autocommitted and flushStatements will return no results.
47   * <p>
48   * The writer is thread safe after its properties are set (normal singleton behavior), so it can be used to write in
49   * multiple concurrent transactions.
50   *
51   * @author Eduardo Macarron
52   *
53   * @since 1.1.0
54   */
55  public class MyBatisBatchItemWriter<T> implements ItemWriter<T>, InitializingBean {
56  
57    private static final Logger LOGGER = LoggerFactory.getLogger(MyBatisBatchItemWriter.class);
58  
59    private SqlSessionTemplate sqlSessionTemplate;
60  
61    private String statementId;
62  
63    private boolean assertUpdates = true;
64  
65    private Converter<T, ?> itemToParameterConverter = new PassThroughConverter<>();
66  
67    /**
68     * Public setter for the flag that determines whether an assertion is made that number of BatchResult objects returned
69     * is one and all items cause at least one row to be updated.
70     *
71     * @param assertUpdates
72     *          the flag to set. Defaults to true;
73     */
74    public void setAssertUpdates(boolean assertUpdates) {
75      this.assertUpdates = assertUpdates;
76    }
77  
78    /**
79     * Public setter for {@link SqlSessionFactory} for injection purposes.
80     *
81     * @param sqlSessionFactory
82     *          a factory object for the {@link SqlSession}.
83     */
84    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
85      if (sqlSessionTemplate == null) {
86        this.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory, ExecutorType.BATCH);
87      }
88    }
89  
90    /**
91     * Public setter for the {@link SqlSessionTemplate}.
92     *
93     * @param sqlSessionTemplate
94     *          a template object for use the {@link SqlSession} on the Spring managed transaction
95     */
96    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
97      this.sqlSessionTemplate = sqlSessionTemplate;
98    }
99  
100   /**
101    * Public setter for the statement id identifying the statement in the SqlMap configuration file.
102    *
103    * @param statementId
104    *          the id for the statement
105    */
106   public void setStatementId(String statementId) {
107     this.statementId = statementId;
108   }
109 
110   /**
111    * Public setter for a converter that converting item to parameter object.
112    * <p>
113    * By default implementation, an item does not convert.
114    *
115    * @param itemToParameterConverter
116    *          a converter that converting item to parameter object
117    *
118    * @since 2.0.0
119    */
120   public void setItemToParameterConverter(Converter<T, ?> itemToParameterConverter) {
121     this.itemToParameterConverter = itemToParameterConverter;
122   }
123 
124   /**
125    * Check mandatory properties - there must be an SqlSession and a statementId.
126    */
127   @Override
128   public void afterPropertiesSet() {
129     notNull(sqlSessionTemplate, "A SqlSessionFactory or a SqlSessionTemplate is required.");
130     isTrue(ExecutorType.BATCH == sqlSessionTemplate.getExecutorType(),
131         "SqlSessionTemplate's executor type must be BATCH");
132     notNull(statementId, "A statementId is required.");
133     notNull(itemToParameterConverter, "A itemToParameterConverter is required.");
134   }
135 
136   /**
137    * {@inheritDoc}
138    */
139   @Override
140   public void write(final Chunk<? extends T> items) {
141 
142     if (!items.isEmpty()) {
143       LOGGER.debug(() -> "Executing batch with " + items.size() + " items.");
144 
145       for (T item : items) {
146         sqlSessionTemplate.update(statementId, itemToParameterConverter.convert(item));
147       }
148 
149       List<BatchResult> results = sqlSessionTemplate.flushStatements();
150 
151       if (assertUpdates) {
152         if (results.size() != 1) {
153           throw new InvalidDataAccessResourceUsageException("Batch execution returned invalid results. "
154               + "Expected 1 but number of BatchResult objects returned was " + results.size());
155         }
156 
157         int[] updateCounts = results.get(0).getUpdateCounts();
158 
159         for (int i = 0; i < updateCounts.length; i++) {
160           int value = updateCounts[i];
161           if (value == 0) {
162             throw new EmptyResultDataAccessException("Item " + i + " of " + updateCounts.length
163                 + " did not update any rows: [" + items.getItems().get(i) + "]", 1);
164           }
165         }
166       }
167     }
168   }
169 
170   private static class PassThroughConverter<T> implements Converter<T, T> {
171 
172     @Override
173     public T convert(T source) {
174       return source;
175     }
176 
177   }
178 
179 }