伙计我试图从mysql数据库中获取一些结果,并且我在将其提取到scala.html文件时出错。这是我的代码:
/*Customers.scala. Its controller*/
package controllers
import play.api._
import play.api.mvc._
import models.Customers
object Customers extends Controller{
def customer = Action{
val nb_customers = Customers.allCustomers
Ok(views.html.customer(nb_customers)) //I am having error here.
}
// End of customer Action.
}
// End of Customer controller.
/*Now Customers.scala model*/
package models
import anorm._
import play.api.db._
import play.api.Play.current
case class Customers(CustomersID: Int, Name: String)
object Customers {
def allCustomers = {
DB.withConnection {implicit connection =>
SQL("Select * from Customers")().map{row =>
Customers(
CustomersID = row[Int]("CustomersID"),
Name = row[String]("Name")
)
// End of Customers object.
}.toList
// SQL ends.
}
// With connection.
}
// End of allCustomers.
}
// End of of Customers.
请注意,我在conf / application.conf文件中使用JDBC驱动程序进行mysql连接
请帮帮我。非常感谢。
答案 0 :(得分:0)
您的Customers
控制器和模型之间存在命名空间冲突,因为两者都在范围内。你可以做两件事来解决这个问题。
将您的模型重命名为其他内容,例如Customer
。
将Customers.allCustomers
更改为models.Customers.allCustomers
,以区别于controllers.Customers
。