[英]How to retrieve an Option[List[X]] instead of List[X] from a select statement in Play2!Scala Anorm?
In my Play2 app, I am trying to retrieve a list of users from one of my database table. 在我的Play2应用程序中,我试图从我的一个数据库表中检索用户列表。 The query responsible for this may potentially be empty if there is no row in the database matching the criteria (which is the firstName in our case). 如果数据库中没有符合条件的行(在我们的例子中是firstName),则负责此操作的查询可能可能为空。 That is why I have managed to implement it like this : 这就是为什么我设法像这样实现它:
DB.withConnection { implicit connection =>
SQL("""select u.* from users u
where u.firstName like '%{firstName}%'
""").on("firstName" -> firstName).as(userParser *)
}
this query returns a List[User] but how can I return an Option[List[User]] since this query may not retrieve data corresponding to the provided param (firstName) ? 此查询返回List [User],但如何返回Option [List [User]],因为此查询可能无法检索与提供的param(firstName)对应的数据?
any help would be appreciated thanks... 任何帮助将不胜感激...
You don't need to. 你不需要。 If no user is found. 如果找不到用户。 The list will simply be empty. 该列表将是空的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.