【发布时间】:2016-05-25 18:05:15
【问题描述】:
我使用 Scala 连接到 MongoDB:
val driver = new MongoDriver
val connection = driver.connection(List("myhost"))
val db = connection.database("mydb")
这很好,但是如何将它与 Play 控制器集成:
@Singleton
class ReactiveController @Inject() (implicit system: ActorSystem, materializer: Materializer, val reactiveMongoApi: ReactiveMongoApi)
extends Controller with MongoController with ReactiveMongoComponents {
我需要在我的数据库配置中注入自定义 ReactiveMongoApi 吗?
或者我需要用我的数据库设置修改 application.conf 吗?
我正在使用 play 2.5,http://reactivemongo.org/releases/0.11/documentation/tutorial/play2.html 提供了此代码:
package api
import reactivemongo.api.{ DB, MongoConnection, MongoDriver }
trait ReactiveMongoApi {
def driver: MongoDriver
def connection: MongoConnection
def db: DB
}
但我不确定如何将它与我的 Play 应用程序集成?
我想我不知道使用 Play! 配置数据库源的一些标准方法!申请?
【问题讨论】:
-
如果您阅读了问题中链接的文档,您可以看到“配置您的数据库访问”部分,这表明您需要在
application.conf中添加mongodb.uri设置跨度> -
并添加 play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"
-
如果您想要一个带有最新 MongoDB 驱动程序的 Play 2.5 工作示例作为参考,请查看lightbend.com/activator/template/play-reactive-mongo-db
标签: mongodb scala playframework reactivemongo