[英]Play 2 Anorm and scala pattern matching
我在使用anorm从play2模块查询mysql数据库时尝试使用模式匹配。 代码如下:
def test= Action {
DB.withConnection { implicit c =>
val entities = SQL("SELECT entity.idEntity, entity.name FROM entity")().collect {
case Row(id:Int, name:String) => Entity(id, name)
}
printList(entities.toList)
}
但是name:String不匹配任何内容(已经尝试匹配整数并且可以正常工作)。 在我的数据库中,实体表的“名称”列类型为varchar(45)。
我有什么想念的吗?
您可以尝试将Int
上的Row
与命名的通配符匹配。
scala> new Row(0,"foo")
res0: Row = Row(0,foo)
scala> res0 match{
| case Row(i: Int,s @ _) => println(i + " " + s)
| }
0 foo
如果名称可为空,则此匹配应起作用:
case Row(id:Int, Some(name:String))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.