问题:
我在 play 2.5 编译时遇到以下问题。
You do not have an implicit Application in scope. If you want to bring the current running Application into context, please use dependency injection.
Getting issue in line - DB.withConnection {^
代码片段:
object User {
def getId(emailid: String): Option[Int] = {
DB.withConnection { implicit c =>
SQL("select id from user where email = {email}").on(
'email -> emailid).as(SqlParser.scalar[Int].singleOpt)
}
}
}
我该如何解决这个问题?
最佳答案
让 User
成为一个类并在你需要的地方注入(inject)它。
class User @Inject()(db:数据库){ ..}
在你的 Controller 中:
类 MyController @Inject()(用户:用户)扩展 Controller {
//..
用户.getId
//..
}
在这里阅读更多:https://www.playframework.com/documentation/2.5.x/ScalaDatabase
关于mysql - play 2.5 DB 问题 - 使用依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37103172/