Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are RowMapper, ResultSetExtractor, bind variables and query types?

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?
like image 475
user962206 Avatar asked Dec 10 '22 04:12

user962206


1 Answers

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.

like image 135
matsev Avatar answered Dec 11 '22 18:12

matsev