在此示例中演示了MappedColumnType
的使用:
如何在另一个表类中重用dateColumnType
?
答案 0 :(得分:3)
trait DateColumnMapper extends HasDatabaseConfig[JdbcProfile] {
protected val dbConfig: DatabaseConfig[JdbcProfile]
import driver.api._
implicit val dateColumnType = MappedColumnType.base[Date, Long](
d => d.getTime,
d => new Date(d)
)
}
然后,您可以将此特征包含在您需要的任何DAO
或db组件中:
class WhateverDAO
extends WhateverComponent
with HasDatabaseConfig[JdbcProfile]
with DateColumnMapper {
class Whatevers(tag: Tag) extends Table[Whatever](tag, "WHATEVER") {
def anyDate = column[Option[Date]]("ANYDATE")
...
}
}