我目前正在使用 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
}
这是不对的,为了拥有一个简单的案例类和一个访问对象,我必须将每个字段声明三次。有没有办法避免这种容易出错的重复性?
更新:当然,这个问题的解决方案应该支持读写操作。