org.simpleflatmapper.jdbc.spring.ResultSetExtractorImpl Maven / Gradle / Ivy
package org.simpleflatmapper.jdbc.spring;
import org.simpleflatmapper.jdbc.JdbcMapper;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
import org.simpleflatmapper.util.ListCollector;
import org.simpleflatmapper.util.CheckedConsumer;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* Provide integration point with Spring JdbcTemplate.
*
* It implements {@link RowMapper}, {@link PreparedStatementCallback} and {@link ResultSetExtractor}.
* Because some JdbcTemplate template signature match against a few of those type you might need to downcast, declare the variable with a specific type or use the type specific method in {@link JdbcTemplateMapperFactory}.
*
*
*
*
* class MyDao {
* private final JdbcTemplateMapper<DbObject> jdbcMapper =
* JdbcTemplateMapperFactory.newInstance().newMapper(DbObject.class);
* private final RowMapper<DbObject> rowMapper = jdbcMapper;
*
* public void doSomething() {
* List<DbObject> results = template.query(DbHelper.TEST_DB_OBJECT_QUERY, rowMapper);
* }
*
* public void doSomethingElse() {
* template
* .query(TEST_DB_OBJECT_QUERY,
* jdbcMapper.newResultSetExtractor((o) -> System.out.println(o.toString())));
* }
* }
*
*
* @param the mapped type
* @see JdbcMapperFactory
* @see JdbcMapper
* @see JdbcTemplateMapperFactory#newPreparedStatementCallback(Class)
* @see JdbcTemplateMapperFactory#newResultSetExtractor(Class)
* @see JdbcTemplateMapperFactory#newRowMapper(Class)
*
*/
public final class ResultSetExtractorImpl implements ResultSetExtractor> {
private final JdbcMapper mapper;
public ResultSetExtractorImpl(JdbcMapper mapper) {
this.mapper = mapper;
}
@Override
public List extractData(ResultSet rs) throws SQLException,
DataAccessException {
return mapper.forEach(rs, new ListCollector()).getList();
}
public > ResultSetExtractor newResultSetExtractor(final H handler) {
return new ResultSetExtractor() {
@Override
public H extractData(ResultSet rs) throws SQLException,
DataAccessException {
return mapper.forEach(rs, handler);
}
};
}
}