我是playframework 2.0的新用户。我想将用户对象映射到窗体中
我知道在光滑的 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 然后映射到表单?
表单如何绑定 scala2.0 中的数据?
谁能为此提供任何例子?
通常的做法是使用 case 类来表示要插入到数据库中的对象。然后,您可以轻松为案例类创建表单映射。
一个例子是泰国人。
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 然后映射到表单"是什么意思,但希望这就是您正在寻找的答案。