Skip to content

Commit

Permalink
Add explanation for MyBatisBatchItemWriter#itemToParameterConverter o…
Browse files Browse the repository at this point in the history
…n doc

See #21
  • Loading branch information
kazuki43zoo committed May 4, 2018
1 parent df2602e commit 4503621
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 15 deletions.
67 changes: 66 additions & 1 deletion src/site/es/xdoc/batch.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010-2017 the original author or authors.
Copyright 2010-2018 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -226,6 +226,71 @@ public MyBatisBatchItemWriter<User> writer() {
.build();
}]]></source>


<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>

<p>
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
Follows below a sample:
</p>

<p>
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
</p>

<source><![CDATA[
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
return item -> {
Map<String, Object> parameter = new HashMap<>();
parameter.put("item", item);
parameter.put("operationBy", operationBy);
parameter.put("operationAt", operationAt);
return parameter;
};
}]]></source>

<p>
At next, you write a sql mapping.
</p>

<source><![CDATA[
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
insert into persons (first_name, last_name, operation_by, operation_at)
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
</select>]]></source>

<p>
At last, you configure the <code>MyBatisBatchItemWriter</code>.
</p>

<source><![CDATA[
// Sample for Java Config
@Bean
public MyBatisBatchItemWriter<Person> writer() throws Exception {
return new MyBatisBatchItemWriterBuilder<Person>()
.sqlSessionFactory(sqlSessionFactory())
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
.build();
}]]></source>

<source><![CDATA[
<!-- Sample for XML Config -->
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
<property name="itemToParameterConverter">
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
</bean>
</property>
</bean>
]]></source>

<p><strong>Escribiendo en distintas tablas usando composite writers (con algunos condicionantes):</strong></p>

<p>Esta técnica sólo puede usarse con MyBatis 3.2+, por que había un
Expand Down
70 changes: 69 additions & 1 deletion src/site/ja/xdoc/batch.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010-2017 the original author or authors.
Copyright 2010-2018 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -212,6 +212,74 @@ public MyBatisBatchItemWriter<User> writer() {
.build();
}]]></source>


<p><strong>ItemReaderを使用して読み込んだアイテムを任意のパラメータオブジェクトへ変換する</strong></p>

<p>
デフォルトの動作では、<code>MyBatisBatchItemWriter</code> は <code>ItemReader</code> を使用して読みこんだアイテム
(または <code>ItemProcessor</code> によって変換したアイテム)を、
そのままMyBatis(<code>SqlSession</code> の <code>update</code> メソッド)のパラメーターオブジェクトとして渡します。
もしMyBatisへ渡すパラメーターオブジェクトをカスタマイズしたい場合は、
<code>itemToParameterConverter</code> オプションを利用することで実現するすることができます。
たとえば、<code>itemToParameterConverter</code> オプションを使用すると、 アイテムオブジェクト以外のオブジェクトをMyBatisへ渡すことができます。
以下にサンプルを示します。
</p>

<p>
まず、任意のパラメータオブジェクトに変換するためのコンバータクラス(またはファクトリメソッド)を作成します。以下のサンプルではファクトリメソッドを使用します。
</p>

<source><![CDATA[
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
return item -> {
Map<String, Object> parameter = new HashMap<>();
parameter.put("item", item);
parameter.put("operationBy", operationBy);
parameter.put("operationAt", operationAt);
return parameter;
};
}]]></source>

<p>
つぎに, SQLマッピングを書きます。
</p>

<source><![CDATA[
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
insert into persons (first_name, last_name, operation_by, operation_at)
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
</select>]]></source>

<p>
さいごに, <code>MyBatisBatchItemWriter</code>の設定を行います。
</p>

<source><![CDATA[
// Sample for Java Config
@Bean
public MyBatisBatchItemWriter<Person> writer() throws Exception {
return new MyBatisBatchItemWriterBuilder<Person>()
.sqlSessionFactory(sqlSessionFactory())
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
.build();
}]]></source>

<source><![CDATA[
<!-- Sample for XML Config -->
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
<property name="itemToParameterConverter">
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
</bean>
</property>
</bean>
]]></source>


<p><strong>Composite Writer を使って複数のテーブルに書き込む(注意事項あり)</strong></p>

<p>このテクニックを使うには MyBatis 3.2 以降が必要です。それ以前のバージョンには <a href="http://code.google.com/p/mybatis/issues/detail?id=741">問題</a> があるため、Writer が期待通りに動作しません。
Expand Down
68 changes: 67 additions & 1 deletion src/site/ko/xdoc/batch.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010-2017 the original author or authors.
Copyright 2010-2018 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -204,6 +204,72 @@ public MyBatisBatchItemWriter<User> writer() {
.build();
}]]></source>


<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>

<p>
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
Follows below a sample:
</p>

<p>
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
</p>

<source><![CDATA[
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
return item -> {
Map<String, Object> parameter = new HashMap<>();
parameter.put("item", item);
parameter.put("operationBy", operationBy);
parameter.put("operationAt", operationAt);
return parameter;
};
}]]></source>

<p>
At next, you write a sql mapping.
</p>

<source><![CDATA[
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
insert into persons (first_name, last_name, operation_by, operation_at)
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
</select>]]></source>

<p>
At last, you configure the <code>MyBatisBatchItemWriter</code>.
</p>

