001/*
002 * Copyright 2002-2018 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
022import org.springframework.lang.Nullable;
023
024/**
025 * An interface used by {@link JdbcTemplate} for mapping rows of a
026 * {@link java.sql.ResultSet} on a per-row basis. Implementations of this
027 * interface perform the actual work of mapping each row to a result object,
028 * but don't need to worry about exception handling.
029 * {@link java.sql.SQLException SQLExceptions} will be caught and handled
030 * by the calling JdbcTemplate.
031 *
032 * <p>Typically used either for {@link JdbcTemplate}'s query methods
033 * or for out parameters of stored procedures. RowMapper objects are
034 * typically stateless and thus reusable; they are an ideal choice for
035 * implementing row-mapping logic in a single place.
036 *
037 * <p>Alternatively, consider subclassing
038 * {@link org.springframework.jdbc.object.MappingSqlQuery} from the
039 * {@code jdbc.object} package: Instead of working with separate
040 * JdbcTemplate and RowMapper objects, you can build executable query
041 * objects (containing row-mapping logic) in that style.
042 *
043 * @author Thomas Risberg
044 * @author Juergen Hoeller
045 * @param <T> the result type
046 * @see JdbcTemplate
047 * @see RowCallbackHandler
048 * @see ResultSetExtractor
049 * @see org.springframework.jdbc.object.MappingSqlQuery
050 */
051@FunctionalInterface
052public interface RowMapper<T> {
053
054        /**
055         * Implementations must implement this method to map each row of data
056         * in the ResultSet. This method should not call {@code next()} on
057         * the ResultSet; it is only supposed to map values of the current row.
058         * @param rs the ResultSet to map (pre-initialized for the current row)
059         * @param rowNum the number of the current row
060         * @return the result object for the current row (may be {@code null})
061         * @throws SQLException if an SQLException is encountered getting
062         * column values (that is, there's no need to catch SQLException)
063         */
064        @Nullable
065        T mapRow(ResultSet rs, int rowNum) throws SQLException;
066
067}