I know how to use JDBC Template and DAO, but I still have questions regarding it:
RowMapper
and ResultSetExtractor
?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With