【发布时间】:2013-05-03 21:33:54
【问题描述】:
我想根据 Id 从用户那里查询一行。我有以下虚拟代码
case class User(
id: Option[Int],
name: String
}
object Users extends Table[User]("user") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = id ~ name <>(User, User.unapply _)
def findById(userId: Int)(implicit session: Session): Option[User] = {
val user = this.map { e => e }.where(u => u.id === userId).take(1)
val usrList = user.list
if (usrList.isEmpty) None
else Some(usrList(0))
}
}
在我看来,findById 是查询单个列的过度杀伤力,因为 Id 是标准主键。有谁知道更好的方法?请注意,我正在使用 Play! 2.1.0
【问题讨论】:
标签: scala slick scalaquery