You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. In addition to the examples shown elsewhere, the library has additional specialized support for MyBatis3 beyond what is shown in the other examples. This support mainly exists to support MyBatis Generator and the code generated by that tool. Even without MyBatis Generator, some of the techniques shown on this page may prove useful.
3
3
4
-
This support is added to the DELETE, SELECT, and UPDATE statement generators and enables the creating of reusable 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.
4
+
The goal of this support is to reduce the amount of boilerplate code needed for a typical CRUD mapper. For example, this support allows you to create a reusable SELECT method where the user only needs to specify a WHERE clause.
5
5
6
-
With version 1.1.3, specialized interfaces 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.
6
+
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.
7
7
8
8
## Count Method Support
9
9
@@ -19,25 +19,22 @@ long count(SelectStatementProvider selectStatement);
19
19
This is a standard method for MyBatis Dynamic SQL that executes a query and returns a `long`. The second method will reuse this method and supply everything needed to build the select statement except the where clause:
This method shows the use of `MyBatis3CountHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows:
27
+
This method shows the use of `MyBatis3SelectCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows:
31
28
32
29
```java
33
-
long rows = mapper.count(h->
34
-
h.where(occupation, isNull()));
30
+
long rows = mapper.count(c->
31
+
c.where(occupation, isNull()));
35
32
```
36
33
37
34
There is a utility method that can be used to count all rows in a table:
38
35
39
36
```java
40
-
long rows = mapper.count(MyBatis3CountHelper.allRows());
37
+
long rows = mapper.count(MyBatis3SelectCompleter.allRows());
41
38
```
42
39
43
40
## Delete Method Support
@@ -54,26 +51,74 @@ int delete(DeleteStatementProvider deleteStatement);
54
51
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:
This method shows the use of `MyBatis3DeleteHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows:
59
+
This method shows the use of `MyBatis3DeleteCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause. Clients can use the method as follows:
65
60
66
61
```java
67
-
int rows = mapper.delete(h->
68
-
h.where(occupation, isNull()));
62
+
int rows = mapper.delete(c->
63
+
c.where(occupation, isNull()));
69
64
```
70
65
71
66
There is a utility method that can be used to delete all rows in a table:
72
67
73
68
```java
74
-
int rows = mapper.delete(MyBatis3DeleteHelper.allRows());
69
+
int rows = mapper.delete(MyBatis3DeleteCompleter.allRows());
75
70
```
76
71
72
+
## Insert Method Support
73
+
74
+
The goal of insert method support is to remove some of the boilerplate code from insert methods in a mapper interfaces.
75
+
76
+
To use this support, we envision creating several methods on a MyBatis mapper interface. The first two methods are the standard MyBatis Dynamic SQL method that will execute an insert:
returnMyBatis3Utils.insertMultiple(this::insertMultiple, records, person, c ->
109
+
c.map(id).toProperty("id")
110
+
.map(firstName).toProperty("firstName")
111
+
.map(lastName).toProperty("lastName")
112
+
.map(birthDate).toProperty("birthDate")
113
+
.map(employed).toProperty("employed")
114
+
.map(occupation).toProperty("occupation")
115
+
.map(addressId).toProperty("addressId")
116
+
);
117
+
}
118
+
```
119
+
120
+
In the mapper, only the column mappings need to be specified and no other boilerplate code is needed.
121
+
77
122
## Select Method Support
78
123
79
124
The goal of select method support is to enable the creation of methods that execute a select statement allowing a user to specify a where clause and/or order by clause at runtime, but abstracting away all other details.
@@ -82,89 +127,84 @@ To use this support, we envision creating several methods on a MyBatis mapper in
This method shows the use of `MyBatis3SelectOneHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause.
162
+
This method shows the use of `MyBatis3SelectCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause.
114
163
115
164
The general `selectOne` method can be used to implement a `selectByPrimaryKey` method:
The `selectMany` method can be used to implement generalized select methods where a user can specify a where clause and/or an order by clause. Typically we recommend two of these methods - for select, and select distinct:
These methods show the use of `MyBatis3SelectListHelper` which is a specialization of a `java.util.Function` that will allow a user to supply a where clause and/or an order by clause.
147
187
148
188
Clients can use the methods as follows:
149
189
150
190
```java
151
-
List<SimpleTableRecord> rows = mapper.select(h->
152
-
h.where(id, isEqualTo(1))
191
+
List<PersonRecord> rows = mapper.select(c->
192
+
c.where(id, isEqualTo(1))
153
193
.or(occupation, isNull()));
154
194
```
155
195
156
196
There are utility methods that will select all rows in a table:
@@ -181,33 +221,31 @@ int update(UpdateStatementProvider updateStatement);
181
221
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:
This method shows the use of `MyBatis3UpdateHelper` which is a specialization of a `java.util.Function` that will allow a user to supply values and a where clause. Clients can use the method as follows:
229
+
This method shows the use of `MyBatis3UpdateCompleter` which is a specialization of a `java.util.Function` that will allow a user to supply values and a where clause. Clients can use the method as follows:
192
230
193
231
```java
194
-
int rows = mapper.update(h->
195
-
h.set(occupation).equalTo("Programmer")
232
+
int rows = mapper.update(c->
233
+
c.set(occupation).equalTo("Programmer")
196
234
.where(id, isEqualTo(100)));
197
235
```
198
236
199
237
All rows in a table can be updated by simply omitting the where clause:
200
238
201
239
```java
202
-
int rows = mapper.update(h->
203
-
h.set(occupation).equalTo("Programmer"));
240
+
int rows = mapper.update(c->
241
+
c.set(occupation).equalTo("Programmer"));
204
242
```
205
243
206
244
It is also possible to write a utility method that will set values. For example:
Prior to version 1.1.3, it was also possible to write reusable methods, but they were a bit inconsistent with other helper methods.
267
+
Prior to version 1.1.3, it was also possible to write reusable methods, but they were a bit inconsistent with other helper methods. Mappers of this style are deprecated and the support classes for mappers of this style will be removed in a future version of this library.
230
268
231
269
For example, it is possible to write a mapper interface like this:
0 commit comments