我的问题与这里提出的问题类似: http ://forum.springsource.org/showthread.php?84508-jdbctemplate.query()-sorted-result-set 但没有提供明确的答案 - 一个 ArrayList
不保证订单。
基本上,我想知道 jdbcTemplate.query() 返回的调用是否保证结果集的顺序,以及是否可以将其转储到 LinkedList 并将其传递:)
谢谢!
编辑:我应该澄清一下,查询确实包含 order by
子句,因此我需要保证顺序的结果集。我对 ArrayList 不这样做的看法是错误的。由于 jdbcTemplate 是一个接口,因此实现将取决于数据库库。我应该假设将使用 ArrayList 还是再次对其进行排序以确保安全?
My question is similar to the one asked here: http://forum.springsource.org/showthread.php?84508-jdbctemplate.query()-sorted-result-set but a clear answer was not provided - an ArrayList
does not guarantee order.
Basically, I would like to know if the call returned by jdbcTemplate.query()
guarantees the order of the resultset and if I can dump it into a LinkedList
and pass it along :)
Thanks!
Edit: I should clarify that the query does include an order by
clause, hence my requirements for a resultset that guarantees order. I was incorrect regarding the ArrayList
not doing so. Since the jdbcTemplate is an interface the implementation would depend upon the db library. Should I make the assumption that an ArrayList
will be used or sort it again to be on the safe side?
发布评论
评论(3)
这是错误的。
ArrayList
确实保证顺序。它有一个 get(int index) 方法,可用于从指定的从 0 开始的索引中检索元素。add(T item)
方法会将它们按顺序添加到列表中。您可能会将 List 集合类型与 Set 接口混淆,后者与 List 类似,但不保证顺序。也就是说,它实际上并没有回答您的问题...
ResultSet
在没有指定顺序的情况下,将按照表的自然 顺序返回数据。这通常基于 PK 字段,但并非所有 DBMS 都如此。如果顺序很重要,请明确指定。即使它现在以所需的顺序返回,对数据库架构的更改也可能会影响以后并打破代码所做的假设。明确更好,并且可以更清楚地表达代码的意图。
从编辑更新到问题:
如果查询中指定了 order by,则可以 100% 依赖结果集的顺序。无需再次排序。
This is just wrong. An
ArrayList
does guarantee order. It has aget(int index)
method which can be used to retrieve an element from the specified 0-based index. Theadd(T item)
method will add them in sequence to the list. You may be confusing the List collection type with the Set interface, which is similar to a List except for it does not guarantee order.That said, it does not actually answer your question...
The
ResultSet
, without a specified ordering, will return the data in the table's natural order. This is usually based of of the PK field(s) but this is not true for all DBMSs.If order is important, specify it to be certain. Even if it comes back in the desired order now, changes to the DB schema might affect this later and break assumptions made by your code. Being explicit is better, and will express the code's intent clearer.
Update from edit to question:
If an order by is specified in the query, you can rely 100% on the order of the result set. No need to sort it again.
这能回答你的问题吗?
来自 RowMapperResultSetExtractor。此类在
JdbcTemplate
中的多个query()
方法中使用。没有地方可以混合结果集记录。事实上,结果集更像是一个迭代器或流,而不是一个完整的集合。这意味着更改顺序的效率非常低,因为您必须提前加载结果集中的所有记录。
Does this answer your question?
From RowMapperResultSetExtractor. This class is used in in several
query()
methods inJdbcTemplate
.No place for result set records mixing. In fact, result set is more like an iterator or stream rather than a full collection. This means that it would be very inefficient to change the order because you would have to load all the records from result set in advance.
如果您在sql中使用order by condtion,则不需要再次对记录进行排序,
检查您的存储过程游标输出查询“
OPEN o_provisionalOrders FOR SELECT cols from table order by 条件
”是正确的。
Its not required to sort records again if you are using order by condtion in sql,
check your stored procedure cursor output query "
OPEN o_provisionalOrders FOR SELECT cols from table order by condtion
" is proper.