scala Play 框架 + SLICK (Scalaquery) 教程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11379608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:20:19  来源:igfitidea点击:

Play framework + SLICK (Scalaquery) tutorial

scalaplayframework-2.0scalaquery

提问by Salil

Does anybody know of a good tutorial or a sample project (github) of using Play framework with SLICK (ScalaQuery)? I am struggling to make them work together.

有人知道使用带有 SLICK (ScalaQuery) 的 Play 框架的好的教程或示例项目 (github) 吗?我正在努力让他们一起工作。

I am getting this error:

我收到此错误:

[info] play - Application started (Dev)
[error] application - 

! @6b13oi41c - Internal server error, for request [GET /listBooks] ->

play.core.ActionInvoker$$anonfun$receive$$anon: Execution exception [[NoClassDefFoundError: Could not initialize class scala.slick.ast.opt.Relational$]]
    at play.core.ActionInvoker$$anonfun$receive.apply(Invoker.scala:134) [play_2.9.1-2.0.2.jar:2.0.2]
    at play.core.ActionInvoker$$anonfun$receive.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 - 

Here is my Book model:

这是我的书模型:

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)
  }

}

EDIT:
This is my Build.scala

编辑:
这是我的 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      
    )

}

回答by andy petrella

EditNow that Play2.1 has entered its RC process, we can use Slick. That's because Play2.1 is using Scala2.10 (RC as well) under the sea and because Slick will be the default DB access lib in the Typesafe stack.

编辑现在 Play2.1 已经进入了它的 RC 进程,我们可以使用 Slick。这是因为 Play2.1 在海底使用 Scala2.10(也有 RC),并且因为 Slick 将是 Typesafe 堆栈中的默认 DB 访问库。

To recall, Slick is now able to access RDBMS, and will target soon MongoDB as well. It's using a slick (^^) internal DSL for querying backends. This DSL is managed by Macros, that's why Scala 2.10 is required.

回想一下,Slick 现在可以访问 RDBMS,并且很快也将面向 MongoDB。它使用光滑的 (^^) 内部 DSL 来查询后端。该 DSL 由宏管理,这就是为什么需要 Scala 2.10。

However, note that the macro system is in experimental status (even when Scala2.10 will be released). I don't know yet the potential caveats of such status on the Slick lib in the near future.

但是请注意,宏系统处于实验状态(即使 Scala2.10 将发布)。我还不知道在不久的将来 Slick lib 上这种状态的潜在警告。

To enjoy this RC, go there Play2.1RC1, and browse the doc... there are a lot of changes out there, like the Json API f.i.

要享受这个 RC,去那里Play2.1RC1,并浏览文档......那里有很多变化,比如 Json API fi



Hmmmm. Not sure that slick will work out of the box with Play as easy.

嗯嗯。不确定在 Play 中是否可以立即使用。

Because PLay 2.0 is actually build upon Scala 2.9.x, where slick is requiring 2.10 (for Macro).

因为 PLAy 2.0 实际上是建立在 Scala 2.9.x 之上的,其中 slick 需要 2.10(对于宏)。

So, at first there is a mismatch between the deps you're declaring (slick_2.10.0-M4is saying I'm using Scala 2.10.0-M4) and the scala version that'll be used.

因此,首先您声明的 deps(slick_2.10.0-M4即我使用的是 Scala 2.10.0-M4)与将使用的 Scala 版本之间存在不匹配。

BTW, according to this example site(for Slick) your SBT deps seems ok. But the problem might come that the driver will required other deps (AST probably) and leave SBT discover the right version using the current scala version you're using (this is done by declaring dependency without scala version in the "articfact name") => This case, the AST won't be found because it doesn't exists for pre-2.10.

顺便说一句,根据这个示例站点(对于 Slick),您的 SBT 部门似乎没问题。但是问题可能会出现,驱动程序将需要其他 deps(可能是 AST)并让 SBT 使用您正在使用的当前 scala 版本发现正确的版本(这是通过在“工件名称”中声明没有 scala 版本的依赖项来完成的)= > 在这种情况下,将找不到 AST,因为它在 2.10 之前不存在。

What could be tried is to define another version of scala for the whole project...

可以尝试的是为整个项目定义另一个版本的 scala ......

My 2c

我的 2c

回答by Brent Faust

Using Play 2.1, with the latest Slick release (1.0.1-RC1), you'd use:

使用 Play 2.1 和最新的 Slick 版本 (1.0.1-RC1),您可以使用:

val appDependencies = Seq(
  "com.typesafe.slick" %% "slick" % "1.0.1-RC1",
  ...
}