简体   繁体   English

NamedParameterJdbcTemplate 返回可选<Integer>

[英]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.根据数据库技术,有像LIMITOFFSET这样的 SQL 子句(Postgres 见https://www.postgresql.org/docs/8.1/queries-limit.htmlhttps://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.

相关问题 选修的<integer>空时从 Optional.ifPresentOrElse() 返回一个字符串?</integer> - Optional<Integer> return a string from Optional.ifPresentOrElse() when empty? NamedParameterJdbcTemplate查询方法返回错误的整数值 - NamedParameterJdbcTemplate query method returning wrong integer value 使用聚合函数时,NamedParameterJdbcTemplate 查询不返回任何内容 - NamedParameterJdbcTemplate query return nothing when using aggregate functions namedParameterJdbcTemplate 更新返回 1 但数据未添加到数据库 POSTGRES SPRING-JDBC - namedParameterJdbcTemplate update return 1 but data is not added to database POSTGRES SPRING-JDBC OptionalInt vs Optional <Integer> - OptionalInt vs Optional<Integer> 将Optional <Integer>转换为Optional <Long>的好方法 - Good way to convert Optional<Integer> to Optional<Long> 在 H2 上使用 NamedParameterJDBCTemplate 上的整数时的未知数据类型 [SPRING-BOOT] - Unknown data type when using an integer over NamedParameterJDBCTemplate on H2 [SPRING-BOOT] 如何转换可选<list<integer> &gt; 到可选<arraylist<integer> &gt; </arraylist<integer></list<integer> - How to convert Optional<List<Integer>> to Optional<ArrayList<Integer>> Optional.map中的Integer :: toString - Integer::toString in Optional.map 如何转换可选<Integer>到 OptionalInt - How to convert Optional<Integer> to OptionalInt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM