|
|
|
|
@ -1,5 +1,5 @@
@@ -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,57 @@ package org.springframework.jdbc.object;
@@ -18,43 +18,57 @@ 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 #setRowMapper |
|
|
|
|
* @see #setRowMapperClass |
|
|
|
|
*/ |
|
|
|
|
public class GenericSqlQuery<T> extends SqlQuery<T> { |
|
|
|
|
|
|
|
|
|
Class<?> rowMapperClass; |
|
|
|
|
private RowMapper<T> rowMapper; |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("rawtypes") |
|
|
|
|
private Class<? extends RowMapper> rowMapperClass; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RowMapper<?> rowMapper; |
|
|
|
|
/** |
|
|
|
|
* 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) |
|
|
|
|
throws IllegalAccessException, InstantiationException { |
|
|
|
|
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.isTrue(this.rowMapper != null || this.rowMapperClass != null, |
|
|
|
|
"'rowMapper' or '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 (this.rowMapper != null ? this.rowMapper : BeanUtils.instantiateClass(this.rowMapperClass)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|