加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 08:52:38 所属栏目:安全 来源:网络整理
导读:我目前正在使用Slick 1.x在playframework 2.1.3中访问 MySQL.虽然Slick的功能通常看起来很不错,但我无法理解声明语法的重复性.我的意思是看看下面的代码: case class User (id: Option[Long],firstName: String,lastName: String,email: String,password: S
我目前正在使用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
}

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

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

解决方法

您可以使用代码生成或将来使用:输入提供程序.

见https://groups.google.com/d/msg/scalaquery/Pdp3GTXsKCo/O0e3JLXAaK8J

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读