【发布时间】:2016-10-13 15:24:03
【问题描述】:
我目前有这个代码:
class UsersRepository @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile] {
import driver.api._
private val Users = TableQuery[UsersTable]
def findByName(name: String): Future[Option[User]] =
db.run(Users.filter(_.name === name).take(1).result.headOption)
implicit val localDateTimeColumnType = MappedColumnType.base[LocalDateTime, Timestamp](
d => Timestamp.from(d.toInstant(ZoneOffset.ofHours(0))),
d => d.toLocalDateTime
)
trait GenericTable {
this: Table[_] =>
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def createdAt = column[LocalDateTime]("created_at")
def updatedAt = column[LocalDateTime]("updated_at")
}
private class UsersTable(tag: Tag) extends Table[User](tag, "User") with GenericTable {
def name = column[String]("name")
def url = column[String]("url")
def nameIndex = index("name_index", name, unique = true)
override def * = (id, createdAt, updatedAt, name, url) <> (User.tupled, User.unapply)
}
}
我现在如何轻松地将 GenericTable 特征移出 UsersRepository,以便将其与其他表一起使用?如果我只是将其移出,则不再找到 column、Table 和 O 之类的东西,因为我从驱动程序导入中丢失了这些东西。
我还想将 UsersTable 定义本身移出 DAO/repository 类。在这种情况下,我遇到了同样的问题。
谢谢, 马格努斯
【问题讨论】:
标签: scala playframework slick