Skip to content

Commit 474dbfb

Browse files
committed
Documentation for Spring Batch support
1 parent 6b1cc99 commit 474dbfb

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/site/markdown/docs/springBatch.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Spring Batch Support
2+
This library provides some utilities to make it easier to interact with the MyBatis Spring Batch support.
3+
4+
## The Problem
5+
6+
MyBatis Spring support provides utility classes for interacting with Spring Batch (see [http://www.mybatis.org/spring/batch.html](http://www.mybatis.org/spring/batch.html)). These classes are specialized implementations of Spring Batch's `ItemReader` and `ItemWriter` interfaces that have support for MyBatis mappers.
7+
8+
The `ItemWriter` implementations work with SQL generated by MyBatis Dynamic SQL with no modification needed.
9+
10+
The `ItemReader` implementations need special care. Those classes assume that all query parameters will be placed in a Map (as per usual when using multiple parameters in a query). MyBatis Dynamic SQL, by default, builds a parameter object that should be the only parameter in a query and will not work when placed in a Map of parameters.
11+
12+
## The Solution
13+
14+
The solution involves these steps:
15+
16+
1. The SQL must be rendered such that the parameter markers are aware of the enclosing parameter Map in the `ItemReader`
17+
1. The `SelectStatamentProvider` must be placed in the `ItemReader` parameter Map with a known key.
18+
1. The `@SelectProvider` must be configured to be aware of the enclosing parameter Map
19+
20+
MyBatis Dynamic SQL provides utilities for each of these requirements. Each utility uses a shared Map key for consistency.
21+
22+
### Specialized Rendering
23+
24+
MyBatis Dynamic SQL provides a specialized rendering strategy for queries used with the MyBatis Spring `ItemReader` implementations. Queries should be rendered as follows:
25+
26+
```java
27+
SelectStatementProvider selectStatement = SelectDSL.select(person.allColumns())
28+
.from(person)
29+
.where(lastName, isEqualTo("flintstone"))
30+
.build()
31+
.render(SpringBatchUtility.SPRING_BATCH_READER_RENDERING_STRATEGY);
32+
```
33+
34+
### Creating the Parameter Map
35+
36+
The `SpringBatchUtility` provides a method to create the parameter values Map needed by the MyBatis Spring `ItemReader`. It can be used as follows:
37+
38+
```java
39+
MyBatisCursorItemReader<Person> reader = new MyBatisCursorItemReader<>();
40+
reader.setQueryId(PersonMapper.class.getName() + ".selectMany");
41+
reader.setSqlSessionFactory(sqlSessionFactory);
42+
reader.setParameterValues(SpringBatchUtility.toParameterValues(selectStatement));
43+
```
44+
45+
### Specialized @SelectProvider Adapter
46+
47+
MyBatis mappers should be configured to use the specialized `@SelectProvider` adapter as follows:
48+
49+
```java
50+
@SelectProvider(type=SpringBatchProviderAdapter.class, method="select")
51+
@Results({
52+
@Result(column="id", property="id", id=true),
53+
@Result(column="first_name", property="firstName"),
54+
@Result(column="last_name", property="lastName")
55+
})
56+
List<Person> selectMany(Map<String, Object> parameterValues);
57+
```
58+
59+
## Complete Example
60+
61+
The unit tests for MyBatis Dynamic SQL include a complete example of using MyBatis Spring Batch support using both a reader and writer. You can see the full example here: [https://github.com/mybatis/mybatis-dynamic-sql/tree/master/src/test/java/examples/springbatch](https://github.com/mybatis/mybatis-dynamic-sql/tree/master/src/test/java/examples/springbatch)

src/site/site.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<item href="docs/update.html" name="UPDATE Statements" />
4646
<item href="docs/mybatis3.html" name="MyBatis3 Support" />
4747
<item href="docs/spring.html" name="Spring Support" />
48+
<item href="docs/springBatch.html" name="Spring Batch Support" />
4849
<item href="docs/howItWorks.html" name="How it Works" />
4950
<item href="docs/extending.html" name="Extending the Library" />
5051
<item href="docs/codingStandards.html" name="Coding Standards" />

src/test/java/examples/springbatch/PersonMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package examples.springbatch;
1717

1818
import java.util.List;
19+
import java.util.Map;
1920

2021
import org.apache.ibatis.annotations.Result;
2122
import org.apache.ibatis.annotations.Results;
@@ -35,7 +36,7 @@ public interface PersonMapper {
3536
@Result(column="first_name", property="firstName"),
3637
@Result(column="last_name", property="lastName")
3738
})
38-
List<Person> selectMany(SelectStatementProvider selectStatement);
39+
List<Person> selectMany(Map<String, Object> parameterValues);
3940

4041
@UpdateProvider(type=SqlProviderAdapter.class, method="update")
4142
int update(UpdateStatementProvider updateStatement);

0 commit comments

Comments
 (0)