Skip to content

Commit e70ab37

Browse files
committed
Documentation for new utility interfaces
1 parent 48ff1a7 commit e70ab37

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

src/site/markdown/docs/mybatis3.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,68 @@
11
# Specialized Support for MyBatis3
2-
Most of the examples shown on this site are for usage with MyBatis3. But the library does have some additional support for MyBatis3 beyond what is shown in the other examples.
2+
Most of the examples shown on this site are for usage with MyBatis3 - even though the library does support other SQL runtimes like Spring JDBC templates. But the library does have some additional specialized support for MyBatis3 beyond what is shown in the other examples.
33

44
This support is added to the DELETE, SELECT, and UPDATE statement generators and enables the creating of reusable "by example" methods as delivered in MyBatis Generator. These methods can provide some boilerplate code for the setup of the statement (a column list and table name for example), and allow the user to specify a where clause.
55

6-
For example, it is possible to write a mapper like this:
6+
With version 1.1.3, specialized interfaces were added that can further simplify client code.
7+
8+
For example, it is possible to write a mapper interface like this:
9+
10+
```java
11+
import static examples.simple.SimpleTableDynamicSqlSupport.*;
12+
13+
import java.util.List;
14+
15+
import org.apache.ibatis.annotations.Mapper;
16+
import org.apache.ibatis.annotations.Result;
17+
import org.apache.ibatis.annotations.Results;
18+
import org.apache.ibatis.annotations.SelectProvider;
19+
import org.apache.ibatis.type.JdbcType;
20+
import org.mybatis.dynamic.sql.select.SelectDSL;
21+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
22+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
23+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectByExampleHelper;
24+
25+
@Mapper
26+
public interface SimpleTableMapper {
27+
28+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
29+
@Results(id="SimpleTableResult", value= {
30+
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
31+
@Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR),
32+
@Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR),
33+
@Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE),
34+
@Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class),
35+
@Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR)
36+
})
37+
List<SimpleTableRecord> selectMany(SelectStatementProvider selectStatement);
38+
39+
default List<SimpleTableRecord> selectByExample(MyBatis3SelectByExampleHelper<SimpleTableRecord> helper) {
40+
return helper.apply(SelectDSL.selectWithMapper(this::selectMany, id, firstName, lastName, birthDate, employed, occupation)
41+
.from(simpleTable))
42+
.build()
43+
.execute();
44+
}
45+
}
46+
```
47+
48+
Notice the `selectByExample` method - it specifies the column list and table name and accepts a lambda expression that can be used to build the WHERE clause. It also reuses the `selectMany` mapper method.
49+
50+
The code is used like this:
51+
52+
```java
53+
List<SimpleTableRecord> rows = mapper.selectByExample(q ->
54+
q.where(id, isEqualTo(1))
55+
.or(occupation, isNull()));
56+
```
57+
58+
59+
It is expected that MyBatis Generator will generate code that looks like this.
60+
61+
62+
## Prior Support
63+
Prior to version 1.1.3, it was also possible to write reusable methods, but they were a bit inconsistent with other helper methods.
64+
65+
For example, it is possible to write a mapper interface like this:
766

867
```java
968
import static examples.simple.SimpleTableDynamicSqlSupport.*;
@@ -22,7 +81,7 @@ import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2281
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
2382

2483
@Mapper
25-
public interface SimpleTableAnnotatedMapper {
84+
public interface SimpleTableMapper {
2685

2786
@SelectProvider(type=SqlProviderAdapter.class, method="select")
2887
@Results(id="SimpleTableResult", value= {
@@ -36,10 +95,9 @@ public interface SimpleTableAnnotatedMapper {
3695
List<SimpleTableRecord> selectMany(SelectStatementProvider selectStatement);
3796

3897
default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<SimpleTableRecord>>> selectByExample() {
39-
return SelectDSL.select(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
98+
return SelectDSL.selectWithMapper(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation)
4099
.from(simpleTable);
41100
}
42-
43101
}
44102
```
45103

@@ -54,5 +112,3 @@ The code is used like this:
54112
.build()
55113
.execute();
56114
```
57-
58-
It is expected that MyBatis Generator will generate code that looks like this.

0 commit comments

Comments
 (0)