如何在 Table 类 中重用 MappedColumnType?

How to reuse MappedColumnType in Table classes?

MappedColumnType的用法在这个例子中演示:

https://github.com/playframework/play-slick/blob/master/samples/computer-database/app/dao/ComputersDAO.scala#L21

如何在另一个 table class 中重复使用 dateColumnType

例如,您可以将其移动到这样的特征:

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 或数据库组件中包含这个特征:

class WhateverDAO
  extends WhateverComponent
  with HasDatabaseConfig[JdbcProfile]
  with DateColumnMapper {

  class Whatevers(tag: Tag) extends Table[Whatever](tag, "WHATEVER") {
    def anyDate = column[Option[Date]]("ANYDATE")
    ...
  }
}