pit*_*408 6 scala playframework playframework-2.0 reactivemongo
我已经安装了以下内容:1.Play 2.4 2.Created一个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
Run Code Online (Sandbox Code Playgroud)
如何将配置应用于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")
Run Code Online (Sandbox Code Playgroud)
我得到一个解决错误消息:
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
Run Code Online (Sandbox Code Playgroud)
所以,在得知我需要将依赖项添加到/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
Run Code Online (Sandbox Code Playgroud)
完成这些步骤后,我想验证是否正确安装.所以我试图从https://github.com/ReactiveMongo/Play-ReactiveMongo将教程代码添加到我的项目中
/app
/controllers/Application.scala
/controllers/UsingJsonReadersWriters.scala
/models/models.scala
/conf
/routes
Run Code Online (Sandbox Code Playgroud)
然后我做一个激活剂清洁然后我做一个激活器运行
运行后我看到一个错误:
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.
Run Code Online (Sandbox Code Playgroud)
所以,我的安装似乎失败了.所以,这个问题仍然存在.
将以下行添加到您的build.sbt:
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24"
Run Code Online (Sandbox Code Playgroud)
例如:
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test,
"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24",
"org.scalatest" % "scalatest_2.11" % "2.2.4" % "test"
)
Run Code Online (Sandbox Code Playgroud)
至于反应式 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"))
}
}
Run Code Online (Sandbox Code Playgroud)