Skip to content

Commit 4503621

Browse files
committed
Add explanation for MyBatisBatchItemWriter#itemToParameterConverter on doc
See #21
1 parent df2602e commit 4503621

File tree

12 files changed

+391
-15
lines changed

12 files changed

+391
-15
lines changed

src/site/es/xdoc/batch.xml

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -226,6 +226,71 @@ public MyBatisBatchItemWriter<User> writer() {
226226
.build();
227227
}]]></source>
228228

229+
230+
<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>
231+
232+
<p>
233+
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
234+
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
235+
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
236+
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
237+
Follows below a sample:
238+
</p>
239+
240+
<p>
241+
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
242+
</p>
243+
244+
<source><![CDATA[
245+
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
246+
return item -> {
247+
Map<String, Object> parameter = new HashMap<>();
248+
parameter.put("item", item);
249+
parameter.put("operationBy", operationBy);
250+
parameter.put("operationAt", operationAt);
251+
return parameter;
252+
};
253+
}]]></source>
254+
255+
<p>
256+
At next, you write a sql mapping.
257+
</p>
258+
259+
<source><![CDATA[
260+
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
261+
insert into persons (first_name, last_name, operation_by, operation_at)
262+
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
263+
</select>]]></source>
264+
265+
<p>
266+
At last, you configure the <code>MyBatisBatchItemWriter</code>.
267+
</p>
268+
269+
<source><![CDATA[
270+
// Sample for Java Config
271+
@Bean
272+
public MyBatisBatchItemWriter<Person> writer() throws Exception {
273+
return new MyBatisBatchItemWriterBuilder<Person>()
274+
.sqlSessionFactory(sqlSessionFactory())
275+
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
276+
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
277+
.build();
278+
}]]></source>
279+
280+
<source><![CDATA[
281+
<!-- Sample for XML Config -->
282+
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
283+
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
284+
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
285+
<property name="itemToParameterConverter">
286+
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
287+
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
288+
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
289+
</bean>
290+
</property>
291+
</bean>
292+
]]></source>
293+
229294
<p><strong>Escribiendo en distintas tablas usando composite writers (con algunos condicionantes):</strong></p>
230295

231296
<p>Esta técnica sólo puede usarse con MyBatis 3.2+, por que había un

src/site/ja/xdoc/batch.xml

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -212,6 +212,74 @@ public MyBatisBatchItemWriter<User> writer() {
212212
.build();
213213
}]]></source>
214214

215+
216+
<p><strong>ItemReaderを使用して読み込んだアイテムを任意のパラメータオブジェクトへ変換する</strong></p>
217+
218+
<p>
219+
デフォルトの動作では、<code>MyBatisBatchItemWriter</code> は <code>ItemReader</code> を使用して読みこんだアイテム
220+
(または <code>ItemProcessor</code> によって変換したアイテム)を、
221+
そのままMyBatis(<code>SqlSession</code> の <code>update</code> メソッド)のパラメーターオブジェクトとして渡します。
222+
もしMyBatisへ渡すパラメーターオブジェクトをカスタマイズしたい場合は、
223+
<code>itemToParameterConverter</code> オプションを利用することで実現するすることができます。
224+
たとえば、<code>itemToParameterConverter</code> オプションを使用すると、 アイテムオブジェクト以外のオブジェクトをMyBatisへ渡すことができます。
225+
以下にサンプルを示します。
226+
</p>
227+
228+
<p>
229+
まず、任意のパラメータオブジェクトに変換するためのコンバータクラス(またはファクトリメソッド)を作成します。以下のサンプルではファクトリメソッドを使用します。
230+
</p>
231+
232+
<source><![CDATA[
233+
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
234+
return item -> {
235+
Map<String, Object> parameter = new HashMap<>();
236+
parameter.put("item", item);
237+
parameter.put("operationBy", operationBy);
238+
parameter.put("operationAt", operationAt);
239+
return parameter;
240+
};
241+
}]]></source>
242+
243+
<p>
244+
つぎに, SQLマッピングを書きます。
245+
</p>
246+
247+
<source><![CDATA[
248+
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
249+
insert into persons (first_name, last_name, operation_by, operation_at)
250+
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
251+
</select>]]></source>
252+
253+
<p>
254+
さいごに, <code>MyBatisBatchItemWriter</code>の設定を行います。
255+
</p>
256+
257+
<source><![CDATA[
258+
// Sample for Java Config
259+
@Bean
260+
public MyBatisBatchItemWriter<Person> writer() throws Exception {
261+
return new MyBatisBatchItemWriterBuilder<Person>()
262+
.sqlSessionFactory(sqlSessionFactory())
263+
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
264+
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
265+
.build();
266+
}]]></source>
267+
268+
<source><![CDATA[
269+
<!-- Sample for XML Config -->
270+
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
271+
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
272+
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
273+
<property name="itemToParameterConverter">
274+
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
275+
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
276+
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
277+
</bean>
278+
</property>
279+
</bean>
280+
]]></source>
281+
282+
215283
<p><strong>Composite Writer を使って複数のテーブルに書き込む(注意事項あり)</strong></p>
216284

217285
<p>このテクニックを使うには MyBatis 3.2 以降が必要です。それ以前のバージョンには <a href="http://code.google.com/p/mybatis/issues/detail?id=741">問題</a> があるため、Writer が期待通りに動作しません。

src/site/ko/xdoc/batch.xml

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -204,6 +204,72 @@ public MyBatisBatchItemWriter<User> writer() {
204204
.build();
205205
}]]></source>
206206

