scala – 如何在Play 2.4上安装ReactiveMongo?
我安装了以下内容:
1.播放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的新文件结构? 这是我试图做的没有成功: 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 完成这些步骤后,我想验证我是否正确安装.所以我试图将教程代码添加到我的项目中 /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:
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24" 例如: libraryDependencies ++= Seq( jdbc,specs2 % Test,"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24","org.scalatest" % "scalatest_2.11" % "2.2.4" % "test" ) 至于反应性mongo的例子,我从来没有让它们起作用.我想他们可能有点过时了.尝试(不是最好的例子,但很简单): 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")) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |