如何理解play2异常的结果解析器?

发布于 2025-01-02 04:49:38 字数 1075 浏览 5 评论 0原文

这是来自 play2 内置 zentasks 的示例代码:

object Project {

  // -- Parsers

  /**
   * Parse a Project from a ResultSet
   */
  val simple = {
    get[Pk[Long]]("project.id") ~
    get[String]("project.folder") ~
    get[String]("project.name") map {
      case id~folder~name => Project(id, folder, name)
    }
  }
}

请注意字段键:project.idproject.folderproject.name

project 部分是什么意思?它从哪里来?

看一下查询方法:

  def findById(id: Long): Option[Project] = {
    DB.withConnection { implicit connection =>
      SQL("select * from project where id = {id}").on(
        'id -> id
      ).as(Project.simple.singleOpt)
    }
  }

SQL是select * from project ...,结果应该是:

    id    |    folder    |   name

不是:

    project.id    |    project.folder    |    project.name

为什么要把key指定为project.??? ,但不是直接字段名称

play2 如何使用 project. 部分?

This is a sample code from play2's built-in's zentasks:

object Project {

  // -- Parsers

  /**
   * Parse a Project from a ResultSet
   */
  val simple = {
    get[Pk[Long]]("project.id") ~
    get[String]("project.folder") ~
    get[String]("project.name") map {
      case id~folder~name => Project(id, folder, name)
    }
  }
}

Please notice the field keys: project.id, project.folder, project.name.

What does the project part mean? Where is it come from?

Look at the query method:

  def findById(id: Long): Option[Project] = {
    DB.withConnection { implicit connection =>
      SQL("select * from project where id = {id}").on(
        'id -> id
      ).as(Project.simple.singleOpt)
    }
  }

The SQL is select * from project ..., the result should be:

    id    |    folder    |   name

Not:

    project.id    |    project.folder    |    project.name

Why we should specify the keys as project.???, but not field name directly?

How play2 use the project. part?

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

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

发布评论

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

评论(2

我做我的改变 2025-01-09 04:49:38

根据 这里,Anorm Parser 使用提供的字符串来检索给定的列。

当它获取一些元数据时,它需要该字段的完整范围(表+名称)才能工作。

As per the code available in here, Anorm Parser uses the string provided to retrieve the given column.

As it gets some metadata, it requires teh full scope of the field (table + name) to work.

无法回应 2025-01-09 04:49:38

恕我直言,这只是数据库别名。

select id, folder, name from project

等于

select project.id, project.folder, project.name from project

或什至

select p.id, p.folder, p.name from project p

IMHO it's just DB alias.

select id, folder, name from project

is the same as

select project.id, project.folder, project.name from project

or even

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