207+
208+
<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>
209+
210+
<p>
211+
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
212+
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
213+
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
214+
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
215+
Follows below a sample:
216+
</p>
217+
218+
<p>
219+
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
220+
</p>
221+
222+
<source><![CDATA[
223+
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
224+
return item -> {
225+
Map<String, Object> parameter = new HashMap<>();
226+
parameter.put("item", item);
227+
parameter.put("operationBy", operationBy);
228+
parameter.put("operationAt", operationAt);
229+
return parameter;
230+
};
231+
}]]></source>
232+
233+
<p>
234+
At next, you write a sql mapping.
235+
</p>
236+
237+
<source><![CDATA[
238+
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
239+
insert into persons (first_name, last_name, operation_by, operation_at)
240+
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
241+
</select>]]></source>
242+
243+
<p>
244+
At last, you configure the <code>MyBatisBatchItemWriter</code>.
245+
</p>
246+
247+
<source><![CDATA[
248+
// Sample for Java Config
249+
@Bean
250+
public MyBatisBatchItemWriter<Person> writer() throws Exception {
251+
return new MyBatisBatchItemWriterBuilder<Person>()
252+
.sqlSessionFactory(sqlSessionFactory())
253+
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
254+
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
255+
.build();
256+
}]]></source>
257+
258+
<source><![CDATA[
259+
<!-- Sample for XML Config -->
260+
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
261+
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
262+
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
263+
<property name="itemToParameterConverter">
264+
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
265+
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
266+
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
267+
</bean>
268+
</property>
269+
</bean>
270+
]]></source>
271+
272+
207273
<p><strong>여러개의 테이블에 데이터를 쓰려면 한꺼번에 처리할 수 있도록 만든 writer(몇가지 규칙을 가지고)를 사용하자. </strong></p>
208274

209275
<p>이 기능은 마이바티스 3.2이상에서만 사용할 수 있다. 이전의 버전에서는 예상과 다르게 동작하는데 그 내용은

src/site/xdoc/batch.xml

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2010-2017 the original author or authors.
4+
Copyright 2010-2018 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -224,6 +224,70 @@ public MyBatisBatchItemWriter<User> writer() {
224224
.build();
225225
}]]></source>
226226

227+
<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>
228+
229+
<p>
230+
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
231+
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
232+
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
233+
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
234+
Follows below a sample:
235+
</p>
236+
237+
<p>
238+
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
239+
</p>
240+
241+
<source><![CDATA[
242+
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
243+
return item -> {
244+
Map<String, Object> parameter = new HashMap<>();
245+
parameter.put("item", item);
246+
parameter.put("operationBy", operationBy);
247+
parameter.put("operationAt", operationAt);
248+
return parameter;
249+
};
250+
}]]></source>
251+
252+
<p>
253+
At next, you write a sql mapping.
254+
</p>
255+
256+
<source><![CDATA[
257+
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
258+
insert into persons (first_name, last_name, operation_by, operation_at)
259+
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
260+
</select>]]></source>
261+
262+
<p>
263+
At last, you configure the <code>MyBatisBatchItemWriter</code>.
264+
</p>
265+
266+
<source><![CDATA[
267+
// Sample for Java Config
268+
@Bean
269+
public MyBatisBatchItemWriter<Person> writer() throws Exception {
270+
return new MyBatisBatchItemWriterBuilder<Person>()
271+
.sqlSessionFactory(sqlSessionFactory())
272+
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
273+
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
274+
.build();
275+
}]]></source>
276+
277+
<source><![CDATA[
278+
<!-- Sample for XML Config -->
279+
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
280+
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
281+
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
282+
<property name="itemToParameterConverter">
283+
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
284+
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
285+
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
286+
</bean>
287+
</property>
288+
</bean>
289+
]]></source>
290+
227291
<p><strong>Writing to different tables using composite writers (with some caveats):</strong></p>
228292

229293
<p>This technique can only be used with MyBatis 3.2+, as there was an

src/site/zh/xdoc/batch.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,72 @@ public MyBatisBatchItemWriter<User> writer() {
200200
.build();
201201
}]]></source>
202202

203+
204+
<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>
205+
206+
<p>
207+
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
208+
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
209+
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
210+
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
211+
Follows below a sample:
212+
</p>
213+
214+
<p>
215+
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
216+
</p>
217+
218+
<source><![CDATA[
219+
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
220+
return item -> {
221+
Map<String, Object> parameter = new HashMap<>();
222+
parameter.put("item", item);
223+
parameter.put("operationBy", operationBy);
224+
parameter.put("operationAt", operationAt);
225+
return parameter;
226+
};
227+
}]]></source>
228+
229+
<p>
230+
At next, you write a sql mapping.
231+
</p>
232+
233+
<source><![CDATA[
234+
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
235+
insert into persons (first_name, last_name, operation_by, operation_at)
236+
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
237+
</select>]]></source>
238+
239+
<p>
240+
At last, you configure the <code>MyBatisBatchItemWriter</code>.
241+
</p>
242+
243+
<source><![CDATA[
244+
// Sample for Java Config
245+
@Bean
246+
public MyBatisBatchItemWriter<Person> writer() throws Exception {
247+
return new MyBatisBatchItemWriterBuilder<Person>()
248+
.sqlSessionFactory(sqlSessionFactory())
249+
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
250+
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
251+
.build();
252+
}]]></source>
253+
254+
<source><![CDATA[
255+
<!-- Sample for XML Config -->
256+
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
257+
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
258+
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
259+
<property name="itemToParameterConverter">
260+
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
261+
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
262+
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
263+
</bean>
264+
</property>
265+
</bean>
266+
]]></source>
267+
268+
203269
<p><strong>使用复合 writer 对多个表进行写入(但带有问题):</strong></p>
204270

205271
<p>

0 commit comments

Comments
 (0)