我在Play Framework 2.5中有以下案例类和类。由于DB对象已被弃用,我正在尝试使用新的依赖注入方法,但不理解为什么我的代码不能编译。旧方法工作正常,新方法有编译错误。
隐式连接上缺少参数类型=>
package models
import javax.inject.{Inject, Singleton}
import play.db._
import anorm.SqlParser._
import anorm._
import play.api.db.DB
import play.api.Play.current
case class Department(id: Int,
name: String)
@Singleton
class DepartmentDao @Inject() (dBApi: DBApi) {
private val db = dBApi.getDatabase("default")
val simple = {
get[Int]("department.id") ~
get[String]("department.name") map {
case id ~ name => Department(id, name)
}
}
def getDepartments = DB.withConnection { implicit connection =>
SQL("SELECT * FROM DEPARTMENT")
}
def getDepartmentsNew = db.withConnection { implicit connection =>
SQL("SELECT * FROM DEPARMTMENT")
}
}
更新:
我之前已经放置了代码看起来像我最初试图让它工作的东西。除非我遗漏了这些代码应该运行的东西。它与我在多个示例项目中看到的代码相同。
@Singleton
class DepartmentDao @Inject() (dBApi: DBApi) {
private val db = dBApi.getDatabase("default")
val simple = {
get[Int]("department.id") ~
get[String]("department.name") map {
case id ~ name => Department(id, name)
}
}
def getDepartments: Seq[Department] = db.withConnection { implicit connection =>
SQL("SELECT * FROM DEPARMTMENT").as(simple.*)
}
}