什么是 RowMapper、ResultSetExtractor、绑定变量和查询类型?

发布于 2024-12-20 01:49:27 字数 165 浏览 1 评论 0原文

我知道如何使用 JDBC Template 和 DAO,但我仍然有疑问:

  1. RowMapperResultSetExtractor 有什么用?
  2. 什么是绑定变量?
  3. 查询是列表类型吗?

I know how to use JDBC Template and DAO, but I still have questions regarding it:

  1. What's the use of RowMapper and ResultSetExtractor?
  2. What is a bind variable?
  3. Are queries a type of List?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-12-27 01:49:28

Q1:这些接口与 JdbcTemplate 在查询数据库时经常使用的 RowCallbackHandler 一起使用。您实现哪个接口、如何实现它以及在 JdbcTemplate 中使用哪种方法取决于您的数据库以及您想要执行的查询类型。来自 Spring API 文档 和一些其他注释:

RowMapper

JdbcTemplate 使用的接口,用于按行映射 ResultSet 的行。此接口的实现执行将每行映射到结果对象的实际工作

,即当数据库中的行与结果对象之间存在一对一关系时,通常使用 RowMapper 来映射对象。结果对象。

ResultSetExtractor

ResultSetExtractor 对象通常是无状态的,因此可重用

ResultSetExtractor 的实现通常会从多行中创建一个对象,然后返回该对象。它是无状态的,因为实现类在方法调用之间不保留任何状态。

RowCallbackHandler

此接口的实现执行处理每一行的实际工作[...]与 ResultSetExtractor 相比,RowCallbackHandler 对象通常是有状态的:它将结果状态保留在对象内,以供以后检查。< /p>

RowCallbackHandler 用于更新或删除行等查询。此外,当您需要跟踪 ResultSet 中的状态(例如 RowCountCallbackHandler

Q1: Theses interfaces are together with RowCallbackHandler frequently used by the JdbcTemplate when queering a database. Which interface you implement, how you implement it and which method you use in the JdbcTemplate depends on your database and what kind of query you would like to execute. From the Spring API doc and some additional comments:

RowMapper:

An interface used by JdbcTemplate for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object

i.e. the RowMapper is commonly used to map objects when there is a one-to-one relationship between a row in the database and the resulting object.

ResultSetExtractor:

ResultSetExtractor object is typically stateless and thus reusable

Implementations of the ResultSetExtractor typically creates one object out of several rows, that is subsequently returned. It is stateless because the implementing class does not preserve any state between method calls.

RowCallbackHandler:

Implementations of this interface perform the actual work of processing each row [...] In contrast to a ResultSetExtractor, a RowCallbackHandler object is typically stateful: It keeps the result state within the object, to be available for later inspection.

The RowCallbackHandler is used for queries such as updating or deleting rows. Additionally, it is used when you need to track a state across the ResultSet, such as number of rows in the RowCountCallbackHandler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文