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
The extension method shows the use of the `SelectCompleter` type alias. This is a DSL extension supplied with the library. We will detail its use below. For now see that the extension method can be used in client code as follows:
@@ -94,10 +94,10 @@ This is a standard method for MyBatis Dynamic SQL that executes a query and retu
94
94
95
95
```kotlin
96
96
fun PersonMapper.count(completer:CountCompleter) =
This method shows the use of `CountCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. Clients can use the method as follows:
100
+
This method shows the use of `CountCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. This also shows use of the Kotlin `count` method which is supplied by the library. That method will build and execute the select count statement with the supplied where clause. Clients can use the method as follows:
101
101
102
102
```kotlin
103
103
val rows = mapper.count {
@@ -131,10 +131,10 @@ This is a standard method for MyBatis Dynamic SQL that executes a delete and ret
131
131
132
132
```kotlin
133
133
fun PersonMapper.delete(completer:DeleteCompleter) =
This method shows the use of `DeleteCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. Clients can use the method as follows:
137
+
This method shows the use of `DeleteCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. This also shows use of the Kotlin `deleteFrom` method which is supplied by the library. That method will build and execute the delete statement with the supplied where clause. Clients can use the method as follows:
138
138
139
139
```kotlin
140
140
val rows = mapper.delete {
@@ -169,43 +169,43 @@ These methods can be used to implement simplified insert methods with Kotlin ext
169
169
170
170
```kotlin
171
171
fun PersonMapper.insert(record:PersonRecord) =
172
-
insert(this::insert, record, Person) {
173
-
map(id).toProperty("id")
174
-
map(firstName).toProperty("firstName")
175
-
map(lastName).toProperty("lastName")
176
-
map(birthDate).toProperty("birthDate")
177
-
map(employed).toProperty("employed")
178
-
map(occupation).toProperty("occupation")
179
-
map(addressId).toProperty("addressId")
180
-
}
172
+
insert(this::insert, record, Person) {
173
+
map(id).toProperty("id")
174
+
map(firstName).toProperty("firstName")
175
+
map(lastName).toProperty("lastName")
176
+
map(birthDate).toProperty("birthDate")
177
+
map(employed).toProperty("employed")
178
+
map(occupation).toProperty("occupation")
179
+
map(addressId).toProperty("addressId")
180
+
}
181
181
182
182
fun PersonMapper.insertMultiple(varargrecords:PersonRecord) =
183
-
insertMultiple(records.toList())
183
+
insertMultiple(records.toList())
184
184
185
185
fun PersonMapper.insertMultiple(records:Collection<PersonRecord>) =
Note these methods use Kotlin utility methods named `insert` and `insertMultiple`. Both methods accept a function with a receiver that will allow column mappings.
208
+
Note these methods use Kotlin utility methods named `insert` and `insertMultiple`. Both methods accept a function with a receiver that will allow column mappings. The methods will build and execute insert statements.= with the supplied column mappings.
209
209
210
210
Clients use these methods as follows:
211
211
@@ -254,24 +254,24 @@ These methods can be used to create simplified select methods with Kotlin extens
These methods show the use of `SelectCompleter` which is a which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. 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. The `selectOne` method is used to create a generalized select method where a user can specify a where clause. These methods also show the use of the built in Kotlin functions `selectDistinct`and `selectList`. These functions help to avoid platform type issues in Kotlin and enable the Kotlin compiler to correctly infer the result type (`List<PersonRecord>` in this case).
266
+
These methods show the use of `SelectCompleter` which is a which is a Kotlin typealias for a function with a receiver that will allow a user to supply a where clause. 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. The `selectOne` method is used to create a generalized select method where a user can specify a where clause. These methods also show the use of the built in Kotlin functions `selectDistinct`, `selectList`, and `selectOne`. These functions build and execute select statements, and help to avoid platform type issues in Kotlin. They enable the Kotlin compiler to correctly infer the result type (either `PersonRecord?` or `List<PersonRecord>` in this case).
267
267
268
268
The general `selectOne` method can also be used to implement a `selectByPrimaryKey` method:
269
269
270
270
```kotlin
271
271
fun PersonMapper.selectByPrimaryKey(id_:Int) =
272
-
selectOne {
273
-
where(id, isEqualTo(id_))
274
-
}
272
+
selectOne {
273
+
where(id, isEqualTo(id_))
274
+
}
275
275
```
276
276
277
277
Clients can use the methods as follows:
@@ -317,10 +317,10 @@ This is a standard method for MyBatis Dynamic SQL that executes an update and re
317
317
318
318
```kotlin
319
319
fun PersonMapper.update(completer:UpdateCompleter) =
This extension method shows the use of `UpdateCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply values and a where clause. Clients can use the method as follows:
323
+
This extension method shows the use of `UpdateCompleter` which is a Kotlin typealias for a function with a receiver that will allow a user to supply values and a where clause. This also shows use of the Kotlin `update` method which is supplied by the library. That method will build and execute the update statement with the supplied values and where clause. Clients can use the method as follows:
324
324
325
325
```kotlin
326
326
val rows = mapper.update {
@@ -341,15 +341,15 @@ It is also possible to write a utility method that will set values. For example:
341
341
342
342
```kotlin
343
343
fun UpdateDSL<UpdateModel>.updateSelectiveColumns(record:PersonRecord) =
This method will selectively set values if corresponding fields in a record are non null. This method can be used as follows:
@@ -360,3 +360,26 @@ val rows = mapper.update {
360
360
where(id, isEqualTo(100))
361
361
}
362
362
```
363
+
## Join Support
364
+
365
+
There are extension functions that support building a reusable select method based on a join. In this way, you can create the start of the select statement (the column list and join specifications) and allow the user to supply where clauses and other parts of a select statement. For example, you could code a mapper extension method like this:
366
+
367
+
```kotlin
368
+
fun PersonWithAddressMapper.select(completer:SelectCompleter): List<PersonWithAddress> {
369
+
val start = select(columnList).from(Person, "p") {
This method creates the start of a select statement with a join, and accepts user input to complete the statement. This shows use of and overloaded `selectList` method that accepts the start of a select statement and a completer. Like other select methods, this method can be used as follows:
0 commit comments