平滑过滤器nscala-time DateTime大于等于或小于等于

时间:2018-12-05 17:22:57

标签: scala slick nscala-time

我正在尝试用nscala-time过滤DateTime Slick

package models.repositories.tables

import java.sql.Timestamp
import com.github.nscala_time.time.Imports._
import models.entities.User
import slick.lifted.ProvenShape
import slick.jdbc.MySQLProfile.api._

class UserTable (tag: Tag)  extends Table[User](tag, "users") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  def createdAt = column[DateTime]("createdAt", O.SqlType("TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"))
  def updatedAt = column[DateTime]("updatedAt", O.SqlType("TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"))
  override def * : ProvenShape[User] = (
    id,
    name,
    createdAt,
    updatedAt
  ) <> (User.tupled, User.unapply)
}

object UserTable extends GenericTableQuery[User, UserTable] {
  val userQuery = tableQuery

  implicit def dateTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
    dateTime => new Timestamp(dateTime.getMillis),
    timeStamp => new DateTime(timeStamp.getTime)
  )

}

但是当我尝试使用

进行过滤时
def getUsers(from: DateTime, end: DateTime, offset: Int) = {
  val usersQuery = UserTable.userQuery.filter(p => p.createdAt >= from && p.createdAt <= end).sortBy(_.createdAt.desc)
  users.drop(offset).take(25)
}

我得到符号>= <=无法解析

1 个答案:

答案 0 :(得分:0)

我需要做的就是在需要使用的地方显式导入implicit def dateTimeMapping

import models.repositories.tables.UserTable.dateTimeMapping