如何避免Slick的模型声明详细程度和重复性

key*_*rai 8 scala slick playframework-2.1

我目前正在使用Slick 1.x在playframework 2.1.3中访问MySQL.虽然Slick的功能通常看起来很不错,但我无法理解声明语法的重复性.我的意思是看看下面的代码:

case class User
 (id: Option[Long]
 , firstName: String
 , lastName: String
 , email: String
 , password: String
 , status: String
 , createDate: Long = Platform.currentTime
 , firstLogin: Option[Long]
 , lastLogin: Option[Long]
 , passwordChanged: Option[Long]
 , failedAttempts: Int = 0
  )

object User extends Table[User]("USER") {
  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def firstName = column[String]("firstName", O.NotNull)
  def lastName = column[String]("lastName", O.NotNull)
  def email = column[String]("mail", O.NotNull)
  def password = column[String]("password", O.NotNull)
  def status = column[String]("status", O.NotNull)
  def createDate = column[Long]("createDate", O.NotNull)
  def firstLogin = column[Long]("firstLogin", O.Nullable)
  def lastLogin = column[Long]("lastLogin", O.Nullable)
  def passwordChanged = column[Long]("passwordChanged", O.Nullable)
  def failedAttempts = column[Int]("failedAttempts", O.NotNull)

  def * = id.? ~ firstName ~ lastName ~ email ~ password ~ status ~ createDate ~ firstLogin.? ~ lastLogin.? ~ passwordChanged.? ~ failedAttempts <>(User.apply _, User.unapply _)
  def autoInc = * returning id
}
Run Code Online (Sandbox Code Playgroud)

它是不对的,为了拥有一个简单的case类和一个访问对象,我必须将每个字段声明三次.有没有办法避免这种容易出错的重复性?

更新:当然,此问题的解决方案应支持读写操作.

dre*_*xin 5

您可以使用直接嵌入.它是一个基于宏的实验性API,可让您像这样编写表格:

@table("COFFEES") case class Coffee(
  @column("COF_NAME")  name:  String,
  @column("SUP_ID") supID: Int,
  @column("PRICE") price: Double
)
val coffees = Queryable[Coffee]
Run Code Online (Sandbox Code Playgroud)

编辑:

文档在这里:http://slick.typesafe.com/doc/1.0.1/direct-embedding.html


cvo*_*ogt 1

您可以使用代码生成或将来:类型提供程序。

请参阅https://groups.google.com/d/msg/scalaquery/Pdp3GTXsKCo/O0e3JLXAaK8J