瀏覽代碼

调整selectOne方法.

nieqiurong 1 年之前
父節點
當前提交
6fa16685d1

+ 11 - 3
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/mapper/BaseMapper.java

@@ -27,6 +27,7 @@ import org.apache.ibatis.exceptions.TooManyResultsException;
 import org.apache.ibatis.session.ResultHandler;
 
 import java.io.Serializable;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -221,14 +222,21 @@ public interface BaseMapper<T> extends Mapper<T> {
      * @param throwEx      boolean 参数,为true如果存在多个结果直接抛出异常
      */
     default T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper, boolean throwEx) {
-        List<T> list = this.selectList(queryWrapper);
-        // 抄自 DefaultSqlSession#selectOne
+        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();
+            }
+        });
         int size = list.size();
         if (size == 1) {
             return list.get(0);
         } else if (size > 1) {
             if (throwEx) {
-                throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
+                throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found multiple records");
             }
             return list.get(0);
         }

+ 7 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -910,7 +910,6 @@ class H2UserTest extends BaseTest {
         System.out.println("---------------selectBatchIds-------------------");
         baseMapper.selectBatchIds(ids, resultContext -> System.out.println(resultContext.getResultObject()));
         System.out.println("---------------selectList-------------------");
-        baseMapper.selectList(Wrappers.emptyWrapper(), resultContext -> System.out.println(resultContext.getResultObject()));
         System.out.println("---------------selectObjs-------------------");
         baseMapper.selectObjs(Wrappers.emptyWrapper(), (ResultHandler<Long>) resultContext -> System.out.println(resultContext.getResultObject()));
         System.out.println("---------------selectByMap-------------------");
@@ -921,4 +920,11 @@ class H2UserTest extends BaseTest {
         baseMapper.selectMaps(Wrappers.emptyWrapper(), resultContext -> resultContext.getResultObject().forEach((k, v) -> System.out.println(k + "--------" + v)));
     }
 
+    @Test
+    void testSelectOne() {
+        Assertions.assertTrue(userService.list().size() > 2);
+        Assertions.assertThrows(TooManyResultsException.class, () -> userService.getBaseMapper().selectOne(Wrappers.emptyWrapper()));
+        Assertions.assertNotNull(userService.getBaseMapper().selectOne(Wrappers.emptyWrapper(), false));
+    }
+
 }