我是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查詢和映射? –