Skip to content

Commit c77ba58

Browse files
committed
Documentation
1 parent 91a0810 commit c77ba58

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

src/site/markdown/docs/mybatis3.md

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,50 @@ The goal of this support is to reduce the amount of boilerplate code needed for
55

66
With version 1.1.3, specialized interfaces and utilities were added that can further simplify client code. This support enables the creation of methods that have similar functionality to some of the methods generated in previous versions of MyBatis generator like countByExample, deleteByExample, and selectByExample. We no longer use the "by example" terms for these methods as this library has eliminated the Example class that was generated by prior versions of MyBatis Generator.
77

8-
## Dynamic Result Mapping Support
9-
MyBatis is very good at mapping result sets to objects - this is one of its primary differentiators. MyBatis also requires
10-
that you predefine the mappings for every possibility. This presents a challenge if you want very dynamic column lists
11-
in a query. This library provides a generalized MyBatis mapper that can assist with that problem.
12-
13-
The general mapper is `org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper`. This mapper can be injected into a MyBatis configuration
14-
as is, or it can be extended by an existing mapper. If you are using MyBatis Spring support and auto scanning for mappers,
15-
you can create an extension in your application's mapper package as follows:
8+
## Common Mapper Support
9+
The library includes several common mappers for MyBatis that can be injected into a MyBatis configuration as-is, or can be
10+
extended. These mappers can be used to eliminate repetitive boilerplate code for several operations - namely count queries,
11+
deletes, inserts, and updates. In addition, there is a common select mapper that can be used to avoid writing custom
12+
result maps for every query. The common select mapper provides a row mapper function that is very similar to Spring
13+
JDBC template.
14+
15+
### Common Count, Delete, Insert, and Update Mappers
16+
These mappers provide utility functions that execute simple queries. They can be used as-as, or can be extended. They
17+
provide methods as follows:
18+
19+
Mapper | Methods(s)
20+
---|---
21+
`org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper` | `long count(SelectStatementProvider)`
22+
`org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper` | `int delete(DeleteStatementProvider)`
23+
`org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper<T>` | `int insert(InsertStatementProvider<T>)`<br/>`int generalInsert(GeneralInsertStatementProvider)`<br/>`int insertSelect(InsertSelectStatementProvider)`<br/>`int insertMultiple(MultiRowInsertStatementProvider<T>)`
24+
`org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper` | `int update(UpdateStatementProvider)`
25+
26+
These mappers, as well as the common selectmapper, can be used to create a general purpose CRUD mapper as follows:
1627

1728
```java
18-
package foo.mapper;
19-
2029
import org.apache.ibatis.annotations.Mapper;
30+
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper;
31+
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper;
32+
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper;
2133
import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper;
34+
import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper;
2235

2336
@Mapper
24-
public interface MyGeneralMapper extends CommonSelectMapper {
37+
public interface FooMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<Foo>, CommonSelectMapper,
38+
CommonUpdateMapper {
2539
}
2640
```
2741

42+
This mapper can be extended with default methods as shown below.
43+
44+
### Common Select Mapper
45+
MyBatis is very good at mapping result sets to objects - this is one of its primary differentiators. MyBatis also requires
46+
that you predefine the mappings for every possibility. This presents a challenge if you want very dynamic column lists
47+
in a query. This library provides a generalized MyBatis mapper that can assist with that problem.
48+
49+
The general mapper is `org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper`. This mapper can be injected into a MyBatis configuration
50+
as is, or it can be extended by an existing mapper.
51+
2852
The mapper contains three types of methods:
2953

3054
1. The `selectOneMappedRow` and `selectManyMappedRows` methods allow you to use select statements with
@@ -111,10 +135,11 @@ import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
111135
import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper;
112136

113137
public class MyService {
114-
public Long getCount() {
138+
public Long getAverageAge() {
115139
CommonSelectMapper mapper = getGeneralMapper(); // not shown
116140

117-
SelectStatementProvider selectStatement = countFrom(foo)
141+
SelectStatementProvider selectStatement = select(avg(age))
142+
.from(foo)
118143
.where(description. isLike("%bar%"))
119144
.build()
120145
.render(RenderingStrategies.MYBATIS3);
@@ -134,7 +159,10 @@ To use this support, we envision creating several methods on a MyBatis mapper in
134159
long count(SelectStatementProvider selectStatement);
135160
```
136161

137-
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `long`. The other methods will reuse this method and supply everything needed to build the select statement except the where clause. There are several variants of count queries that may be useful:
162+
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `long`. The other methods will reuse
163+
this method and supply everything needed to build the select statement except the where clause. In lieu of writing this method,
164+
you could extend `org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper` instead. There are several variants
165+
of count queries that may be useful:
138166

139167
1. `count(*)` - counts the number of rows that match a where clause
140168
1. `count(column)` - counts the number of non-null column values that match a where clause
@@ -180,7 +208,9 @@ To use this support, we envision creating two methods on a MyBatis mapper interf
180208
int delete(DeleteStatementProvider deleteStatement);
181209
```
182210

183-
This is a standard method for MyBatis Dynamic SQL that executes a delete and returns an `int` - the number of rows deleted. The second method will reuse this method and supply everything needed to build the delete statement except the where clause:
211+
This is a standard method for MyBatis Dynamic SQL that executes a delete and returns an `int` - the number of rows deleted.
212+
In lieu of writing this method, you could extend `org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper` instead.
213+
The second method will reuse this method and supply everything needed to build the delete statement except the where clause:
184214

185215
```java
186216
default int delete(DeleteDSLCompleter completer) {
@@ -218,7 +248,9 @@ int generalInsert(GeneralInsertStatementProvider insertStatement);
218248
int insertMultiple(MultiRowInsertStatementProvider<PersonRecord> insertStatement);
219249
```
220250

221-
These methods are standard methods for MyBatis Dynamic SQL. They execute a single row insert, a general insert, and a multiple row insert.
251+
These methods are standard methods for MyBatis Dynamic SQL. They execute a single row insert, a general insert, and a
252+
multiple row insert. In lieu of writing these methods, you could extend
253+
`org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper` instead.
222254

223255
These methods can be used to implement simplified insert methods:
224256

@@ -357,7 +389,10 @@ To use this support, we envision creating several methods on a MyBatis mapper in
357389
int update(UpdateStatementProvider updateStatement);
358390
```
359391

360-
This is a standard method for MyBatis Dynamic SQL that executes a query and returns an `int` - the number of rows updated. The second method will reuse this method and supply everything needed to build the update statement except the values and the where clause:
392+
This is a standard method for MyBatis Dynamic SQL that executes a query and returns an `int` - the number of rows updated.
393+
In lieu of writing this method, you could extend `org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper` instead.
394+
The second method will reuse this method and supply everything needed to build the update statement except the values
395+
and the where clause:
361396

362397
```java
363398
default int update(UpdateDSLCompleter completer) {

0 commit comments

Comments
 (0)