|
@@ -222,21 +222,13 @@ public interface BaseMapper<T> extends Mapper<T> {
|
|
* @param throwEx boolean 参数,为true如果存在多个结果直接抛出异常
|
|
* @param throwEx boolean 参数,为true如果存在多个结果直接抛出异常
|
|
*/
|
|
*/
|
|
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, boolean throwEx) {
|
|
default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, boolean throwEx) {
|
|
- List<T> list = new ArrayList<>();
|
|
|
|
- //TODO 后期配合Page参数可以做数据库分页,下面的换成RowBounds做限制结果集也行
|
|
|
|
- this.selectList(queryWrapper, resultContext -> {
|
|
|
|
- T resultObject = resultContext.getResultObject();
|
|
|
|
- list.add(resultObject);
|
|
|
|
- if (!throwEx || resultContext.getResultCount() > 1) {
|
|
|
|
- resultContext.stop();
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ List<T> list = this.selectList(queryWrapper);
|
|
int size = list.size();
|
|
int size = list.size();
|
|
if (size == 1) {
|
|
if (size == 1) {
|
|
return list.get(0);
|
|
return list.get(0);
|
|
} else if (size > 1) {
|
|
} else if (size > 1) {
|
|
if (throwEx) {
|
|
if (throwEx) {
|
|
- throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found multiple records");
|
|
|
|
|
|
+ throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
|
|
}
|
|
}
|
|
return list.get(0);
|
|
return list.get(0);
|
|
}
|
|
}
|