/** * Copyright (c) 2011-2020, hubin (jobob@qq.com). *

* 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 *

* http://www.apache.org/licenses/LICENSE-2.0 *

* 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.plugins.pagination; import com.baomidou.mybatisplus.exceptions.MybatisPlusException; /** *

* 分页辅助类 *

* * @author liutao , hubin * @since 2017-06-24 */ public class PageHelper { // 分页本地线程变量 private static final ThreadLocal LOCAL_PAGE = new ThreadLocal<>(); /** *

* 获取总条数 *

*/ public static int getTotal() { if (isPageable()) { return LOCAL_PAGE.get().getTotal(); } else { throw new MybatisPlusException("The current thread does not start paging. Please call before PageHelper.startPage"); } } /** *

* 释放资源并获取总条数 *

*/ public static int freeTotal() { int total = getTotal(); remove();// 释放资源 return total; } /** *

* 获取分页 *

*/ public static Pagination getPagination() { return LOCAL_PAGE.get(); } /** *

* 设置分页 *

*/ public static void setPagination(Pagination page) { LOCAL_PAGE.set(page); } /** *

* 启动分页 *

* * @param current 当前页 * @param size 页大小 */ public static void startPage(int current, int size) { LOCAL_PAGE.set(new Pagination(current, size)); } /** *

* 是否存在分页 *

* * @return */ public static boolean isPageable() { return LOCAL_PAGE.get() != null; } /** *

* 释放资源 *

*/ public static void remove() { LOCAL_PAGE.remove(); } }