Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.mybatis.dynamic.sql.util.kotlin.MyBatisDslMarker
import org.mybatis.dynamic.sql.util.kotlin.SelectCompleter
import org.mybatis.dynamic.sql.util.kotlin.UpdateCompleter
import org.springframework.dao.EmptyResultDataAccessException
import org.springframework.jdbc.core.RowMapper
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
Expand Down Expand Up @@ -193,6 +194,11 @@ fun NamedParameterJdbcTemplate.selectDistinct(
fun <T> NamedParameterJdbcTemplate.selectList(
selectStatement: SelectStatementProvider,
rowMapper: (rs: ResultSet, rowNum: Int) -> T
): List<T> = selectList(selectStatement, RowMapper(rowMapper))

fun <T> NamedParameterJdbcTemplate.selectList(
selectStatement: SelectStatementProvider,
rowMapper: RowMapper<T>
): List<T> =
query(selectStatement.selectStatement, selectStatement.parameters, rowMapper)

Expand All @@ -217,10 +223,15 @@ fun NamedParameterJdbcTemplate.selectOne(
this
)

@SuppressWarnings("SwallowedException")
fun <T> NamedParameterJdbcTemplate.selectOne(
selectStatement: SelectStatementProvider,
rowMapper: (rs: ResultSet, rowNum: Int) -> T
): T? = selectOne(selectStatement, RowMapper(rowMapper))

@SuppressWarnings("SwallowedException")
fun <T> NamedParameterJdbcTemplate.selectOne(
selectStatement: SelectStatementProvider,
rowMapper: RowMapper<T>
): T? = try {
queryForObject(selectStatement.selectStatement, selectStatement.parameters, rowMapper)
} catch (e: EmptyResultDataAccessException) {
Expand Down Expand Up @@ -251,6 +262,9 @@ class SelectListMapperGatherer(
) {
fun <T> withRowMapper(rowMapper: (rs: ResultSet, rowNum: Int) -> T): List<T> =
template.selectList(selectStatement, rowMapper)

fun <T> withRowMapper(rowMapper: RowMapper<T>): List<T> =
template.selectList(selectStatement, rowMapper)
}

@MyBatisDslMarker
Expand All @@ -260,6 +274,9 @@ class SelectOneMapperGatherer(
) {
fun <T> withRowMapper(rowMapper: (rs: ResultSet, rowNum: Int) -> T): T? =
template.selectOne(selectStatement, rowMapper)

fun <T> withRowMapper(rowMapper: RowMapper<T>): T? =
template.selectOne(selectStatement, rowMapper)
}

@MyBatisDslMarker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,12 +621,10 @@ open class CanonicalSpringKotlinTemplateDirectTest {

@Test
fun testAutoMapping() {
val rm = DataClassRowMapper.newInstance(AddressRecord::class.java)

val rows = template.select(address.id.`as`("id"), address.streetAddress, address.city, address.state) {
from(address)
orderBy(address.id)
}.withRowMapper(rm::mapRow)
}.withRowMapper(DataClassRowMapper(AddressRecord::class.java))

assertThat(rows).hasSize(2)
with(rows[0]) {
Expand All @@ -637,6 +635,23 @@ open class CanonicalSpringKotlinTemplateDirectTest {
}
}

@Test
fun testAutoMappingOneRow() {
val row = template.selectOne(address.id.`as`("id"), address.streetAddress, address.city, address.state) {
from(address)
where { address.id isEqualTo 1 }
orderBy(address.id)
}.withRowMapper(DataClassRowMapper(AddressRecord::class.java))

assertThat(row).isNotNull
with(row!!) {
assertThat(id).isEqualTo(1)
assertThat(streetAddress).isEqualTo("123 Main Street")
assertThat(city).isEqualTo("Bedrock")
assertThat(state).isEqualTo("IN")
}
}

@Test
fun testSelectWithJoin() {
val rows = template.select(
Expand Down