Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Kotlin公开的DSL查询映射 如何将select查询映射到类_Sql_Kotlin_Kotlin Exposed - Fatal编程技术网

Sql Kotlin公开的DSL查询映射 如何将select查询映射到类

Sql Kotlin公开的DSL查询映射 如何将select查询映射到类,sql,kotlin,kotlin-exposed,Sql,Kotlin,Kotlin Exposed,是否有任何@transactional操作来执行查询 object UserRepository { fun getAll() : List<User> { return User.selectAll().map { User } // how to add it in a transaction ? // Is it the right way to map query to a Class ? } fun get(id: Int) : User? { return

是否有任何@transactional操作来执行查询

object UserRepository {
fun getAll() : List<User> {
   return User.selectAll().map { User } // how to add it in a transaction ? // Is it the right way to map query to a Class ?
}

fun get(id: Int) : User? {
    return User.select { User.id eq id id}.map { User.it } // Mapping Not working
}

映射应该自动完成。请参阅文档:

表:

object Users : IntIdTable() {
  val name = varchar("name", 50)
}
实体:

class User(id: EntityID<Int>) : IntEntity(id) {
  companion object : IntEntityClass<User>(Users)

  var name     by Users.name
}
用法:

fun getAllUsers(): List<User> {
  Database.connect(/* ... */)
  return transaction {
    User.all().toList()
  }
}

我知道已经晚了,但对于其他有这个问题的人来说: 如果您使用的是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) }
    }

我建议你从读那篇博客开始,这对我不起作用。它说类型推断失败了。预期类型不匹配:必需:找到列表:ListNevermind。我做的是用户。全部不是用户。全部