我是Play 2.1和Scala的新手。我来自node.js background &它直接以JSON形式从数据库返回结果。我想要的是从Play(Scala)中以JSON形式从数据库获取数据。我已经尝试过Json.toJson,但它显示了反序列化器或其他东西的错误。任何人都可以通过模型&控制器描述找到我解决这个问题的方法。提前致谢。从PLAY(Scala)中以JSON的形式从数据库获取数据
我正在使用Mysql数据库。 这里是型号代码...
import anorm.SqlParser._
import play.api.db.DB
//class definition
case class Data(Date_Time_id:BigInteger,Details:String,Image:Strig,Status:Boolean,Type:String)
object Model{
def getDetails(Person_id:Long):Map[BigInteger,Data]={
DB.withConnection{ implicit c=>
val result=SQL("""select Date_Time_id,Details,Image,
,Status,Type from table1 where Person_id={perId}
""").on("perId"->Person_id)
//mapping result
val detailss=result().map(row=>
row[BigInteger]("Date_Time_id")->row[BigInteger]("Date_Time_id"),row[String]("Details"),row[String]("Image"),row[Boolean]("Status"),row[String]("Type"))).toMap
return detailss
}
}
我从控制器喜欢叫它:
var getResult=Model.getDetails(some Id)
退房http://www.playframework.com/documentation/2.1.1/ ScalaJson – immutable
你在使用什么样的数据库?你打电话给数据库怎么样?你可以在你的问题中包含该代码吗? – EECOLOR
向我们展示一些代码...您如何从数据库获取数据?一些ORM或原始的SQL查询和映射? –