|
| 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) |
0 commit comments