Skip to content

Commit 1098287

Browse files
committed
Better Spring pattern for examples
1 parent d2457d3 commit 1098287

9 files changed

+109
-108
lines changed

src/test/java/examples/spring/AddressDynamicSqlSupport.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,6 @@
1515
*/
1616
package examples.spring;
1717

18-
import java.sql.JDBCType;
19-
2018
import org.mybatis.dynamic.sql.SqlColumn;
2119
import org.mybatis.dynamic.sql.SqlTable;
2220

@@ -28,10 +26,10 @@ public final class AddressDynamicSqlSupport {
2826
public static final SqlColumn<String> state = address.state;
2927

3028
public static final class Address extends SqlTable {
31-
public final SqlColumn<Integer> id = column("address_id", JDBCType.INTEGER);
32-
public final SqlColumn<String> streetAddress = column("street_address", JDBCType.VARCHAR);
33-
public final SqlColumn<String> city = column("city", JDBCType.VARCHAR);
34-
public final SqlColumn<String> state = column("state", JDBCType.VARCHAR);
29+
public final SqlColumn<Integer> id = column("address_id");
30+
public final SqlColumn<String> streetAddress = column("street_address");
31+
public final SqlColumn<String> city = column("city");
32+
public final SqlColumn<String> state = column("state");
3533

3634
public Address() {
3735
super("Address");

src/test/java/examples/spring/PersonDynamicSqlSupport.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package examples.spring;
1717

18-
import java.sql.JDBCType;
1918
import java.util.Date;
2019

2120
import org.mybatis.dynamic.sql.SqlColumn;
@@ -32,15 +31,15 @@ public final class PersonDynamicSqlSupport {
3231
public static final SqlColumn<Integer> addressId = person.addressId;
3332

3433
public static final class Person extends SqlTable {
35-
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
36-
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR);
37-
public final SqlColumn<LastName> lastName = column("last_name", JDBCType.VARCHAR)
34+
public final SqlColumn<Integer> id = column("id");
35+
public final SqlColumn<String> firstName = column("first_name");
36+
public final SqlColumn<LastName> lastName = column("last_name")
3837
.withParameterTypeConverter(new LastNameParameterConverter());
39-
public final SqlColumn<Date> birthDate = column("birth_date", JDBCType.DATE);
40-
public final SqlColumn<Boolean> employed = column("employed", JDBCType.VARCHAR)
38+
public final SqlColumn<Date> birthDate = column("birth_date");
39+
public final SqlColumn<Boolean> employed = column("employed")
4140
.withParameterTypeConverter(new YesNoParameterConverter());
42-
public final SqlColumn<String> occupation = column("occupation", JDBCType.VARCHAR);
43-
public final SqlColumn<Integer> addressId = column("address_id", JDBCType.INTEGER);
41+
public final SqlColumn<String> occupation = column("occupation");
42+
public final SqlColumn<Integer> addressId = column("address_id");
4443

4544
public Person() {
4645
super("Person");

src/test/kotlin/examples/kotlin/spring/canonical/AddressDynamicSqlSupport.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,12 @@
1616
package examples.kotlin.spring.canonical
1717

1818
import org.mybatis.dynamic.sql.SqlTable
19-
import java.sql.JDBCType
2019

2120
object AddressDynamicSqlSupport {
2221
object Address : SqlTable("Address") {
23-
val id = column<Int>("address_id", JDBCType.INTEGER)
24-
val streetAddress = column<String>("street_address", JDBCType.VARCHAR)
25-
val city = column<String>("city", JDBCType.VARCHAR)
26-
val state = column<String>("state", JDBCType.VARCHAR)
22+
val id = column<Int>("address_id")
23+
val streetAddress = column<String>("street_address")
24+
val city = column<String>("city")
25+
val state = column<String>("state")
2726
}
2827
}

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTemplateDirectTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ class CanonicalSpringKotlinTemplateDirectTest {
299299

300300
@Test
301301
fun testInsertWithGeneratedKey() {
302-
val record = GeneratedAlwaysRecord(firstName = "Fred", lastName = "Flintstone")
302+
val command = GeneratedAlwaysCommand(firstName = "Fred", lastName = "Flintstone")
303303

304304
val keyHolder = GeneratedKeyHolder()
305305

306306
val rows = template.withKeyHolder(keyHolder) {
307-
insert(record).into(GeneratedAlways) {
307+
insert(command).into(GeneratedAlways) {
308308
map(GeneratedAlways.firstName).toProperty("firstName")
309309
map(GeneratedAlways.lastName).toProperty("lastName")
310310
}
@@ -317,13 +317,13 @@ class CanonicalSpringKotlinTemplateDirectTest {
317317

318318
@Test
319319
fun testMultiRowInsertWithGeneratedKey() {
320-
val record1 = GeneratedAlwaysRecord(firstName = "Fred", lastName = "Flintstone")
321-
val record2 = GeneratedAlwaysRecord(firstName = "Barney", lastName = "Rubble")
320+
val command1 = GeneratedAlwaysCommand(firstName = "Fred", lastName = "Flintstone")
321+
val command2 = GeneratedAlwaysCommand(firstName = "Barney", lastName = "Rubble")
322322

323323
val keyHolder = GeneratedKeyHolder()
324324

325325
val rows = template.withKeyHolder(keyHolder) {
326-
insertMultiple(record1, record2).into(GeneratedAlways) {
326+
insertMultiple(command1, command2).into(GeneratedAlways) {
327327
map(GeneratedAlways.firstName).toProperty("firstName")
328328
map(GeneratedAlways.lastName).toProperty("lastName")
329329
}

src/test/kotlin/examples/kotlin/spring/canonical/CanonicalSpringKotlinTest.kt

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,9 @@ class CanonicalSpringKotlinTest {
417417

418418
@Test
419419
fun testInsertWithGeneratedKey() {
420-
val record = GeneratedAlwaysRecord(firstName = "Fred", lastName = "Flintstone")
420+
val command = GeneratedAlwaysCommand(firstName = "Fred", lastName = "Flintstone")
421421

422-
val insertStatement = insert(record).into(GeneratedAlways) {
422+
val insertStatement = insert(command).into(GeneratedAlways) {
423423
map(GeneratedAlways.firstName).toProperty("firstName")
424424
map(GeneratedAlways.lastName).toProperty("lastName")
425425
}
@@ -434,10 +434,10 @@ class CanonicalSpringKotlinTest {
434434

435435
@Test
436436
fun testMultiRowInsertWithGeneratedKey() {
437-
val record1 = GeneratedAlwaysRecord(firstName = "Fred", lastName = "Flintstone")
438-
val record2 = GeneratedAlwaysRecord(firstName = "Barney", lastName = "Rubble")
437+
val command1 = GeneratedAlwaysCommand(firstName = "Fred", lastName = "Flintstone")
438+
val command2 = GeneratedAlwaysCommand(firstName = "Barney", lastName = "Rubble")
439439

440-
val insertStatement = insertMultiple(record1, record2).into(GeneratedAlways) {
440+
val insertStatement = insertMultiple(command1, command2).into(GeneratedAlways) {
441441
map(GeneratedAlways.firstName).toProperty("firstName")
442442
map(GeneratedAlways.lastName).toProperty("lastName")
443443
}
@@ -993,12 +993,10 @@ class CanonicalSpringKotlinTest {
993993

994994
@Test
995995
fun testUpdateWithTypeConverterAndNullValue() {
996-
val record = PersonRecord(id = 3, firstName = "Sam")
997-
998996
val updateStatement = update(Person) {
999-
set(firstName).equalTo(record::firstName)
1000-
set(lastName).equalTo(record::lastName)
1001-
where(id, isEqualTo(record::id))
997+
set(firstName).equalTo("Sam")
998+
set(lastName).equalTo(null)
999+
where(id, isEqualTo(3))
10021000
}
10031001

10041002
assertThat(updateStatement.updateStatement).isEqualTo(
@@ -1018,7 +1016,7 @@ class CanonicalSpringKotlinTest {
10181016

10191017
val selectStatement = select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
10201018
from(Person)
1021-
where(id, isEqualTo(record::id))
1019+
where(id, isEqualTo(3))
10221020
}
10231021

10241022
val returnedRecord = template.selectOne(selectStatement, personRowMapper)
@@ -1028,12 +1026,10 @@ class CanonicalSpringKotlinTest {
10281026

10291027
@Test
10301028
fun testUpdateWithTypeConverterAndNonNullValue() {
1031-
val record = PersonRecord(id = 3, firstName = "Sam", lastName = LastName("Smith"))
1032-
10331029
val updateStatement = update(Person) {
1034-
set(firstName).equalTo(record::firstName)
1035-
set(lastName).equalTo(record::lastName)
1036-
where(id, isEqualTo(record::id))
1030+
set(firstName).equalTo("Sam")
1031+
set(lastName).equalTo(LastName("Smith"))
1032+
where(id, isEqualTo(3))
10371033
}
10381034

10391035
assertThat(updateStatement.updateStatement).isEqualTo(
@@ -1053,7 +1049,7 @@ class CanonicalSpringKotlinTest {
10531049

10541050
val selectStatement = select(id, firstName, lastName, birthDate, employed, occupation, addressId) {
10551051
from(Person)
1056-
where(id, isEqualTo(record::id))
1052+
where(id, isEqualTo(3))
10571053
}
10581054

10591055
val returnedRecord = template.selectOne(selectStatement, personRowMapper)
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,13 +24,13 @@ val lastNameConverter: (LastName?) -> String? = { it?.name }
2424
val booleanToStringConverter: (Boolean?) -> String = { it?.let { if (it) "Yes" else "No" } ?: "No" }
2525

2626
data class PersonRecord(
27-
var id: Int? = null,
28-
var firstName: String? = null,
29-
var lastName: LastName? = null,
30-
var birthDate: Date? = null,
31-
var employed: Boolean? = null,
32-
var occupation: String? = null,
33-
var addressId: Int? = null
27+
var id: Int,
28+
var firstName: String?,
29+
var lastName: LastName?,
30+
var birthDate: Date?,
31+
var employed: Boolean?,
32+
var occupation: String?,
33+
var addressId: Int?
3434
) {
3535
val lastNameAsString: String?
3636
get() = lastNameConverter(lastName)
@@ -40,25 +40,30 @@ data class PersonRecord(
4040
}
4141

4242
data class PersonWithAddress(
43-
var id: Int? = null,
44-
var firstName: String? = null,
45-
var lastName: LastName? = null,
46-
var birthDate: Date? = null,
47-
var employed: Boolean? = null,
48-
var occupation: String? = null,
49-
var address: AddressRecord? = null
43+
var id: Int,
44+
var firstName: String?,
45+
var lastName: LastName?,
46+
var birthDate: Date?,
47+
var employed: Boolean?,
48+
var occupation: String?,
49+
var address: AddressRecord?
5050
)
5151

5252
data class AddressRecord(
53-
var id: Int? = null,
54-
var streetAddress: String? = null,
55-
var city: String? = null,
56-
var state: String? = null
53+
var id: Int,
54+
var streetAddress: String?,
55+
var city: String?,
56+
var state: String?
57+
)
58+
59+
data class GeneratedAlwaysCommand(
60+
var firstName: String?,
61+
var lastName: String?
5762
)
5863

5964
data class GeneratedAlwaysRecord(
60-
var id: Int? = null,
61-
var firstName: String? = null,
62-
var lastName: String? = null,
63-
var fullName: String? = null
65+
var id: Int,
66+
var firstName: String?,
67+
var lastName: String?,
68+
var fullName: String?
6469
)

src/test/kotlin/examples/kotlin/spring/canonical/GeneratedAlwaysDynamicSqlSupport.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,12 @@
1616
package examples.kotlin.spring.canonical
1717

1818
import org.mybatis.dynamic.sql.SqlTable
19-
import java.sql.JDBCType
2019

2120
object GeneratedAlwaysDynamicSqlSupport {
2221
object GeneratedAlways : SqlTable("GeneratedAlways") {
23-
val id = column<Int>("id", JDBCType.INTEGER)
24-
val firstName = column<String>("first_name", JDBCType.VARCHAR)
25-
val lastName = column<String>("last_name", JDBCType.VARCHAR)
26-
val fullName = column<String>("full_name", JDBCType.VARCHAR)
22+
val id = column<Int>("id")
23+
val firstName = column<String>("first_name")
24+
val lastName = column<String>("last_name")
25+
val fullName = column<String>("full_name")
2726
}
2827
}
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,19 +16,18 @@
1616
package examples.kotlin.spring.canonical
1717

1818
import org.mybatis.dynamic.sql.SqlTable
19-
import java.sql.JDBCType
2019
import java.util.Date
2120

2221
object PersonDynamicSqlSupport {
2322
object Person : SqlTable("Person") {
24-
val id = column<Int>("id", JDBCType.INTEGER)
25-
val firstName = column<String>("first_name", JDBCType.VARCHAR)
26-
val lastName = column<LastName>("last_name", JDBCType.VARCHAR)
23+
val id = column<Int>("id")
24+
val firstName = column<String>("first_name")
25+
val lastName = column<LastName>("last_name")
2726
.withParameterTypeConverter(lastNameConverter)
28-
val birthDate = column<Date>("birth_date", JDBCType.DATE)
29-
val employed = column<Boolean>("employed", JDBCType.VARCHAR)
27+
val birthDate = column<Date>("birth_date")
28+
val employed = column<Boolean>("employed")
3029
.withParameterTypeConverter(booleanToStringConverter)
31-
val occupation = column<String>("occupation", JDBCType.VARCHAR)
32-
val addressId = column<Int>("address_id", JDBCType.INTEGER)
30+
val occupation = column<String>("occupation")
31+
val addressId = column<Int>("address_id")
3332
}
3433
}

0 commit comments

Comments
 (0)