找不到参数连接的隐式值:java.sql.connection

找不到参数连接的隐式值:java.sql.connection,sql,scala,playframework,playframework-2.0,anorm,Sql,Scala,Playframework,Playframework 2.0,Anorm,我从Scala开始。我在模型中有我的方法: def get_courses = { DB.withConnection { implicit connection => val result = SQL("SELECT * FROM courses") result } 当我从控制器调用它时,我正在这样做(我想得到一个列表): 当我试图转换列表(从)中的流[行]时,我遇到了错误 could not find implicit value fo

我从Scala开始。我在模型中有我的方法:

def get_courses = {

    DB.withConnection { implicit connection =>

      val result = SQL("SELECT * FROM courses")
      result

    }
当我从控制器调用它时,我正在这样做(我想得到一个列表):

当我试图转换列表(从)中的流[行]时,我遇到了错误

could not find implicit value for parameter connection: java.sql.Connection
与AllCourses()代码相关

有什么想法吗

然而,这很奇怪,因为当我添加所有相同的方法时

def get_courses = DB.withConnection { implicit connection =>
   val result = SQL("SELECT * FROM courses")

   // Transform the resulting Stream[Row] as a List[(String)]
   val CoursesList = result().map(row =>
       row[String]("name")
   ).toList

} 
它是有效的(例如)


但是我想让它们在控制器中分开…

我想你的问题的答案是,当你简单地定义sql语句时,实际的数据库连接不会被使用。如果将鼠标悬停在SQL调用上,您将看到它不需要DB连接。然而,result()或SQL(“…”)确实需要连接,这就是对数据库的实际调用发生的地方

我可以谦虚地建议您编写控制器只是为了对类对象进行操作,而不是为了与anorm/数据库级人工制品进行交互。因此,例如,您的模型层将有一个方法
get_courses:List[Course]
(或者可能只是
all
,因为您可能会将其引用为Course companion对象的一个方法,即
Course.all()
,因此可能不需要在方法名称中使用Course)来返回课程对象。您的控制器会根据需要调用它,而不必担心课程对象是来自数据库、json文件还是来自任何地方


我还发现解析器非常有用,因此定义一个解析器
courseP
,例如,它创建课程对象,然后在需要阅读一门或多门课程的任何地方使用,比如
SQL(“从课程中选择…””).as(courseP*)
,用于课程列表,或者
SQL(“选择…”)as(courseP.singleOpt)
获取一个选项[课程]。

解决您的问题可能会重复任何喜悦?感谢wwkudu的帮助。正如您所说,在控制器中添加一个连接就解决了这个问题。然而,我听从了你们的建议,我把所有的数据库工作放在模型上,以保持控制器没有db连接。抱歉迟到了。
def get_courses = DB.withConnection { implicit connection =>
   val result = SQL("SELECT * FROM courses")

   // Transform the resulting Stream[Row] as a List[(String)]
   val CoursesList = result().map(row =>
       row[String]("name")
   ).toList

}