-
Notifications
You must be signed in to change notification settings - Fork 211
/
GeneratedAlwaysMapper.kt
71 lines (62 loc) · 3.34 KB
/
GeneratedAlwaysMapper.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package examples.kotlin.mybatis3.canonical
import examples.kotlin.mybatis3.canonical.GeneratedAlwaysDynamicSqlSupport.firstName
import examples.kotlin.mybatis3.canonical.GeneratedAlwaysDynamicSqlSupport.generatedAlways
import examples.kotlin.mybatis3.canonical.GeneratedAlwaysDynamicSqlSupport.lastName
import org.apache.ibatis.annotations.Flush
import org.apache.ibatis.annotations.InsertProvider
import org.apache.ibatis.annotations.Options
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.executor.BatchResult
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
import org.mybatis.dynamic.sql.util.SqlProviderAdapter
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insert
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertBatch
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertMultipleWithGeneratedKeys
interface GeneratedAlwaysMapper {
@InsertProvider(type = SqlProviderAdapter::class, method = "insert")
@Options(useGeneratedKeys = true, keyProperty = "row.id,row.fullName", keyColumn = "id,full_name")
fun insert(insertStatement: InsertStatementProvider<GeneratedAlwaysRecord>): Int
@InsertProvider(type = SqlProviderAdapter::class, method = "generalInsert")
@Options(useGeneratedKeys = true, keyProperty = "parameters.id,parameters.fullName", keyColumn = "id,full_name")
fun generalInsert(insertStatement: GeneralInsertStatementProvider): Int
@InsertProvider(type = SqlProviderAdapter::class, method = "insertMultipleWithGeneratedKeys")
@Options(useGeneratedKeys = true, keyProperty = "records.id,records.fullName", keyColumn = "id,full_name")
fun insertMultiple(insertStatement: String, @Param("records") records: List<GeneratedAlwaysRecord>): Int
@Flush
fun flush(): List<BatchResult>
}
fun GeneratedAlwaysMapper.insert(record: GeneratedAlwaysRecord): Int {
return insert(this::insert, record, generatedAlways) {
map(firstName) toProperty "firstName"
map(lastName) toProperty "lastName"
}
}
fun GeneratedAlwaysMapper.insertMultiple(vararg records: GeneratedAlwaysRecord): Int = insertMultiple(records.asList())
fun GeneratedAlwaysMapper.insertMultiple(records: Collection<GeneratedAlwaysRecord>): Int {
return insertMultipleWithGeneratedKeys(this::insertMultiple, records, generatedAlways) {
map(firstName) toProperty "firstName"
map(lastName) toProperty "lastName"
}
}
fun GeneratedAlwaysMapper.insertBatch(records: Collection<GeneratedAlwaysRecord>): List<Int> {
return insertBatch(this::insert, records, generatedAlways) {
map(firstName) toProperty "firstName"
map(lastName) toProperty "lastName"
}
}