首页 > 解决方案 > Kotlin 暴露了 DSL 查询映射

问题描述

标签: sqlkotlinkotlin-exposed

解决方案


我知道已经晚了,但对于其他有这个问题的人来说:如果您使用的是 DSL,您可以在数据类中创建一个函数以用于映射目的。例如 :

data class User(
    val id: Int,
    val username: String,
    val password: String
) {

    companion object {

        fun fromRow(resultRow: ResultRow) = User(
            id = resultRow[UserTable.id].value,
            username= resultRow[UserTable.username],
            password = resultRow[UserTable.password]
        )
    }
}

在您的交易块中:

transaction {
        user = UserTable.select { UserTable.id eq userId }.map { User.fromRow(it) }
    }

推荐阅读