Play! 2 Framework - Add Java Mongo driver

2020-07-26 11:50发布

问题:

I am trying to use the java mongo driver with Play!.

So I read though:

http://www.playframework.org/documentation/2.0.2/SBTDependencies

http://www.mongodb.org/display/DOCS/Java+Tutorial

I added the buildpath in eclipse. But play can't find the dependency.

So there is an example on the playframework homepage but I still can't figure it out.

val appDependencies = Seq(

      "org.scala-tools" %% "scala-stm" % "0.3",
      "org.apache.derby" % "derby" % "10.4.1.3" % "test"

    ) 

Why are der two% signs? How do I get the correct names? I only know that the version should be 2.8.0. So if I want to add the Java driver, what do I have to do?

And if i use the official java driver, I don't have to use Morphia right?

Installing play framework mongodb morphia module

.

.

Edit:

import sbt._
import Keys._
import PlayProject._

object ApplicationBuild extends Build {

    val appName         = "website"
    val appVersion      = "1.0-SNAPSHOT"

    val appDependencies = Seq(
        "org.mongodb" % "mongo-java-driver" % "2.8.0"
        //"com.mongodb" % "Mongo" %"2.8.0"

    )

    val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
      // Add your own project settings here      
    )

}

回答1:

To add the MongoDB drivers to your Play project, edit your project/Build.scala file to add the MongoDB dependency:

val appDependencies = Seq(

   // ... here are your previous dependencies (derby etc...)
   "org.mongodb" % "mongo-java-driver" % "2.8.0"

) 

After you add this, you need to restart your Play server for the changes to take affect.



回答2:

I use reactive-mongo for my project. You can find some information here http://reactivemongo.org/releases/0.10.5/documentation/tutorial/setup.html



回答3:

In case the accepted answer doesn't work for some of you , you can try adding this version of mongodb in the build.sbt

"org.mongodb.morphia" % "morphia" % "0.107"