diff --git a/src/main/java/org/mybatis/dynamic/sql/SqlTable.java b/src/main/java/org/mybatis/dynamic/sql/SqlTable.java index fab3d8c2a..066e040be 100644 --- a/src/main/java/org/mybatis/dynamic/sql/SqlTable.java +++ b/src/main/java/org/mybatis/dynamic/sql/SqlTable.java @@ -16,12 +16,15 @@ package org.mybatis.dynamic.sql; import java.sql.JDBCType; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; import java.util.Optional; public class SqlTable implements TableExpression { protected String tableName; + protected final List> columns = new ArrayList<>(); protected SqlTable(String tableName) { this.tableName = Objects.requireNonNull(tableName); @@ -31,21 +34,30 @@ public String tableName() { return tableName; } + public List> columns() { + return columns; + } + public BasicColumn allColumns() { return SqlColumn.of("*", this); //$NON-NLS-1$ } public SqlColumn column(String name) { - return SqlColumn.of(name, this); + SqlColumn sqlColumn = SqlColumn.of(name, this); + columns.add(sqlColumn); + return sqlColumn; } public SqlColumn column(String name, JDBCType jdbcType) { - return SqlColumn.of(name, this, jdbcType); + SqlColumn sqlColumn = SqlColumn.of(name, this, jdbcType); + columns.add(sqlColumn); + return sqlColumn; } public SqlColumn column(String name, JDBCType jdbcType, String typeHandler) { - SqlColumn column = SqlColumn.of(name, this, jdbcType); - return column.withTypeHandler(typeHandler); + SqlColumn column = SqlColumn.of(name, this, jdbcType).withTypeHandler(typeHandler); + columns.add(column); + return column; } @Override diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlTableExtensions.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlTableExtensions.kt index 24962ef01..74947429f 100644 --- a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlTableExtensions.kt +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/elements/SqlTableExtensions.kt @@ -45,5 +45,7 @@ fun SqlTable.column( withParameterTypeConverter(parameterTypeConverter) withJavaType(javaType?.java) withJavaProperty(javaProperty) - build() + val column = build() + columns().add(column) + column } diff --git a/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/KotlinDslMapper.kt b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/KotlinDslMapper.kt new file mode 100644 index 000000000..5b69a227e --- /dev/null +++ b/src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/mybatis3/KotlinDslMapper.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2016-2025 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 org.mybatis.dynamic.sql.util.kotlin.mybatis3 + +import org.apache.ibatis.annotations.SelectProvider +import org.mybatis.dynamic.sql.BasicColumn +import org.mybatis.dynamic.sql.SqlTable +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider +import org.mybatis.dynamic.sql.util.SqlProviderAdapter +import org.mybatis.dynamic.sql.util.kotlin.CountCompleter +import org.mybatis.dynamic.sql.util.kotlin.DeleteCompleter +import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter +import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter +import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper +import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper +import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper +import org.mybatis.dynamic.sql.util.mybatis3.CommonUpdateMapper + +/** + * @author lidiwei + * @create 2025-12-23 14:30 + */ +interface KotlinDslMapper : CommonCountMapper, CommonDeleteMapper, + CommonInsertMapper, + CommonUpdateMapper { + val table: Table + + @SelectProvider(type = SqlProviderAdapter::class, method = "select") + fun doSelectList(selectStatement: SelectStatementProvider): List + + @SelectProvider(type = SqlProviderAdapter::class, method = "select") + fun doSelectOne(selectStatement: SelectStatementProvider): Record? +} + +fun KotlinDslMapper.insert(record: Record): Int = + insert( + mapper = this::insert, + row = record, + table = this.table + ) { + table.columns().forEach { withMappedColumn(it) } + } + +fun
KotlinDslMapper.update(completer: UpdateCompleter): Int = + update(this::update, table, completer) + +fun
KotlinDslMapper.delete(completer: DeleteCompleter): Int = + deleteFrom(this::delete, table, completer) + +fun
KotlinDslMapper.count(completer: CountCompleter): Long = + count(this::count, table.allColumns(), table = this.table, completer = completer) + +fun
KotlinDslMapper.selectOne( + selectList: List = table.columns(), + completer: SelectCompleter +): Record? = selectOne(this::doSelectOne, selectList, table, completer) + +fun
KotlinDslMapper.selectList( + selectList: List = table.columns(), + completer: SelectCompleter +): List = selectList(this::doSelectList, selectList, table, completer) + + +fun
KotlinDslMapper.insertBatch(vararg records: Record): List = + insertBatch(records.toList()) + +fun
KotlinDslMapper.insertBatch(records: Collection): List = + insertBatch(this::insert, records, table) { + table.columns().forEach { withMappedColumn(it) } + } diff --git a/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapper.kt b/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapper.kt index d1b177db7..baccc49a1 100644 --- a/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapper.kt +++ b/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapper.kt @@ -23,6 +23,7 @@ import org.apache.ibatis.annotations.SelectProvider import org.apache.ibatis.type.JdbcType import org.mybatis.dynamic.sql.select.render.SelectStatementProvider import org.mybatis.dynamic.sql.util.SqlProviderAdapter +import org.mybatis.dynamic.sql.util.kotlin.mybatis3.KotlinDslMapper import org.mybatis.dynamic.sql.util.mybatis3.CommonCountMapper import org.mybatis.dynamic.sql.util.mybatis3.CommonDeleteMapper import org.mybatis.dynamic.sql.util.mybatis3.CommonInsertMapper @@ -66,3 +67,9 @@ interface PersonMapper : CommonCountMapper, CommonDeleteMapper, CommonInsertMapp @ResultMap("PersonResult") fun selectOne(selectStatement: SelectStatementProvider): PersonRecord? } + +@Mapper +interface PersonDslMapper: KotlinDslMapper { + override val table: PersonDynamicSqlSupport.Person + get() = PersonDynamicSqlSupport.person +} diff --git a/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt b/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt index 86af992a7..76bb0a7b3 100644 --- a/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt +++ b/src/test/kotlin/examples/kotlin/mybatis3/canonical/PersonMapperTest.kt @@ -42,6 +42,8 @@ import org.mybatis.dynamic.sql.util.kotlin.elements.isIn import org.mybatis.dynamic.sql.util.kotlin.elements.sortColumn import org.mybatis.dynamic.sql.util.kotlin.elements.stringConstant import org.mybatis.dynamic.sql.util.kotlin.elements.sum +import org.mybatis.dynamic.sql.util.kotlin.mybatis3.count +import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insert import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertInto import org.mybatis.dynamic.sql.util.kotlin.mybatis3.insertSelect import org.mybatis.dynamic.sql.util.kotlin.mybatis3.multiSelect @@ -226,6 +228,15 @@ class PersonMapperTest { val rows = mapper.insert(row) assertThat(rows).isEqualTo(1) } + + sqlSessionFactory.openSession().use { session -> + val mapper = session.getMapper(PersonDslMapper::class.java) + + val row = PersonRecord(100, "Joe", LastName("Jones"), Date(), true, "Developer", 1) + + val rows = mapper.insert(row) + assertThat(rows).isEqualTo(1) + } } @Test @@ -500,6 +511,19 @@ class PersonMapperTest { assertThat(rows).isEqualTo(2L) } + + sqlSessionFactory.openSession().use { session -> + val mapper = session.getMapper(PersonDslMapper::class.java) + + val rows = mapper.count { + where { + occupation.isNull() + and { employed.isFalse() } + } + } + + assertThat(rows).isEqualTo(2L) + } } @Test