0

我有一个从 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

4

1 回答 1

2

只需切换firstOptionlist

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)
  }

}
于 2013-11-10T20:07:35.793 回答