[英]NamedParameterJdbcTemplate return Optional<Integer>
Is there any way to return Optional from NamedParameterJdbcTemplate?有没有办法从 NamedParameterJdbcTemplate 返回 Optional?
My query some times can return empty.我的查询有时会返回空。 So I want beautiful way for check for emptyness.
所以我想要检查空虚的美丽方式。 I dont want to do try catch for EmptyResultDataAccessException coz this little ugly.
我不想尝试捕获 EmptyResultDataAccessException 因为这有点难看。
From what I understand your issue appears to be that your ResultSet
may sometimes be entirely empty, so it's not about the value being returned as null
but rather not having any result in the first place, hence why you mention EmptyResultDataAccessException
.据我了解,您的问题似乎是您的
ResultSet
有时可能完全为空,所以这不是关于返回为null
的值,而是首先没有任何结果,因此您为什么提到EmptyResultDataAccessException
。 In my answer I am going to assume you're interested only in the first result as otherwise it wouldn't really make sense.在我的回答中,我将假设您只对第一个结果感兴趣,否则它真的没有意义。
There is no direct method available in NamedParameterJdbcTemplate
but you can use lambdas to get you there. NamedParameterJdbcTemplate
没有可用的直接方法,但您可以使用 lambdas 来实现。 Specifically, you can get a list of all results and then use stream().findAny()
to obtain an Optional
.具体来说,您可以获取所有结果的列表,然后使用
stream().findAny()
获取Optional
。
More or less, it would go like this:或多或少,它会是这样的:
Optional<Integer> values = namedParameterJdbcTemplate.queryForList(...)
.stream()
.map(m -> (Integer)m.get("MY_COLUMN"))
.findAny();
In doing the above, you may want to limit the number of results your query is going to return as you may otherwise putting excessive strain on your network and also on the server's memory.在执行上述操作时,您可能希望限制查询将返回的结果数量,否则可能会给网络和服务器内存带来过多压力。 Depending on the database technology, there are SQL clauses like
LIMIT
and OFFSET
(see https://www.postgresql.org/docs/8.1/queries-limit.html for Postgres orhttps://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html for MySQL) to help you with the task.根据数据库技术,有像
LIMIT
和OFFSET
这样的 SQL 子句(Postgres 见https://www.postgresql.org/docs/8.1/queries-limit.html或https://dev.mysql.com/doc/ refman/5.5/en/limit-optimization.html for MySQL)来帮助您完成任务。
It is however to be noted that if your query returns more than one row in the first place, you're effectively discarding some data and therefore it's probably safer to assume that you're doing it wrong in the first place (in other words: you must be sure your query will always return either zero or one result).然而需要注意的是,如果您的查询首先返回多行,您实际上是在丢弃一些数据,因此假设您首先做错了可能更安全(换句话说:您必须确保您的查询将始终返回零或一个结果)。
Assuming instead you would always have exactly one row and one column but with the returned value being null, the easiest way to obtain an Optional
would be to wrap the call in Optional.ofNullable
.假设您总是只有一行和一列,但返回值为空,获取
Optional
的最简单方法是将调用包装在Optional.ofNullable
。 Example (pseudo-code, just to get the gist):示例(伪代码,只是为了获取要点):
Optional<Integer> v = Optional<Integer>.ofNullable(namedParameterJdbcTemplate.queryForObject(...));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.