Browse Source

fix: 可能会出现的 npe

miemie 6 năm trước cách đây
mục cha
commit
63cd456132

+ 3 - 6
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/metadata/IPage.java

@@ -15,13 +15,13 @@
  */
 package com.baomidou.mybatisplus.core.metadata;
 
-import static java.util.stream.Collectors.toList;
-
 import java.io.Serializable;
 import java.util.List;
 import java.util.Map;
 import java.util.function.Function;
 
+import static java.util.stream.Collectors.toList;
+
 /**
  * <p>
  * 分页 Page 对象接口
@@ -143,13 +143,10 @@ public interface IPage<T> extends Serializable {
      * <p>
      * 当前满足条件总行数
      * </p>
-     * <p>
-     * 当 total 为 null 或者大于 0 分页插件不在查询总数
-     * </p>
      *
      * @return 总条数
      */
-    Long getTotal();
+    long getTotal();
 
     /**
      * <p>

+ 10 - 10
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Page.java

@@ -15,12 +15,12 @@
  */
 package com.baomidou.mybatisplus.extension.plugins.pagination;
 
-import java.util.Collections;
-import java.util.List;
-
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 
+import java.util.Collections;
+import java.util.List;
+
 /**
  * <p>
  * 简单分页模型
@@ -87,25 +87,25 @@ public class Page<T> implements IPage<T> {
      * @param size    每页显示条数
      */
     public Page(long current, long size) {
-        this(current, size, 0);
+        this(current, size, 0L);
     }
 
-    public Page(long current, long size, long total) {
+    public Page(long current, long size, Long total) {
         this(current, size, total, true);
     }
 
     public Page(long current, long size, boolean isSearchCount) {
-        this(current, size, 0, isSearchCount);
+        this(current, size, 0L, isSearchCount);
     }
 
-    public Page(long current, long size, long total, boolean isSearchCount) {
+    public Page(long current, long size, Long total, boolean isSearchCount) {
         if (current > 1) {
             this.current = current;
         }
         this.size = size;
         this.total = total;
         this.isSearchCount = isSearchCount;
-        if (total < 0) {
+        if (total == null || total != 0) {
             this.isSearchCount = false;
         }
     }
@@ -144,8 +144,8 @@ public class Page<T> implements IPage<T> {
     }
 
     @Override
-    public Long getTotal() {
-        return this.total;
+    public long getTotal() {
+        return this.total != null ? this.total : 0;
     }
 
     @Override