001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.core;
018
019import java.sql.ResultSet;
020import java.sql.SQLException;
021
022/**
023 * An interface used by {@link JdbcTemplate} for mapping rows of a
024 * {@link java.sql.ResultSet} on a per-row basis. Implementations of this
025 * interface perform the actual work of mapping each row to a result object,
026 * but don't need to worry about exception handling.
027 * {@link java.sql.SQLException SQLExceptions} will be caught and handled
028 * by the calling JdbcTemplate.
029 *
030 * <p>Typically used either for {@link JdbcTemplate}'s query methods
031 * or for out parameters of stored procedures. RowMapper objects are
032 * typically stateless and thus reusable; they are an ideal choice for
033 * implementing row-mapping logic in a single place.
034 *
035 * <p>Alternatively, consider subclassing
036 * {@link org.springframework.jdbc.object.MappingSqlQuery} from the
037 * {@code jdbc.object} package: Instead of working with separate
038 * JdbcTemplate and RowMapper objects, you can build executable query
039 * objects (containing row-mapping logic) in that style.
040 *
041 * @author Thomas Risberg
042 * @author Juergen Hoeller
043 * @see JdbcTemplate
044 * @see RowCallbackHandler
045 * @see ResultSetExtractor
046 * @see org.springframework.jdbc.object.MappingSqlQuery
047 */
048public interface RowMapper<T> {
049
050        /**
051         * Implementations must implement this method to map each row of data
052         * in the ResultSet. This method should not call {@code next()} on
053         * the ResultSet; it is only supposed to map values of the current row.
054         * @param rs the ResultSet to map (pre-initialized for the current row)
055         * @param rowNum the number of the current row
056         * @return the result object for the current row (may be {@code null})
057         * @throws SQLException if a SQLException is encountered getting
058         * column values (that is, there's no need to catch SQLException)
059         */
060        T mapRow(ResultSet rs, int rowNum) throws SQLException;
061
062}