/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.object;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Author: Thomas Risberg, Juergen Hoeller Type parameters: - <T> – the result type
See Also: Since: 3.0
/**
* A concrete variant of {@link SqlQuery} which can be configured
* with a {@link RowMapper}.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
* @param <T> the result type
* @see #setRowMapper
* @see #setRowMapperClass
*/
public class GenericSqlQuery<T> extends SqlQuery<T> {
@Nullable
private RowMapper<T> rowMapper;
@SuppressWarnings("rawtypes")
@Nullable
private Class<? extends RowMapper> rowMapperClass;
Set a specific RowMapper
instance to use for this query. Since: 4.3.2
/**
* Set a specific {@link RowMapper} instance to use for this query.
* @since 4.3.2
*/
public void setRowMapper(RowMapper<T> rowMapper) {
this.rowMapper = rowMapper;
}
/**
* Set a {@link RowMapper} class for this query, creating a fresh
* {@link RowMapper} instance per execution.
*/
@SuppressWarnings("rawtypes")
public void setRowMapperClass(Class<? extends RowMapper> rowMapperClass) {
this.rowMapperClass = rowMapperClass;
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
Assert.isTrue(this.rowMapper != null || this.rowMapperClass != null,
"'rowMapper' or 'rowMapperClass' is required");
}
@Override
@SuppressWarnings("unchecked")
protected RowMapper<T> newRowMapper(@Nullable Object[] parameters, @Nullable Map<?, ?> context) {
if (this.rowMapper != null) {
return this.rowMapper;
}
else {
Assert.state(this.rowMapperClass != null, "No RowMapper set");
return BeanUtils.instantiateClass(this.rowMapperClass);
}
}
}