liveBook · Manning

RowMapper

As explained in Spring Start Here, Second Edition

A RowMapper is a Spring JDBC callback interface that converts one row of a result set into an application object.

A RowMapper tells JdbcTemplate how to turn one database result-set row into an application object. It bridges the columns returned by a SELECT query and the model your application uses. section 12.2

Mapping a purchase

For each row, create a Purchase, copy its id, product, and price from the result set, then return it. JdbcTemplate calls the mapper for each row and returns the mapped purchases. section 12.2

Listing 12.7 Using JdbcTemplate to select records from a database
@Repository
public class PurchaseRepository {
 
  // Omitted code
 
  public List<Purchase> findAllPurchases() {    #A
    String sql = "SELECT * FROM purchase";      #B
 
    RowMapper<Purchase> purchaseRowMapper = (r, i) -> {  #C
      Purchase rowObject = new Purchase();              #D
      rowObject.setId(r.getInt("id"));                  #D
      rowObject.setProduct(r.getString("product"));     #D
      rowObject.setPrice(r.getBigDecimal("price"));     #D
      return rowObject;                                 #D
    };
 
    return jdbc.query(sql, purchaseRowMapper);   #E
  }
}

See a RowMapper lambda in a repository query that maps database columns to Purchase fields. section 12.2

How Spring Start Here, Second Edition covers it

How deep it goes
Works through a lambda that creates a Purchase and assigns its id, product, and price from each result-set row.
Examples
Maps purchase table rows into Purchase objects using id, product, and price columns.
What you can do after
Apply it via JdbcTemplate by mapping each result-set row into a model object.
How it connects
Connects JdbcTemplate, ResultSet, SELECT queries, and repository-based data retrieval.

Also explained in

Compare how 3 books explain RowMapper →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.