Clean • Professional
RowMapper is a core interface in Spring JDBC that helps map rows of a database ResultSet into Java objects. It allows developers to convert SQL query results into domain objects cleanly and efficiently, avoiding manual iteration over the ResultSet.
RowMapper is an interface with a single method mapRow(ResultSet rs, int rowNum) that maps a single row of a ResultSet to a Java object.
JdbcTemplate.query() methods to fetch single or multiple rows from the database.👉 In simple words:
RowMapper = How each row of a query result becomes a Java object.
Example:
publicclassUserRowMapperimplementsRowMapper<User> {
@Override
public UsermapRow(ResultSet rs, int rowNum)throws SQLException {
Useruser=newUser();
user.setId(rs.getLong("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
return user;
}
}
// Usage with JdbcTemplate
List<User> users = jdbcTemplate.query("SELECT * FROM users",newUserRowMapper());
Alternative: BeanPropertyRowMapper can be used for automatic mapping based on field names:
List<User> users = jdbcTemplate.query(
"SELECT * FROM users",
newBeanPropertyRowMapper<>(User.class)
);

ResultSet to a Java object, avoiding manual iteration.RowMapper implementationBeanPropertyRowMapperWorks Seamlessly with JdbcTemplate: Used directly in query() methods for single or multiple row results.
String sql = "SELECT * FROM users";
List<User> users = jdbcTemplate.query(sql, (rs, rowNum) -> {
User user = new User();
user.setId(rs.getLong("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
return user;
});
org.springframework.jdbc.core.RowMapper<T> — a generic interface for mapping rows.T mapRow(ResultSet rs, int rowNum) — called for each row in the ResultSet.T.Optional example for completeness:
publicclassUserRowMapperimplementsRowMapper<User> {
@Override
public UsermapRow(ResultSet rs, int rowNum)throws SQLException {
Useruser=newUser();
user.setId(rs.getLong("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
return user;
}
}
RowMapper is used to map each row of a ResultSet to a Java object. You can implement it using an anonymous class, a lambda expression, or a separate reusable class for clean and maintainable code.

Maps each row of the ResultSet to a Java object directly in-place, without creating a separate class. Good for quick, one-off queries.
Stringsql="SELECT * FROM users";
List<User> users = jdbcTemplate.query(sql,newRowMapper<User>() {
@Override
public UsermapRow(ResultSet rs, int rowNum)throws SQLException {
Useruser=newUser();
user.setId(rs.getLong("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
return user;
}
});
Provides a concise way to implement RowMapper inline using a lambda, reducing boilerplate and improving readability.
List<User> users = jdbcTemplate.query(sql, (rs, rowNum) -> {
returnnewUser(
rs.getLong("id"),
rs.getString("name"),
rs.getString("email")
);
});
Encapsulates mapping logic in a dedicated class, making it reusable across multiple queries and keeping business logic clean and separate.
publicclassUserRowMapperimplementsRowMapper<User> {
@Override
public UsermapRow(ResultSet rs, int rowNum)throws SQLException {
returnnewUser(
rs.getLong("id"),
rs.getString("name"),
rs.getString("email")
);
}
}
Usage:
List<User> users = jdbcTemplate.query("SELECT * FROM users",newUserRowMapper());
Use RowMapper when:
Not necessary when:
queryForObject)| Aspect | RowMapper | BeanPropertyRowMapper |
|---|---|---|
| Definition | Interface you implement to manually map each ResultSet row to an object | Built-in RowMapper that automatically maps columns to object properties by name |
| Mapping Control | Full control, custom mapping logic possible | Automatic mapping, requires matching column names with Java property names |
| Boilerplate | More code if many fields | Minimal code, no manual field mapping needed |
| Reusability | Can be reused if implemented as a separate class | Automatically reusable for entities with matching column-property names |
| Use Case | When column names differ from field names or custom logic is needed | When database columns and object fields match exactly, for quick mapping |
Example: Using BeanPropertyRowMapper
List<User> users = jdbcTemplate.query(
"SELECT * FROM users",
newBeanPropertyRowMapper<>(User.class)
);
RowMapper is a core interface in Spring JDBC for mapping database rows to Java objects.
JdbcTemplate and NamedParameterJdbcTemplateBeanPropertyRowMapper) depending on the use case