play 2.5 DB 问题 - 使用依赖注入
play 2.5 DB issue - Use dependency injection
问题:
我在 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
一个class并注入到你需要的地方。
class User @Inject() (db: Database) { ..}
在你的控制器中:
class MyController @Inject() (user: User) extends Controller {
// ..
user.getId
// ..
}
在此处阅读更多内容:https://www.playframework.com/documentation/2.5.x/ScalaDatabase
我想你遇到的问题是这样的:
You do not have an implicit Application in scope: PlayFramework with Oracle
尝试导入'play.api.Play.current'的包,即在你的代码文件中添加以下内容
import play.api.Play.current
祝你好运
问题:
我在 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
一个class并注入到你需要的地方。
class User @Inject() (db: Database) { ..}
在你的控制器中:
class MyController @Inject() (user: User) extends Controller {
// ..
user.getId
// ..
}
在此处阅读更多内容:https://www.playframework.com/documentation/2.5.x/ScalaDatabase
我想你遇到的问题是这样的:
You do not have an implicit Application in scope: PlayFramework with Oracle
尝试导入'play.api.Play.current'的包,即在你的代码文件中添加以下内容
import play.api.Play.current
祝你好运