播放框架+ SLICK(Scalaquery)教程 [英] Play framework + SLICK (Scalaquery) tutorial

查看:84
本文介绍了播放框架+ SLICK(Scalaquery)教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我收到此错误:

[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


This is my 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      
    )

}

推荐答案

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

Edit Now 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.

回想一下,Slick现在能够访问RDBMS,并且还将很快瞄准MongoDB.它使用光滑的(^^)内部DSL查询后端.该DSL由Macros管理,这就是为什么需要Scala 2.10的原因.

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.

但是,请注意宏系统处于实验状态(即使Scala2.10将被释放).我还不知道在不久的将来,这种状态可能会在Slick lib上引起警告.

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.

要享受此RC,请去 Play2.1RC1 ,然后浏览文档...那里有很多更改,例如Json API fi

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.

嗯.不确定滑动功能是否可以像Play一样容易使用.

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

因为PLay 2.0实际上是建立在Scala 2.9.x之上的,因此,Slick需要2.10(对于Macro).

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

因此,起初,您要声明的Deps(slick_2.10.0-M4表示我正在使用Scala 2.10.0-M4)与将要使用的scala版本之间不匹配.

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

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

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.

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

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

我的2c

这篇关于播放框架+ SLICK(Scalaquery)教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