我有一个从db表中检索单行的简单方法:
object Data {
implicit val getListStringResult = GetResult[List[Object]] (
r => (1 to r.numColumns).map(_ => r.nextObject).toList
)
def getUser(id: Int): Option[Map[String, Object]] = DB.withSession {
val columns = MTable.getTables(None, None, None, None).list.filter(_.name.name == "user").head.getColumns.list.map(_.column)
sql"""SELECT * FROM "user" WHERE "id" = $id""".as[List[Object]].firstOption.map(columns zip _ toMap)
}
}
如何修改此方法以将多行检索到列表中?结果类型应类似于List[Map[String, Object]]
我在Play 2.2.1,Slick 1.0.1,Scala 2.10.3,Java 1.8 64bit
答案 0 :(得分:2)
只需将firstOption
切换为list
:
object Data {
implicit val getListStringResult = GetResult[List[Object]] (
r => (1 to r.numColumns).map(_ => r.nextObject).toList
)
def getUser(id: Int): List[Map[String, Object]] = DB.withSession {
val columns = MTable.getTables(None, None, None, None).list.filter(_.name.name == "user").head.getColumns.list.map(_.column)
sql"""SELECT * FROM "user" WHERE "id" = $id""".as[List[Object]].list.map(columns zip _ toMap)
}
}