类 DataAccessUtils
- java.lang.Object
- org.springframework.dao.support.DataAccessUtils
public abstract class DataAccessUtils extends Object
Miscellaneous utility methods for DAO implementations. Useful with any data access technology.- 从以下版本开始:
- 1.0.2
- 作者:
- Juergen Hoeller
构造器概要
构造器 构造器 说明 DataAccessUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static int
intResult(Collection<?> results)
Return a unique int result from the given Collection.static long
longResult(Collection<?> results)
Return a unique long result from the given Collection.static <T> T
nullableSingleResult(Collection<T> results)
Return a single result object from the given Collection.static <T> T
objectResult(Collection<?> results, Class<T> requiredType)
Return a unique result object from the given Collection.static <T> T
requiredSingleResult(Collection<T> results)
Return a single result object from the given Collection.static <T> T
requiredUniqueResult(Collection<T> results)
Return a unique result object from the given Collection.static <T> T
singleResult(Collection<T> results)
Return a single result object from the given Collection.static RuntimeException
translateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet)
Return a translated exception if this is appropriate, otherwise return the given exception as-is.static <T> T
uniqueResult(Collection<T> results)
Return a unique result object from the given Collection.
构造器详细资料
DataAccessUtils
public DataAccessUtils()
方法详细资料
singleResult
@Nullable public static <T> T singleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException
Return a single result object from the given Collection.Returns
null
if 0 result objects found; throws an exception if more than 1 element found.- 参数:
results
- the result Collection (can benull
)- 返回:
- the single result object, or
null
if none - 抛出:
IncorrectResultSizeDataAccessException
- if more than one element has been found in the given Collection
requiredSingleResult
public static <T> T requiredSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException
Return a single result object from the given Collection.Throws an exception if 0 or more than 1 element found.
- 参数:
results
- the result Collection (can benull
but is not expected to containnull
elements)- 返回:
- the single result object
- 抛出:
IncorrectResultSizeDataAccessException
- if more than one element has been found in the given CollectionEmptyResultDataAccessException
- if no element at all has been found in the given Collection
nullableSingleResult
@Nullable public static <T> T nullableSingleResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException
Return a single result object from the given Collection.Throws an exception if 0 or more than 1 element found.
- 参数:
results
- the result Collection (can benull
and is also expected to containnull
elements)- 返回:
- the single result object
- 抛出:
IncorrectResultSizeDataAccessException
- if more than one element has been found in the given CollectionEmptyResultDataAccessException
- if no element at all has been found in the given Collection- 从以下版本开始:
- 5.0.2
uniqueResult
@Nullable public static <T> T uniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException
Return a unique result object from the given Collection.Returns
null
if 0 result objects found; throws an exception if more than 1 instance found.- 参数:
results
- the result Collection (can benull
)- 返回:
- the unique result object, or
null
if none - 抛出:
IncorrectResultSizeDataAccessException
- if more than one result object has been found in the given Collection- 另请参阅:
CollectionUtils.hasUniqueObject(java.util.Collection<?>)
requiredUniqueResult
public static <T> T requiredUniqueResult(@Nullable Collection<T> results) throws IncorrectResultSizeDataAccessException
Return a unique result object from the given Collection.Throws an exception if 0 or more than 1 instance found.
- 参数:
results
- the result Collection (can benull
but is not expected to containnull
elements)- 返回:
- the unique result object
- 抛出:
IncorrectResultSizeDataAccessException
- if more than one result object has been found in the given CollectionEmptyResultDataAccessException
- if no result object at all has been found in the given Collection- 另请参阅:
CollectionUtils.hasUniqueObject(java.util.Collection<?>)
objectResult
public static <T> T objectResult(@Nullable Collection<?> results, @Nullable Class<T> requiredType) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException
Return a unique result object from the given Collection. Throws an exception if 0 or more than 1 result objects found, of if the unique result object is not convertible to the specified required type.- 参数:
results
- the result Collection (can benull
but is not expected to containnull
elements)- 返回:
- the unique result object
- 抛出:
IncorrectResultSizeDataAccessException
- if more than one result object has been found in the given CollectionEmptyResultDataAccessException
- if no result object at all has been found in the given CollectionTypeMismatchDataAccessException
- if the unique object does not match the specified required type
intResult
public static int intResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException
Return a unique int result from the given Collection. Throws an exception if 0 or more than 1 result objects found, of if the unique result object is not convertible to an int.- 参数:
results
- the result Collection (can benull
but is not expected to containnull
elements)- 返回:
- the unique int result
- 抛出:
IncorrectResultSizeDataAccessException
- if more than one result object has been found in the given CollectionEmptyResultDataAccessException
- if no result object at all has been found in the given CollectionTypeMismatchDataAccessException
- if the unique object in the collection is not convertible to an int
longResult
public static long longResult(@Nullable Collection<?> results) throws IncorrectResultSizeDataAccessException, TypeMismatchDataAccessException
Return a unique long result from the given Collection. Throws an exception if 0 or more than 1 result objects found, of if the unique result object is not convertible to a long.- 参数:
results
- the result Collection (can benull
but is not expected to containnull
elements)- 返回:
- the unique long result
- 抛出:
IncorrectResultSizeDataAccessException
- if more than one result object has been found in the given CollectionEmptyResultDataAccessException
- if no result object at all has been found in the given CollectionTypeMismatchDataAccessException
- if the unique object in the collection is not convertible to a long
translateIfNecessary
public static RuntimeException translateIfNecessary(RuntimeException rawException, PersistenceExceptionTranslator pet)
Return a translated exception if this is appropriate, otherwise return the given exception as-is.- 参数:
rawException
- an exception that we may wish to translatepet
- the PersistenceExceptionTranslator to use to perform the translation- 返回:
- a translated persistence exception if translation is possible, or the raw exception if it is not