我有一堂课User
:
case class User (id: Int, name: String)
我想使用Anorm Stream API映射查询中的行。我试过这段代码:
val selectUsers = SQL("SELECT id, name FROM users")
val users = selectUsers().map(
user => User(0, user.name)
).toList
但我收到一个错误:
Error raised is : value name is not a member of play.db.anorm.SqlRow
上
user => User(0, user.↓name)
如何将其映射SqlRow
到一个类?
正如里卡多所建议的,我尝试了:
object User extends Magic[User]
val users: List[User] = SQL("SELECT * FROM users").as(User*)
但是有了这段代码,我得到了一个RuntimeException occured : ColumnNotFound(User.id)
:
val users: List[User] = SQL("SELECT * FROM users").as(User*)
有什么建议么?我是否应该在之前将User
对象放在行中?我还有我的case class User
.