gpt4 book ai didi

scala - 如何在 play 2.4 上安装 ReactiveMongo?

转载 作者:行者123 更新时间:2023-12-04 15:42:58 26 4
gpt4 key购买 nike

我已经安装了以下内容:
1. Play 2.4
2.创建了一个scala项目
3.新增eclipse插件

现在我想添加一个数据库连接。我想试用 ReactiveMongo,但 wiki 页面上的说明适用于 2.3 或更早版本。

https://github.com/ReactiveMongo/Play-ReactiveMongo

对于 2.4,似乎 play 的文件结构发生了变化。我需要知道为 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 的新文件结构?

这是我尝试做但没有成功的事情:
在 project/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 :

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

至于响应式(Reactive) 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"))
}
}

关于scala - 如何在 play 2.4 上安装 ReactiveMongo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30538710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com