001/* 002 * Copyright 2002-2008 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.object; 018 019import java.sql.ResultSet; 020import java.sql.SQLException; 021import java.util.Map; 022import javax.sql.DataSource; 023 024/** 025 * Reusable query in which concrete subclasses must implement the abstract 026 * mapRow(ResultSet, int) method to convert each row of the JDBC ResultSet 027 * into an object. 028 * 029 * <p>Simplifies MappingSqlQueryWithParameters API by dropping parameters and 030 * context. Most subclasses won't care about parameters. If you don't use 031 * contextual information, subclass this instead of MappingSqlQueryWithParameters. 032 * 033 * @author Rod Johnson 034 * @author Thomas Risberg 035 * @author Jean-Pierre Pawlak 036 * @see MappingSqlQueryWithParameters 037 */ 038public abstract class MappingSqlQuery<T> extends MappingSqlQueryWithParameters<T> { 039 040 /** 041 * Constructor that allows use as a JavaBean. 042 */ 043 public MappingSqlQuery() { 044 } 045 046 /** 047 * Convenient constructor with DataSource and SQL string. 048 * @param ds DataSource to use to obtain connections 049 * @param sql SQL to run 050 */ 051 public MappingSqlQuery(DataSource ds, String sql) { 052 super(ds, sql); 053 } 054 055 056 /** 057 * This method is implemented to invoke the simpler mapRow 058 * template method, ignoring parameters. 059 * @see #mapRow(ResultSet, int) 060 */ 061 @Override 062 protected final T mapRow(ResultSet rs, int rowNum, Object[] parameters, Map<?, ?> context) 063 throws SQLException { 064 065 return mapRow(rs, rowNum); 066 } 067 068 /** 069 * Subclasses must implement this method to convert each row of the 070 * ResultSet into an object of the result type. 071 * <p>Subclasses of this class, as opposed to direct subclasses of 072 * MappingSqlQueryWithParameters, don't need to concern themselves 073 * with the parameters to the execute method of the query object. 074 * @param rs ResultSet we're working through 075 * @param rowNum row number (from 0) we're up to 076 * @return an object of the result type 077 * @throws SQLException if there's an error extracting data. 078 * Subclasses can simply not catch SQLExceptions, relying on the 079 * framework to clean up. 080 */ 081 protected abstract T mapRow(ResultSet rs, int rowNum) throws SQLException; 082 083}