Skip to content

Commit 681fdc6

Browse files
committed
Coverage
1 parent a65451d commit 681fdc6

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.kotlin.mybatis3.canonical
17+
18+
import org.mybatis.dynamic.sql.SqlTable
19+
import java.sql.JDBCType
20+
21+
object GeneratedAlwaysDynamicSqlSupport {
22+
val generatedAlways = GeneratedAlways()
23+
val id = generatedAlways.id
24+
val firstName = generatedAlways.firstName
25+
val lastName = generatedAlways.lastName
26+
val fullName = generatedAlways.fullName
27+
28+
class GeneratedAlways : SqlTable("GeneratedAlways") {
29+
val id = column<Int>("id", JDBCType.INTEGER)
30+
val firstName = column<String>("first_name", JDBCType.VARCHAR)
31+
val lastName = column<String>("last_name", JDBCType.VARCHAR)
32+
val fullName = column<String>("full_name", JDBCType.VARCHAR)
33+
}
34+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.kotlin.mybatis3.canonical
17+
18+
import examples.kotlin.mybatis3.canonical.GeneratedAlwaysDynamicSqlSupport.firstName
19+
import examples.kotlin.mybatis3.canonical.GeneratedAlwaysDynamicSqlSupport.generatedAlways
20+
import examples.kotlin.mybatis3.canonical.GeneratedAlwaysDynamicSqlSupport.lastName
21+
import org.apache.ibatis.annotations.InsertProvider
22+
import org.apache.ibatis.annotations.Options
23+
import org.apache.ibatis.annotations.Param
24+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider
25+
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider
26+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter
27+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insert
28+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertMultipleWithGeneratedKeys
29+
30+
interface GeneratedAlwaysMapper {
31+
@InsertProvider(type = SqlProviderAdapter::class, method = "insert")
32+
@Options(useGeneratedKeys = true, keyProperty="record.id,record.fullName", keyColumn = "id,full_name")
33+
fun insert(insertStatement: InsertStatementProvider<GeneratedAlwaysRecord>): Int
34+
35+
@InsertProvider(type = SqlProviderAdapter::class, method = "generalInsert")
36+
fun generalInsert(insertStatement: GeneralInsertStatementProvider): Int
37+
38+
@InsertProvider(type = SqlProviderAdapter::class, method = "insertMultipleWithGeneratedKeys")
39+
@Options(useGeneratedKeys = true, keyProperty="records.id,records.fullName", keyColumn = "id,full_name")
40+
fun insertMultiple(insertStatement: String, @Param("records") records: List<GeneratedAlwaysRecord>): Int
41+
}
42+
43+
fun GeneratedAlwaysMapper.insert(record: GeneratedAlwaysRecord): Int {
44+
return insert(this::insert, record, generatedAlways) {
45+
map(firstName).toProperty("firstName")
46+
map(lastName).toProperty("lastName")
47+
}
48+
}
49+
50+
fun GeneratedAlwaysMapper.insertMultiple(records: Collection<GeneratedAlwaysRecord>): Int {
51+
return insertMultipleWithGeneratedKeys(this::insertMultiple, records, generatedAlways) {
52+
map(firstName).toProperty("firstName")
53+
map(lastName).toProperty("lastName")
54+
}
55+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.kotlin.mybatis3.canonical
17+
18+
data class GeneratedAlwaysRecord(
19+
var id: Int? = null,
20+
var firstName: String?,
21+
var lastName: String?,
22+
var fullName: String? = null
23+
)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package examples.kotlin.mybatis3.canonical
17+
18+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource
19+
import org.apache.ibatis.jdbc.ScriptRunner
20+
import org.apache.ibatis.mapping.Environment
21+
import org.apache.ibatis.session.Configuration
22+
import org.apache.ibatis.session.ExecutorType
23+
import org.apache.ibatis.session.SqlSession
24+
import org.apache.ibatis.session.SqlSessionFactoryBuilder
25+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory
26+
import org.assertj.core.api.Assertions.assertThat
27+
import org.junit.jupiter.api.Test
28+
import java.io.InputStreamReader
29+
import java.sql.DriverManager
30+
31+
class GeneratedAlwaysTest {
32+
private fun newSession(executorType: ExecutorType = ExecutorType.REUSE): SqlSession {
33+
Class.forName(JDBC_DRIVER)
34+
val script = javaClass.getResourceAsStream("/examples/kotlin/spring/CreateGeneratedAlwaysDB.sql")
35+
DriverManager.getConnection(JDBC_URL, "sa", "").use { connection ->
36+
val sr = ScriptRunner(connection)
37+
sr.setLogWriter(null)
38+
sr.runScript(InputStreamReader(script))
39+
}
40+
41+
val ds = UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "")
42+
val environment = Environment("test", JdbcTransactionFactory(), ds)
43+
val config = Configuration(environment)
44+
config.addMapper(GeneratedAlwaysMapper::class.java)
45+
return SqlSessionFactoryBuilder().build(config).openSession(executorType)
46+
}
47+
48+
@Test
49+
fun testInsertSingleRecord() {
50+
newSession().use { session ->
51+
val mapper = session.getMapper(GeneratedAlwaysMapper::class.java)
52+
53+
val record = GeneratedAlwaysRecord(
54+
firstName = "Fred",
55+
lastName = "Flintstone"
56+
)
57+
58+
val rows = mapper.insert(record)
59+
60+
assertThat(rows).isEqualTo(1)
61+
assertThat(record.id).isEqualTo(22)
62+
assertThat(record.fullName).isEqualTo("Fred Flintstone")
63+
}
64+
}
65+
66+
@Test
67+
fun testInsertMultiple() {
68+
newSession().use { session ->
69+
val mapper = session.getMapper(GeneratedAlwaysMapper::class.java)
70+
71+
val record1 = GeneratedAlwaysRecord(
72+
firstName = "Fred",
73+
lastName = "Flintstone"
74+
)
75+
76+
val record2 = GeneratedAlwaysRecord(
77+
firstName = "Barney",
78+
lastName = "Rubble"
79+
)
80+
81+
val rows = mapper.insertMultiple(listOf(record1, record2))
82+
83+
assertThat(rows).isEqualTo(2)
84+
assertThat(record1.id).isEqualTo(22)
85+
assertThat(record1.fullName).isEqualTo("Fred Flintstone")
86+
assertThat(record2.id).isEqualTo(23)
87+
assertThat(record2.fullName).isEqualTo("Barney Rubble")
88+
}
89+
}
90+
91+
companion object {
92+
const val JDBC_URL = "jdbc:hsqldb:mem:aname"
93+
const val JDBC_DRIVER = "org.hsqldb.jdbcDriver"
94+
}
95+
}

0 commit comments

Comments
 (0)