0

伙计们,我正在尝试从 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 连接

请帮帮我。非常感谢。

4

1 回答 1

0

您的控制器和模型之间存在命名空间冲突Customers,因为两者都在范围内。你可以做两件事来解决这个问题。

  1. 将您的模型重命名为不同的名称,例如Customer.

  2. 改为区别于。Customers.allCustomers_models.Customers.allCustomerscontrollers.Customers

于 2014-09-09T16:14:22.463 回答