如何使用 Anorm 将行映射到类?

发布于 2024-11-13 09:17:30 字数 1022 浏览 1 评论 0 原文

我有一个类 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 发生:ColumnNotFound(User.id) on:

val users: List[User] = SQL("SELECT * FROM users").as(User*)

有什么建议吗?我是否应该在前面的行中包含 User 对象?我仍然有我的案例类用户

I have a class User:

case class User (id: Int, name: String)

And I would like to map the rows from a query using Anorm Stream API. I have tried with this code:

val selectUsers = SQL("SELECT id, name FROM users")
val users = selectUsers().map(
    user => User(0, user.name)
).toList

But I get an error:

Error raised is : value name is not a member of play.db.anorm.SqlRow

on

user => User(0, user.↓name)

How can I map the SqlRow to a class?


As suggested by Ricardo, I tried:

object User extends Magic[User]

val users: List[User] = SQL("SELECT * FROM users").as(User*)

But with this code I get an RuntimeException occured : ColumnNotFound(User.id) on:

val users: List[User] = SQL("SELECT * FROM users").as(User*)

Any suggestions? Am I supposted to have the User object in the line right before? and I still have my case class User.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北笙凉宸 2024-11-20 09:17:30

您可以使用 Magic 助手,创建一个扩展 magic 的对象:

object User extends Magic[User]

然后:

val users:List[User] = SQL("select * from User").as(User*)

请参阅文档以获取更多信息: 魔法帮手

You can use Magic helper, create a object that extends magic :

object User extends Magic[User]

Then :

val users:List[User] = SQL("select * from User").as(User*)

See the doc for more information : Magic helper

檐上三寸雪 2024-11-20 09:17:30

我得到了它的工作原理:

val selectUsers = SQL("SELECT id, name FROM users")
val users = selectUsers().map(
    user => new User(user[Int]("id"), user[String]("name"))
).toList

每一行 user 都是一本字典。我不太了解 Scala 语法。

I got it working with this:

val selectUsers = SQL("SELECT id, name FROM users")
val users = selectUsers().map(
    user => new User(user[Int]("id"), user[String]("name"))
).toList

Every row user is a dictionary. I don't know the Scala syntax very well.

毁梦 2024-11-20 09:17:30

为了使其更具可扩展性,您可以这样做。

创建一个 val 并将传入数据映射到用户。

import {classname}

val parsedValueOfUser = {
 get[Int]("id") ~
 get[String]("name") map {
   case id ~ name => User(id, name)
 }
}

现在,当您想从数据库获取用户并将其映射到您的 User 类时,您可以执行以下操作:

val selectUsers = SQL("SELECT id, name FROM users").as(parsedValueOfUser *)

To make it a bit more scalable you could do this.

Create a val and map the incoming data to a user.

import {classname}

val parsedValueOfUser = {
 get[Int]("id") ~
 get[String]("name") map {
   case id ~ name => User(id, name)
 }
}

Now when you want to get a user from the database and map it to your User class you can do:

val selectUsers = SQL("SELECT id, name FROM users").as(parsedValueOfUser *)
鹤仙姿 2024-11-20 09:17:30

我遇到了这个确切的问题,并花了我一段时间才找出问题所在。原来是模型和数据库名称必须相同,区分大小写。

因此,对于您的示例,您的数据库表需要称为“用户”

您可以使用以下命令更改数据库表名称:
将表用户重命名为用户;希望

有帮助。

I ran into this exact issue and took me awhile to finger out what was wrong. It turned out to be the model and database names have to be the same, case-sensitive.

So for your example your database table would need to be called "User"

You can change a database table name using:
RENAME TABLE users TO User ;

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文