Skip to content

Commit a749956

Browse files
committed
Add KotlinDslMapper
1 parent 07ae910 commit a749956

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

src/main/java/org/mybatis/dynamic/sql/SqlTable.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.sql.JDBCType;
19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.Objects;
2022
import java.util.Optional;
2123

2224
public class SqlTable implements TableExpression {
2325

2426
protected String tableName;
27+
protected final List<SqlColumn<?>> columns = new ArrayList<>();
2528

2629
protected SqlTable(String tableName) {
2730
this.tableName = Objects.requireNonNull(tableName);
@@ -31,21 +34,30 @@ public String tableName() {
3134
return tableName;
3235
}
3336

37+
public List<SqlColumn<?>> columns() {
38+
return columns;
39+
}
40+
3441
public BasicColumn allColumns() {
3542
return SqlColumn.of("*", this); //$NON-NLS-1$
3643
}
3744

3845
public <T> SqlColumn<T> column(String name) {
39-
return SqlColumn.of(name, this);
46+
SqlColumn<T> sqlColumn = SqlColumn.of(name, this);
47+
columns.add(sqlColumn);
48+
return sqlColumn;
4049
}
4150

4251
public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
43-
return SqlColumn.of(name, this, jdbcType);
52+
SqlColumn<T> sqlColumn = SqlColumn.of(name, this, jdbcType);
53+
columns.add(sqlColumn);
54+
return sqlColumn;
4455
}
4556

4657
public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandler) {
47-
SqlColumn<T> column = SqlColumn.of(name, this, jdbcType);
48-
return column.withTypeHandler(typeHandler);
58+
SqlColumn<T> column = SqlColumn.of(name, this, jdbcType).withTypeHandler(typeHandler);
59+
columns.add(column);
60+
return column;
4961
}
5062

5163
@Override

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlTableExtensions.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,7 @@ fun <T : Any> SqlTable.column(
4545
withParameterTypeConverter(parameterTypeConverter)
4646
withJavaType(javaType?.java)
4747
withJavaProperty(javaProperty)
48-
build()
48+
val column = build()
49+
columns().add(column)
50+
column
4951
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2016-2025 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+
* https://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+
17+
package org.mybatis.dynamic.sql.util.kotlin.mybatis3
18+
19+
import org.apache.ibatis.annotations.SelectProvider
20+
import org.mybatis.dynamic.sql.BasicColumn
21+
import org.mybatis.dynamic.sql.SqlTable
22+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
23+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter
24+
import org.mybatis.dynamic.sql.util.kotlin.CountCompleter
25+
import org.mybatis.dynamic.sql.util.kotlin.DeleteCompleter
26+
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
27+
import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter
28+
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper
29+
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper
30+
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper
31+
import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper
32+
33+
/**
34+
* @author lidiwei
35+
* @create 2025-12-23 14:30
36+
*/
37+
interface KotlinDslMapper<out Table : SqlTable, Record : Any> : CommonCountMapper, CommonDeleteMapper,
38+
CommonInsertMapper<Record>,
39+
CommonUpdateMapper {
40+
val table: Table
41+
42+
@SelectProvider(type = SqlProviderAdapter::class, method = "select")
43+
fun doSelectList(selectStatement: SelectStatementProvider): List<Record>
44+
45+
@SelectProvider(type = SqlProviderAdapter::class, method = "select")
46+
fun doSelectOne(selectStatement: SelectStatementProvider): Record?
47+
}
48+
49+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insert(record: Record): Int =
50+
insert(
51+
mapper = this::insert,
52+
row = record,
53+
table = this.table
54+
) {
55+
table.columns().forEach { withMappedColumn(it) }
56+
}
57+
58+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.update(completer: UpdateCompleter): Int =
59+
update(this::update, table, completer)
60+
61+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.delete(completer: DeleteCompleter): Int =
62+
deleteFrom(this::delete, table, completer)
63+
64+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.count(completer: CountCompleter): Long =
65+
count(this::count, table.allColumns(), table = this.table, completer = completer)
66+
67+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.selectOne(
68+
selectList: List<BasicColumn> = table.columns(),
69+
completer: SelectCompleter
70+
): Record? = selectOne(this::doSelectOne, selectList, table, completer)
71+
72+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.selectList(
73+
selectList: List<BasicColumn> = table.columns(),
74+
completer: SelectCompleter
75+
): List<Record> = selectList(this::doSelectList, selectList, table, completer)
76+
77+
78+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insertBatch(vararg records: Record): List<Int> =
79+
insertBatch(records.toList())
80+
81+
fun <Table : SqlTable, Record : Any> KotlinDslMapper<Table, Record>.insertBatch(records: Collection<Record>): List<Int> =
82+
insertBatch(this::insert, records, table) {
83+
table.columns().forEach { withMappedColumn(it) }
84+
}

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapper.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.apache.ibatis.annotations.SelectProvider
2323
import org.apache.ibatis.type.JdbcType
2424
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider
2525
import org.mybatis.dynamic.sql.util.SqlProviderAdapter
26+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.KotlinDslMapper
2627
import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper
2728
import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper
2829
import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper
@@ -66,3 +67,9 @@ interface PersonMapper : CommonCountMapper, CommonDeleteMapper, CommonInsertMapp
6667
@ResultMap("PersonResult")
6768
fun selectOne(selectStatement: SelectStatementProvider): PersonRecord?
6869
}
70+
71+
@Mapper
72+
interface PersonDslMapper: KotlinDslMapper<PersonDynamicSqlSupport.Person, PersonRecord> {
73+
override val table: PersonDynamicSqlSupport.Person
74+
get() = PersonDynamicSqlSupport.person
75+
}

src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import org.mybatis.dynamic.sql.util.kotlin.elements.isIn
4242
import org.mybatis.dynamic.sql.util.kotlin.elements.sortColumn
4343
import org.mybatis.dynamic.sql.util.kotlin.elements.stringConstant
4444
import org.mybatis.dynamic.sql.util.kotlin.elements.sum
45+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.count
46+
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insert
4547
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertInto
4648
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertSelect
4749
import org.mybatis.dynamic.sql.util.kotlin.mybatis3.multiSelect
@@ -226,6 +228,15 @@ class PersonMapperTest {
226228
val rows = mapper.insert(row)
227229
assertThat(rows).isEqualTo(1)
228230
}
231+
232+
sqlSessionFactory.openSession().use { session ->
233+
val mapper = session.getMapper(PersonDslMapper::class.java)
234+
235+
val row = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1)
236+
237+
val rows = mapper.insert(row)
238+
assertThat(rows).isEqualTo(1)
239+
}
229240
}
230241

231242
@Test
@@ -500,6 +511,19 @@ class PersonMapperTest {
500511

501512
assertThat(rows).isEqualTo(2L)
502513
}
514+
515+
sqlSessionFactory.openSession().use { session ->
516+
val mapper = session.getMapper(PersonDslMapper::class.java)
517+
518+
val rows = mapper.count {
519+
where {
520+
occupation.isNull()
521+
and { employed.isFalse() }
522+
}
523+
}
524+
525+
assertThat(rows).isEqualTo(2L)
526+
}
503527
}
504528

505529
@Test

0 commit comments

Comments
 (0)