我尝试了解一些Slick的作品以及它需要什么.
这是一个例子:
package models
case class Bar(id: Option[Int] = None, name: String)
object Bars extends Table[Bar]("bar") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
// This is the primary key column
def name = column[String]("name")
// Every table needs a * projection with the same type as the table's type parameter
def * = id.? ~ name <>(Bar, Bar.unapply _)
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下*这里方法的目的是什么<>,为什么unapply?什么是Projection - 方法~'返回实例Projection2?
我有嵌套的类/对象,并希望使用SLICK在数据库中存储(和检索)它们.据我所知,使用SLICK映射投影将是关键.此外,我使用伴侣对象在嵌套对象和平面结构之间进行映射(存储在DB表中).我想做这样的事情(简化示例):
case class Foo(id: Int, myBar: Bar)
case class Bar(myInt: Int, myString: String)
object Foo {
def apply(id: Int, myInt: Int, myString: String): Foo = Foo(id, Bar(myInt, myString))
override def unapply(f: Foo) = (f.id, f.myBar.myInt, f.myBar.myString)
}
object TTable extends Table[Foo]("FOO") {
def id = column[Int]("id", O.PrimaryKey)
def myInt = column[Int]("myInt", O NotNull)
def myString = column[String]("myString", O NotNull)
def * = id ~ myInt ~ myString <> (Foo.apply _, Foo.unapply _)
def query(db: Database, id: Int): Option[Foo] = db …Run Code Online (Sandbox Code Playgroud)