嘗試通過用戶名獲取用戶時出現映射例外。// 不知道為什么 Stack Overflow 需要這么多非代碼描述..................................... ..................................................... .
Exception in thread "main" java.lang.IllegalArgumentException: Mapping Kotlin type User didn't find any columns matching required, non-default constructor parameters in result set
at org.jdbi.v3.core.kotlin.KotlinMapper.specialize$lambda-3(KotlinMapper.kt:72)
at java.base/java.util.Optional.orElseThrow(Optional.java:403)
at org.jdbi.v3.core.kotlin.KotlinMapper.specialize(KotlinMapper.kt:71)
at org.jdbi.v3.core.result.ResultSetResultIterator.<init>(ResultSetResultIterator.java:38)
at org.jdbi.v3.core.result.ResultIterable.lambda$of$0(ResultIterable.java:57)
at org.jdbi.v3.core.result.ResultIterable.one(ResultIterable.java:145)
at JdbiUserRepository.getById$lambda-1(Main.kt:75)
at org.jdbi.v3.core.Jdbi.withHandle(Jdbi.java:357)
at JdbiUserRepository.getById(Main.kt:70)
at MainKt.main(Main.kt:22)
at MainKt.main(Main.kt)
用戶等級:
data class User(val username: String, val password: String)
方法:
class JdbiUserRepository(private val jdbi: Jdbi) : UserRepository {
override fun getById(username: String): User? = jdbi
.withHandle<User?, Exception> { handle -> handle
.createQuery("select from users where username = :username")
.bind("username", username)
.mapTo<User>()
.one()
}
}
搖籃:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.7.10"
application
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
implementation("org.jetbrains.kotlin:kotlin-reflect:1.7.10")
implementation("org.jdbi:jdbi3-core:3.32.0")
implementation("com.h2database:h2:1.4.200")
implementation("org.jdbi:jdbi3-kotlin:3.32.0")
implementation("org.jdbi:jdbi3-kotlin-sqlobject:3.32.0")
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
application {
mainClass.set("MainKt")
}
用戶表(sql腳本):
create table users (
username varchar primary key,
password varchar
)
uj5u.com熱心網友回復:
我相信您的查詢不正確:
.createQuery("select from users where username = :username")
應該
.createQuery("select username, password from users where username = :username")
檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/503838.html
