2015-05-29 13 views
6

Tôi đã cài đặt như sau: 1.Play 2.4 2.Created một dự án scala 3.added nhật thực cắmLàm thế nào để cài đặt ReactiveMongo khi chơi 2.4?

Bây giờ tôi muốn thêm một kết nối cơ sở dữ liệu. Tôi muốn dùng thử ReactiveMongo, nhưng các hướng dẫn trên trang wiki là dành cho 2.3 trở lên.

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

Đối với 2,4 có vẻ như cấu trúc tệp của trò chơi đã thay đổi. Tôi cần phải biết cách thích hợp để cấu hình chơi 2.4 cho ReactiveMongo.

Dưới đây là các hướng dẫn họ cung cấp cho các phiên bản chơi mới hơn 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 

Làm thế nào tôi sẽ áp dụng có cấu hình để cấu trúc file mới chơi 2.4?

Đây là những gì tôi đã cố gắng để làm với không thành công: Trong dự án/plugins.sbt Tôi nói thêm:

resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/" 


addSbtPlugin("org.reactivemongo" % "play2-reactivemongo" % "0.11.0-SNAPSHOT") 

tôi nhận được một thông báo lỗi giải quyết:

 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 

Vì vậy, sau khi học rằng tôi cần thêm phụ thuộc vào tệp /build.sbt và thực hiện các thay đổi ở đó.

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 

Sau khi thực hiện các bước này, tôi muốn xác minh xem tôi đã cài đặt đúng chưa. Vì vậy, tôi đã cố gắng để thêm mã hướng dẫn để dự án của tôi từ https://github.com/ReactiveMongo/Play-ReactiveMongo

/app 
    /controllers/Application.scala 
    /controllers/UsingJsonReadersWriters.scala 
    /models/models.scala 
/conf 
    /routes 

Sau đó, tôi làm một activator sạch Sau đó, tôi làm một chạy activator

tôi thấy một lỗi sau khi chạy:

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. 

Vì vậy, có vẻ như cài đặt của tôi không thành công. Vì vậy, câu hỏi này vẫn còn mở.

+0

Bạn có thể xem [tài liệu] (http://reactivemongo.org/releases/0.11.0/documentation/tutorial/play2.html) được cập nhật vì plugin đã có sẵn dưới dạng 0.11.0. play24-M2 (cột mốc sắp được phát hành là 0,11). – cchantep

Trả lời

1

Thêm dòng sau vào build.sbt của bạn:

"org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24" 

ví dụ:

libraryDependencies ++= Seq(
    jdbc, 
    cache, 
    ws, 
    specs2 % Test, 
    "org.reactivemongo" %% "play2-reactivemongo" % "0.11.0.play24", 
    "org.scalatest" % "scalatest_2.11" % "2.2.4" % "test" 
) 

Đối với ví dụ trên Mongo phản ứng, tôi không bao giờ họ làm việc. Tôi nghĩ họ có thể hơi lạc hậu. Hãy thử (Không phải là ví dụ tốt nhất nhưng đơn giản):

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

Nếu ai vẫn đang tìm kiếm câu trả lời: Trong Chơi 2.4.x phụ thuộc để được thêm vào build.sbt của bạn trông như thế này ...

Đối ReactiveMongo 0.11.7:

libraryDependencies ++= Seq(
    // ... 
    "org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24" 
) 

Đối ReactiveMongo 0.11.9 (phát hành vào ngày 20 tháng mười hai năm 2015):

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

Lưu ý rằng việc đặt tên dường như thay đổi bất cứ lúc nào. Đối với các bản phát hành sau, bạn có thể thử các bước sau:

  • Chuyển tới Kho lưu trữ trung tâm Maven và tìm kiếm g:"org.reactivemongo" play.
  • Tìm một ArtifactId kết thúc gần bằng phiên bản Scala của bạn, ví dụ: play2-reactivemongo_2.11 nếu bạn có Scala 2.11.x.
  • Nhấp vào số phiên bản (ví dụ: 0.11.9) trong dòng đó, sẽ đưa bạn đến một trang như this.
  • Trong phần Thông tin phụ thuộc, hãy nhấp vào "Scala SBT".
  • Sao chép dòng vào tệp build.sbt của bạn. Nếu bạn có phần libraryDependencies ++= Seq(...), chỉ sao chép phần sau +=.
Các vấn đề liên quan