slick尝试基于jodaTime DateTime进行过滤

时间:2016-05-02 16:56:52

标签: scala datetime playframework slick

我在Play框架中遇到此错误

  

无法找到参数tt的隐含值:   slick.ast.TypedType [org.joda.time.DateTime]

如果我没有隐含以下内容,那就太好了 我在声明后使用以下命令进行隐式转换:def datain = columnDateTime(jdateColumnType)

implicit def jdateColumnType  =
      MappedColumnType.base[DateTime, Timestamp](
        dt => new Timestamp(dt.getMillis),
        ts => new DateTime(ts.getTime)
    )

但这并没有解决发生错误的搜索问题:说DateTime没有< =

def getLast24HByAddress(address:String) : Future[List[Email]] = {
    val now = new java.sql.Timestamp(new java.util.Date().getTime())
    db.run(
      Emails.filter(_.datain <= DateTime.now.minusDays(1))
    )
  }

当我以隐含的方式执行此操作时:

  

value&lt; =不是slick.lifted.Rep [org.joda.time.DateTime]的成员

2 个答案:

答案 0 :(得分:1)

我通常在定义表的类中执行此操作:

/**
 * Mapping for using Joda Time.
 */
implicit def dateTimeMapping = MappedColumnType.base[DateTime, java.sql.Timestamp](
   dt => new Timestamp(dt.getMillis),
   ts => new DateTime(ts.getTime, DateTimeZone.UTC))

然后在实例化并导入类时:

 val schema = DBSchemaV2(driver)
 import schema._
 import schema.driver.api._ 

这种映射将会出现。这样你就可以在光滑的情况下使用ExtensionMethod。

答案 1 :(得分:0)

  implicit val getEmailResult = GetResult(r => Email(Some(r.nextLong), r.nextString, r.nextString,
  r.nextString, new DateTime(r.nextTimestamp.getTime)))

  def last24Hours(address: String): Future[Vector[Email]] = {
    val r = sql"""SELECT id, email, uuid, address, datain FROM email 
      WHERE DATE_ADD(datain, INTERVAL 1 DAY) >= NOW() AND address = $address """
      .as[Email]
    db.run(r)
  }