如何在Play 2.4上安装ReactiveMongo?
我已经安装了以下内容:1.Play 2.4 2.创建一个scala项目3.添加eclipse插件
现在我想添加一个数据库连接。 我想试用ReactiveMongo,但维基页面上的说明适用于2.3或更高版本。
https://github.com/ReactiveMongo/Play-ReactiveMongo
对于2.4,似乎游戏的文件结构已经改变。 我需要知道为ReactiveMongo配置play 2.4的正确方法。
以下是他们为比2.4版更新的播放版本提供的说明:
If you want to use the latest snapshot, add the following instead (only for play > 2.3):
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0-SNAPSHOT"
)
Configure your application to use ReactiveMongo plugin
add to your conf/play.plugins
1100:play.modules.reactivemongo.ReactiveMongoPlugin
Configure your database access within application.conf
如何将配置应用于Play 2.4的新文件结构?
这是我试图做的没有成功:在项目/ plugins.sbt我补充说:
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
addSbtPlugin("org.reactivemongo" % "play2-reactivemongo" % "0.11.0-SNAPSHOT")
我收到一条解决错误消息:
at java.lang.Thread.run(Thread.java:745)
[error] (*:update) sbt.ResolveException: unresolved dependency: org.reactivemong
o#play2-reactivemongo;0.11.0-SNAPSHOT: not found
所以,在得知我需要将依赖项添加到/build.sbt文件并在那里进行更改后。
name := """oneid-scala"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
//This is for reactivemongodb
resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
//This is for reactivemongodb
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0-SNAPSHOT"
)
// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
EclipseKeys.createSrc := EclipseCreateSrc.All
在完成这些步骤之后,我想验证我是否正确安装了该设备。 所以我试图从https://github.com/ReactiveMongo/Play-ReactiveMongo将教程代码添加到我的项目中
/app
/controllers/Application.scala
/controllers/UsingJsonReadersWriters.scala
/models/models.scala
/conf
/routes
然后我做一个激活清洁然后我做一个激活运行
运行后我看到一个错误:
missing or invalid dependency detected while loading class file 'JSONGenericHandlers.class'.
Could not access type GenericHandlers in package reactivemongo.api.collections, because it (or its dependencies) are missing.
Check your build definition for missing or conflicting dependencies.
(Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'JSONGenericHandlers.class' was compiled against an incompatible version of reactivemongo.api.collections.
所以,我的安装似乎失败了。 所以,这个问题仍然是开放的。
build.sbt
添加到build.sbt
:
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24"
例如:
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test,
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24",
"org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
)
至于反应性芒果的例子,我从来没有得到它们的工作。 我认为他们可能有点过时了。 尝试(不是最好的例子,但简单):
import reactivemongo.api.MongoDriver
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.bson.BSONDocument
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
class RMongoTest(){
def run = {
val dbHosts: List[String] = List("localhost")
val dbName = "TestDB"
val driver = new MongoDriver
val connection = driver.connection(dbHosts)
val db = connection("TestDB")
val collection :BSONCollection = db("TestCollection")
val futureInsert = collection.insert(BSONDocument("Moo" -> "Over"))
Await.result(futureInsert, 10.seconds) //Not really a great pattern...
val futureResult = collection.find(BSONDocument()).one
val result = Await.result(futureResult, 10.seconds)
println(result.get.get("Moo"))
}
}
如果有人仍在寻找答案:在Play 2.4.x中,要添加到build.sbt
的依赖关系看起来像这样...
对于ReactiveMongo 0.11.7 :
libraryDependencies ++= Seq(
// ...
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24"
)
对于ReactiveMongo 0.11.9 (于2015年12月20日发布):
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
// ...
"org.reactivemongo" % "play2-reactivemongo_2.11" % "0.11.9"
// Or to let SBT add the Scala version suffix automatically:
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.9"
)
请注意,命名似乎会不时变化。 对于更高版本,您可以尝试以下步骤:
g:"org.reactivemongo" play
。 2.11.x
,则play2-reactivemongo_2.11
。 0.11.9
),即可进入此页面。 build.sbt
文件中。 如果你有一个libraryDependencies ++= Seq(...)
节,只复制+=
后的部分。 上一篇: How to install ReactiveMongo on play 2.4?
下一篇: why slick2.0 can not find the TableQuery class in playframework 2.2.1?