Files

48 lines
2.6 KiB
Markdown

# mybatis
Companion code for **MyBatis with Spring Boot 4: Mappers, Dynamic SQL and Pagination** on [ankurm.com](https://ankurm.com).
A small `category` / `product` schema on in-memory H2, annotation and XML mappers over it, result maps, dynamic SQL,
PageHelper pagination, and `@MybatisTest`. Six transcripts record what MyBatis really does, including the traps.
## Versions
| Component | Version |
|---|---|
| Spring Boot | 4.2.0 |
| Spring Framework | 7.1.0 |
| `mybatis-spring-boot-starter` | 4.1.0 (MyBatis 3.5.19, MyBatis-Spring 4.1.0) |
| `pagehelper-spring-boot-starter` | 4.1.1 (PageHelper 6.1.1) |
| H2 | 2.5.252 |
| JDK | 25 (Temurin 25.0.5) |
## Run it
```bash
./scripts/run-all.sh # runs the tests, which regenerate everything in output/
```
No Docker and no database to install.
## Source
| File | What it shows |
|---|---|
| [`ProductMapper.java`](src/main/java/com/ankurm/mybatis/mapper/ProductMapper.java) | annotation mapper: `@Select`, `@Insert` with generated keys, `@Update`, `@Delete`, `@Results` |
| [`ProductXmlMapper.java`](src/main/java/com/ankurm/mybatis/mapper/ProductXmlMapper.java) / [`ProductXmlMapper.xml`](src/main/resources/mappers/ProductXmlMapper.xml) | XML mapper: result map with association, dynamic SQL (`where`, `if`, `choose`, `foreach`, `set`, `bind`), `#{}` vs `${}` |
| [`CategoryMapper.xml`](src/main/resources/mappers/CategoryMapper.xml) | collection by nested result map (one JOIN) vs nested select (N+1) |
| [`ProductService.java`](src/main/java/com/ankurm/mybatis/service/ProductService.java) | the safe PageHelper form, `doSelectPageInfo` |
| [`application.properties`](src/main/resources/application.properties) | MyBatis and PageHelper settings, SQL logging |
## Captured output
| File | Shows |
|---|---|
| [`01-annotation-mapper.txt`](output/01-annotation-mapper.txt) | annotation mapper statements with the SQL MyBatis ran |
| [`02-result-maps.txt`](output/02-result-maps.txt) | association, JOIN collection (1 statement) vs nested select (4 statements) |
| [`03-dynamic-sql.txt`](output/03-dynamic-sql.txt) | the SQL every dynamic-SQL tag produces, empty `foreach` / `set` failures, `${}` vs `#{}` |
| [`04-pagination.txt`](output/04-pagination.txt) | PageHelper, its two traps, RowBounds, hand-written LIMIT/OFFSET |
| [`05-mybatistest-slice.txt`](output/05-mybatistest-slice.txt) | what `@MybatisTest` starts (and that PageHelper is not in it) |
| [`07-camel-case.txt`](output/07-camel-case.txt) | the missing `categoryId` when `map-underscore-to-camel-case` is off |
| [`06-versions-and-full-context.txt`](output/06-versions-and-full-context.txt) | the versions on the classpath for the run |