什么是 RowMapper、ResultSetExtractor、绑定变量和查询类型?
我知道如何使用 JDBC Template 和 DAO,但我仍然有疑问:
RowMapper
和ResultSetExtractor
有什么用?- 什么是绑定变量?
- 查询是列表类型吗?
I know how to use JDBC Template and DAO, but I still have questions regarding it:
- What's the use of
RowMapper
andResultSetExtractor
? - What is a bind variable?
- Are queries a type of List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Q1:这些接口与
JdbcTemplate
在查询数据库时经常使用的RowCallbackHandler
一起使用。您实现哪个接口、如何实现它以及在 JdbcTemplate 中使用哪种方法取决于您的数据库以及您想要执行的查询类型。来自 Spring API 文档 和一些其他注释:RowMapper:
,即当数据库中的行与结果对象之间存在一对一关系时,通常使用 RowMapper 来映射对象。结果对象。
ResultSetExtractor:
ResultSetExtractor
的实现通常会从多行中创建一个对象,然后返回该对象。它是无状态的,因为实现类在方法调用之间不保留任何状态。RowCallbackHandler:
RowCallbackHandler
用于更新或删除行等查询。此外,当您需要跟踪ResultSet
中的状态(例如 RowCountCallbackHandler。Q1: Theses interfaces are together with
RowCallbackHandler
frequently used by theJdbcTemplate
when queering a database. Which interface you implement, how you implement it and which method you use in theJdbcTemplate
depends on your database and what kind of query you would like to execute. From the Spring API doc and some additional comments:RowMapper:
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:
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:
The
RowCallbackHandler
is used for queries such as updating or deleting rows. Additionally, it is used when you need to track a state across theResultSet
, such as number of rows in the RowCountCallbackHandler.