Anil Jagtap
Anil Jagtap

Reputation: 1810

Difference between MappingSqlQuery or RowMapper and which one is recommended to perform select operation?

I want to perform a SELECT operation. When I searched on google I got MappingSqlQuery and RowMapper examples. I am confused about which one is best, performance-wise, to perform a SELECT operation?

Upvotes: 1

Views: 605

Answers (1)

Ken Chan
Ken Chan

Reputation: 90517

MappingSqlQuery actually will create a RowMapper behind the scenes and use JdbcTemplate to query the result:

public List<T> execute(@Nullable Object[] params, @Nullable Map<?, ?> context) throws DataAccessException {
    validateParameters(params);
    RowMapper<T> rowMapper = newRowMapper(params, context);
    return getJdbcTemplate().query(newPreparedStatementCreator(params), rowMapper);
}

So from a performance point of view, directly using RowMapper with JDBCTemplate should be a little little bit faster than MappingSqlQuery. But it is micro performance differences which you will not feel.

Which one to use depends on your preferences. For MappingSqlQuery, you have to create a new class for every query. For RowMapper with JdbcTemplate, you just need to create a single class for a group of queries that do similar things. For a system that has a lot of queries, I would choose the latter one as I don't like to create so many MappingSqlQuery classes.

Upvotes: 1

Related Questions