0

我是 playframework 2.0 的新用户。我想将用户对象映射到表单

我了解在 slick 1.0 中有:

val userForm = Form(
 mapping(
"name" -> text,
"age" -> number
)(UserData.apply)(UserData.unapply)
)

但在 slick 2.0 中,用户是对象:

class User(tag: Tag) extends Table[(Int, String,String, String,Date,String,Option[Long], Int)](tag, "User") {
def id = column[Int]("SUP_ID", O.PrimaryKey, O.AutoInc)
def first_name = column[String]("First_Name")
def second_name=column[String]("Second_Name")
def email=column[String]("Email")
def datebirth=column[Date]("Birth_date")
def password=column[String]("Password")
def addID = column[Option[Long]]("ADRESS", O.Nullable)
def privilege=column[Int]("privilege")
def * = (id, first_name, second_name, email, datebirth, password, addID, privilege)
def home_address=foreignKey("ha_FK", addID, address)(_.id)
}
val user=TableQuery[User]

对象如何更改为 seq 然后映射到 Form?

Form如何绑定scala2.0中的数据?

谁能为此提供任何示例?

4

1 回答 1

0

它通常采用的方式是使用案例类来表示要插入到数据库中的对象。然后,您可以轻松地为您的案例类创建表单映射。

一个例子就是泰国。

case class User(id: Option[Int] = None, name: String, age: Int)

class UserTable(tag: Tag) extends Table[User]("user") {
    def id = column[Int]("SUP_ID", O.PrimaryKey, O.AutoInc)
    def name = column[String]("Name")
    def age = column[Int]("Age")
}

val users = TableQuery[UserTable]

val userForm = Form(
    mapping(
        "id" -> ignored
        "name" -> text,
        "age" -> number
    )
)

我不确定您所说的“更改为 Seq 然后映射到 Form”是什么意思,但希望这是您正在寻找的答案。

于 2014-04-23T03:41:23.373 回答