如何使用Scala Play2.6从数据库中选择单列 [英] How to select Single column from database using scala Play2.6
本文介绍了如何使用Scala Play2.6从数据库中选择单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张表用户详细信息表,它包含在下面的列中
I have one table User details Table and it contains below Columns
用户名,用户名,电子邮件
User id,username,Email
我要选择用户ID为1的电子邮件 而且我希望响应采用正确的JSON格式
I want to select email whose user id is 1 and i want the response as in proper JSON Format
如何在使用Slick的Scala Play 2.6中做到这一点
到目前为止,我已经完成了
How to do that In Scala Play 2.6 with Slick
Till now I have done this
def getPasswqord(username:String):Future[Seq[(String)]]= {
val a2 = (sql"""select a.userpassword from user_details_table a where a.Email=$username or a.Mobile_no=$username""".as[(String)])
dbConfig.run(a2)
}
由此我得到的响应格式为"["12345"]"
.
from this i am getting response in a format "["12345"]"
.
预期的输出格式为
"[{"Password":"12345"}]"
推荐答案
You should define custom Json Writes to format your query result. For example:
import play.api.libs.json.{JsPath, JsValue, Json, Writes}
// Given passwords
val passwords: Seq[(String)] = Seq(("12345"), ("qwerty"))
val writes : Writes[String] = (JsPath \ "password").write[String]
def toJson[T](sequence: Seq[T])(implicit writes: Writes[T]): JsValue = {
Json.toJson(sequence)
}
toJson(passwords)(writes)
这将输出json对象[{"password":"12345"},{"password":"qwerty"}]
This will output json object [{"password":"12345"},{"password":"qwerty"}]
这篇关于如何使用Scala Play2.6从数据库中选择单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文