有人知道使用Play框架和SLICK(ScalaQuery)的好教程或示例项目(github)吗?我正努力让它们一起工作。
我收到此错误:
[info] play - Application started (Dev)
[error] application -
! @6b13oi41c - Internal server error, for request [GET /listBooks] ->
play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[NoClassDefFoundError: Could not initialize class scala.slick.ast.opt.Relational$]]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1-2.0.2.jar:2.0.2]
at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1-2.0.2.jar:2.0.2]
at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor-2.0.2.jar:2.0.2]
at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1-2.0.2.jar:2.0.2]
at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor-2.0.2.jar:2.0.2]
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor-2.0.2.jar:2.0.2]
Caused by: java.lang.NoClassDefFoundError: Could not initialize class scala.slick.ast.opt.Relational$
at scala.slick.driver.BasicProfile$class.processAST(BasicProfile.scala:18) ~[slick_2.10.0-M4-0.10.0-M2.jar:0.10.0-M2]
at scala.slick.driver.PostgresDriver$.processAST(PostgresDriver.scala:69) ~[slick_2.10.0-M4-0.10.0-M2.jar:0.10.0-M2]
at scala.slick.driver.BasicProfile$class.createQueryBuilder(BasicProfile.scala:22) ~[slick_2.10.0-M4-0.10.0-M2.jar:0.10.0-M2]
at scala.slick.driver.PostgresDriver$.createQueryBuilder(PostgresDriver.scala:69) ~[slick_2.10.0-M4-0.10.0-M2.jar:0.10.0-M2]
at scala.slick.driver.BasicProfile$class.buildSelectStatement(BasicProfile.scala:23) ~[slick_2.10.0-M4-0.10.0-M2.jar:0.10.0-M2]
at scala.slick.driver.PostgresDriver$.buildSelectStatement(PostgresDriver.scala:69) ~[slick_2.10.0-M4-0.10.0-M2.jar:0.10.0-M2]
[error] application -
这是我的图书模型:
package models
import play.api.db._
import play.api.Play.current
import scala.slick.driver.PostgresDriver.simple._
import scala.slick.ql.{MappedTypeMapper}
import scala.slick.session.{Session, Database}
case class Book(name: String, filename: String)
object Book extends Table[(Long, String, String)]("book") {
lazy val database = Database.forDataSource(DB.getDataSource())
def id = column[Long]("id", O PrimaryKey, O AutoInc)
def name = column[String]("name", O NotNull)
def filename = column[String]("filename", O NotNull)
def * = id ~ name ~ filename
def findAll() : Seq[Book] = database.withSession { implicit db:Session =>
(for(t <- this) yield t.name ~ t.filename).list.map(attrs => Book(attrs._1, attrs._2))
}
def create(book: Book): Unit = database.withSession { implicit db:Session =>
this.name ~ this.filename insert(book.name, book.filename)
}
}
编辑:
这是我的Build.scala
import sbt._
import Keys._
import PlayProject._
object ApplicationBuild extends Build {
val appName = "PlayWithScala"
val appVersion = "1.0-SNAPSHOT"
val appDependencies = Seq(
// Add your project dependencies here,
"postgresql" % "postgresql" % "9.1-902.jdbc4",
"com.typesafe" % "slick_2.10.0-M4" % "0.10.0-M2"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
// Add your own project settings here
)
}
答案 0 :(得分:12)
答案 1 :(得分:7)
修改强> 现在Play2.1已进入RC过程,我们可以使用Slick。那是因为Play2.1在海底使用Scala2.10(RC也是如此),因为Slick将是Typesafe堆栈中的默认DB访问lib。
回想一下,Slick现在能够访问RDBMS,并且很快就会针对MongoDB。它使用一个灵活的(^^)内部DSL来查询后端。这个DSL由Macros管理,这就是为什么需要Scala 2.10。
但请注意,宏系统处于实验状态(即使将释放Scala2.10)。我不知道在不久的将来Slick lib上这种状态的潜在警告。
要享受这个RC,去那里Play2.1RC1,然后浏览文档...那里有很多变化,比如Json API f.i。
Hmmmm。不太确定光滑是否可以开箱即用Play。
因为PLay 2.0实际上是基于Scala 2.9.x构建的,其中光滑需要2.10(对于宏)。
所以,首先你声明的deps(slick_2.10.0-M4
说我正在使用Scala 2.10.0-M4)和将要使用的scala版本之间存在不匹配。
可以尝试的是为整个项目定义另一个版本的scala ......
我的2c
答案 2 :(得分:1)
使用Play 2.1,使用最新的Slick版本(1.0.1-RC1),您可以使用:
val appDependencies = Seq(
"com.typesafe.slick" %% "slick" % "1.0.1-RC1",
...
}