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


