我想在我的代码中添加一个案例类SupplierID。我想通过自动映射SupplierID和Int Types来简化*
方法。
case class SupplierID(id: Int)
case class Supplier(id: SupplierID, name: String, street: String, city: String, state: String, zip: String)
// Definition of the SUPPLIERS table using case class
class Suppliers(tag: Tag) extends Table[Supplier](tag, "SUPPLIERS") {
def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
def name = column[String]("SUP_NAME")
def street = column[String]("STREET")
def city = column[String]("CITY")
def state = column[String]("STATE")
def zip = column[String]("ZIP")
def * ={
(id, name, street, city, state, zip).shaped.<>({ tuple => Supplier.apply(SupplierID(tuple._1), tuple._2, tuple._3, tuple._4, tuple._5, tuple._6 )}, {
(s : Supplier) => Some{(s.id.id, s.name, s.street, s.city, s.state, s.zip)}
})
}
}
我正在尝试实现类似
的列类型 // And a ColumnType that maps it to Int
implicit val SupplierIDColumnType = MappedColumnType.base[SupplierID, Int](
s => s.id, // map Bool to Int
i => SupplierID(i) // map Int to Bool
)
如何使用这种mappingtype?
答案 0 :(得分:1)
解决方案:
case class SupplierID(id: Int)
// And a ColumnType that maps it to Int
implicit val SupplierIDColumnType = MappedColumnType.base[SupplierID, Int](
s => s.id, // map SupplierID to Int
i => SupplierID(i) // map Int to SupplierID
)
case class Supplier(id: SupplierID, name: String, street: String, city: String, state: String, zip: String)
// Definition of the SUPPLIERS table using case class
class Suppliers(tag: Tag) extends Table[Supplier](tag, "SUPPLIERS") {
def id = column[SupplierID]("SUP_ID", O.PrimaryKey) // This is the primary key column
def name = column[String]("SUP_NAME")
def street = column[String]("STREET")
def city = column[String]("CITY")
def state = column[String]("STATE")
def zip = column[String]("ZIP")
def * = (id, name, street, city, state, zip) <>(Supplier.tupled , Supplier.unapply _)
}