@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -18,43 +18,45 @@ package org.springframework.jdbc.object;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A concrete variant of {@link SqlQuery} which can be configured
|
||||
* with a {@link RowMapper}.
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
* @see #setRowMapperClass
|
||||
*/
|
||||
public class GenericSqlQuery<T> extends SqlQuery<T> {
|
||||
|
||||
Class<?> rowMapperClass;
|
||||
|
||||
RowMapper<?> rowMapper;
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void setRowMapperClass(Class<? extends RowMapper> rowMapperClass)
|
||||
throws IllegalAccessException, InstantiationException {
|
||||
private Class<? extends RowMapper> rowMapperClass;
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
if (!RowMapper.class.isAssignableFrom(rowMapperClass))
|
||||
throw new IllegalStateException("The specified class '" +
|
||||
rowMapperClass.getName() + " is not a sub class of " +
|
||||
"'org.springframework.jdbc.core.RowMapper'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(rowMapperClass, "The 'rowMapperClass' property is required");
|
||||
Assert.notNull(this.rowMapperClass, "'rowMapperClass' is required");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected RowMapper<T> newRowMapper(Object[] parameters, Map<?, ?> context) {
|
||||
try {
|
||||
return (RowMapper<T>) rowMapperClass.newInstance();
|
||||
}
|
||||
catch (InstantiationException e) {
|
||||
throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e);
|
||||
}
|
||||
catch (IllegalAccessException e) {
|
||||
throw new InvalidDataAccessResourceUsageException("Unable to instantiate RowMapper", e);
|
||||
}
|
||||
return BeanUtils.instantiateClass(this.rowMapperClass);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user