<source><![CDATA[
// Sample for Java Config
@Bean
public MyBatisBatchItemWriter<Person> writer() throws Exception {
return new MyBatisBatchItemWriterBuilder<Person>()
.sqlSessionFactory(sqlSessionFactory())
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
.build();
}]]></source>

<source><![CDATA[
<!-- Sample for XML Config -->
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
<property name="itemToParameterConverter">
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
</bean>
</property>
</bean>
]]></source>


<p><strong>여러개의 테이블에 데이터를 쓰려면 한꺼번에 처리할 수 있도록 만든 writer(몇가지 규칙을 가지고)를 사용하자. </strong></p>

<p>이 기능은 마이바티스 3.2이상에서만 사용할 수 있다. 이전의 버전에서는 예상과 다르게 동작하는데 그 내용은
Expand Down
66 changes: 65 additions & 1 deletion src/site/xdoc/batch.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010-2017 the original author or authors.
Copyright 2010-2018 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -224,6 +224,70 @@ public MyBatisBatchItemWriter<User> writer() {
.build();
}]]></source>

<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>

<p>
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
Follows below a sample:
</p>

<p>
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
</p>

<source><![CDATA[
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
return item -> {
Map<String, Object> parameter = new HashMap<>();
parameter.put("item", item);
parameter.put("operationBy", operationBy);
parameter.put("operationAt", operationAt);
return parameter;
};
}]]></source>

<p>
At next, you write a sql mapping.
</p>

<source><![CDATA[
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
insert into persons (first_name, last_name, operation_by, operation_at)
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
</select>]]></source>

<p>
At last, you configure the <code>MyBatisBatchItemWriter</code>.
</p>

<source><![CDATA[
// Sample for Java Config
@Bean
public MyBatisBatchItemWriter<Person> writer() throws Exception {
return new MyBatisBatchItemWriterBuilder<Person>()
.sqlSessionFactory(sqlSessionFactory())
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
.build();
}]]></source>

<source><![CDATA[
<!-- Sample for XML Config -->
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
<property name="itemToParameterConverter">
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
</bean>
</property>
</bean>
]]></source>

<p><strong>Writing to different tables using composite writers (with some caveats):</strong></p>

<p>This technique can only be used with MyBatis 3.2+, as there was an
Expand Down
66 changes: 66 additions & 0 deletions src/site/zh/xdoc/batch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,72 @@ public MyBatisBatchItemWriter<User> writer() {
.build();
}]]></source>


<p><strong>Converting a item that read using ItemReader to an any parameter object:</strong></p>

<p>
By default behavior, the <code>MyBatisBatchItemWriter</code> passes a item that read using <code>ItemReader</code>
(or convert by <code>ItemProcessor</code>) to the MyBatis(<code>SqlSession#update()</code>) as the parameter object.
If you want to customize a parameter object that passes to the MyBatis, you can realize to use the <code>itemToParameterConverter</code> option.
For example using <code>itemToParameterConverter</code> option, you can passes any objects other than the item object to the MyBatis.
Follows below a sample:
</p>

<p>
At first, you create a custom converter class (or factory method). The following sample uses a factory method.
</p>

<source><![CDATA[
public static <T> Converter<T, Map<String, Object>> createItemToParameterMapConverter(String operationBy, LocalDateTime operationAt) {
return item -> {
Map<String, Object> parameter = new HashMap<>();
parameter.put("item", item);
parameter.put("operationBy", operationBy);
parameter.put("operationAt", operationAt);
return parameter;
};
}]]></source>

<p>
At next, you write a sql mapping.
</p>

<source><![CDATA[
<select id="createPerson" resultType="org.mybatis.spring.sample.domain.Person">
insert into persons (first_name, last_name, operation_by, operation_at)
values(#{item.firstName}, #{item.lastName}, #{operationBy}, #{operationAt})
</select>]]></source>

<p>
At last, you configure the <code>MyBatisBatchItemWriter</code>.
</p>

<source><![CDATA[
// Sample for Java Config
@Bean
public MyBatisBatchItemWriter<Person> writer() throws Exception {
return new MyBatisBatchItemWriterBuilder<Person>()
.sqlSessionFactory(sqlSessionFactory())
.statementId("org.mybatis.spring.sample.mapper.PersonMapper.createPerson")
.itemToParameterConverter(createItemToParameterMapConverter("batch_java_config_user", LocalDateTime.now()))
.build();
}]]></source>

<source><![CDATA[
<!-- Sample for XML Config -->
<bean id="writer" class="org.mybatis.spring.batch.MyBatisBatchItemWriter">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<property name="statementId" value="org.mybatis.spring.sample.mapper.PersonMapper.createPerson"/>
<property name="itemToParameterConverter">
<bean class="org.mybatis.spring.sample.config.SampleJobConfig" factory-method="createItemToParameterMapConverter">
<constructor-arg type="java.lang.String" value="batch_xml_config_user"/>
<constructor-arg type="java.time.LocalDateTime" value="#{T(java.time.LocalDateTime).now()}"/>
</bean>
</property>
</bean>
]]></source>


<p><strong>使用复合 writer 对多个表进行写入(但带有问题):</strong></p>

<p>
Expand Down
Loading

0 comments on commit 4503621

Please sign in to comment.