Bläddra i källkod

重构分页走起

hubin 7 år sedan
förälder
incheckning
7cfe570c05

+ 92 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/IPage.java

@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination;
+
+/**
+ * <p>
+ * 分页 Page 对象接口
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-09
+ */
+public interface IPage {
+
+    /**
+     * <p>
+     * 计算当前分页偏移量
+     * </p>
+     *
+     * @param current 当前页
+     * @param size    每页显示数量
+     * @return
+     */
+    default long offsetCurrent(long current, long size) {
+        return current > 0 ? (current - 1) * size : 0;
+    }
+
+    /**
+     * <p>
+     * 当前分页总页数
+     * </p>
+     *
+     * @return
+     */
+    default long getPages() {
+        if (this.size() == 0) {
+            return 0L;
+        }
+        long pages = this.total() / this.size();
+        if (this.total() % this.size() != 0) {
+            pages++;
+        }
+        return pages;
+    }
+
+    /**
+     * 总数
+     */
+    /**
+     * <p>
+     * 当前分页总页数
+     * </p>
+     *
+     * @return
+     */
+    long total();
+
+    /**
+     * 每页显示条数,默认 10
+     */
+    /**
+     * <p>
+     * 当前分页总页数
+     * </p>
+     *
+     * @return
+     */
+    long size();
+
+    /**
+     * <p>
+     * 当前页,默认 1
+     * </p>
+     *
+     * @return
+     */
+    long current();
+
+}

+ 131 - 0
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/pagination/Pagination.java

@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2011-2020, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.extension.plugins.pagination;
+
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 简单分页模型
+ * </p>
+ *
+ * @author hubin
+ * @since 2018-06-09
+ */
+public class Pagination<T> implements IPage, Serializable {
+
+    /**
+     * 总数
+     */
+    private long total;
+
+    /**
+     * 每页显示条数,默认 10
+     */
+    private long size = 10;
+
+    /**
+     * 当前页
+     */
+    private long current = 1;
+
+    /**
+     * 查询总记录数(默认 true)
+     */
+    private boolean searchCount = true;
+
+    public Pagination() {
+        // to do nothing
+    }
+
+    /**
+     * <p>
+     * 分页构造函数
+     * </p>
+     *
+     * @param current 当前页
+     * @param size    每页显示条数
+     */
+    public Pagination(long current, long size) {
+        this(current, size, true);
+    }
+
+    public Pagination(long current, long size, boolean searchCount) {
+        if (current > 1) {
+            this.current = current;
+        }
+        this.size = size;
+        this.searchCount = searchCount;
+    }
+
+    /**
+     * <p>
+     * 存在上一页
+     * </p>
+     *
+     * @return true / false
+     */
+    public boolean hasPrevious() {
+        return this.current > 1;
+    }
+
+    /**
+     * <p>
+     * 存在下一页
+     * </p>
+     *
+     * @return true / false
+     */
+    public boolean hasNext() {
+        return this.current < this.getPages();
+    }
+
+    public void setTotal(long total) {
+        this.total = total;
+    }
+
+    @Override
+    public long total() {
+        return this.total;
+    }
+
+    public void setSize(long size) {
+        this.size = size;
+    }
+
+    @Override
+    public long size() {
+        return this.size;
+    }
+
+    public void setCurrent(long current) {
+        this.current = current;
+    }
+
+
+    @Override
+    public long current() {
+        return this.current;
+    }
+
+    public boolean isSearchCount() {
+        return searchCount;
+    }
+
+    public void setSearchCount(boolean searchCount) {
+        this.searchCount = searchCount;
+    }
+}