zoukankan      html  css  js  c++  java
  • Spring Boot集成Mybatis分页插件pagehelper

    引入依赖

            <!--分页插件开始-->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-autoconfigure</artifactId>
                <version>2.3.1.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.3.0</version>
            </dependency>
            <!--分页插件结束-->

    配置application.properties

    # 配置分页插件pagehelper
    pagehelper.helper-dialect=mysql
    pagehelper.reasonable=true
    pagehelper.support-methods-arguments=true
    pagehelper.params=count=countSql

    Service层

    import com.github.pagehelper.Page;
    import com.github.pagehelper.PageHelper;
    
    
    
        public List<Video> listVideo() {
            try
            {
    //            Object cacheObj=baseCache.getTenMinteCache().get(CacheKeyManager.INDEX_VIDEO_LIST_KEY,()->{
                Page<Object> objects = PageHelper.offsetPage(1, 4, true);
                List<Video> videos = videoMapper.ListVideo();
                System.out.println(objects);
                //System.out.println("=========="+videos);
                return videos;
    //            });
    //            if (cacheObj instanceof List){
    //                return (List<Video>)cacheObj;
    //            }
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }

    PageMethod.java

    PageHelper还有很多其他方法,可以尝试用其他的方法

    /*
     * The MIT License (MIT)
     *
     * Copyright (c) 2014-2017 abel533@gmail.com
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */
    
    package com.github.pagehelper.page;
    
    import com.github.pagehelper.ISelect;
    import com.github.pagehelper.Page;
    import com.github.pagehelper.util.PageObjectUtil;
    
    import java.util.Properties;
    
    /**
     * 基础分页方法
     *
     * @author liuzh
     */
    public abstract class PageMethod {
        protected static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();
        protected static boolean DEFAULT_COUNT = true;
    
        /**
         * 设置 Page 参数
         *
         * @param page
         */
        protected static void setLocalPage(Page page) {
            LOCAL_PAGE.set(page);
        }
    
        /**
         * 获取 Page 参数
         *
         * @return
         */
        public static <T> Page<T> getLocalPage() {
            return LOCAL_PAGE.get();
        }
    
        /**
         * 移除本地变量
         */
        public static void clearPage() {
            LOCAL_PAGE.remove();
        }
    
        /**
         * 获取任意查询方法的count总数
         *
         * @param select
         * @return
         */
        public static long count(ISelect select) {
            Page<?> page = startPage(1, -1, true);
            select.doSelect();
            return page.getTotal();
        }
    
        /**
         * 开始分页
         *
         * @param params
         */
        public static <E> Page<E> startPage(Object params) {
            Page<E> page = PageObjectUtil.getPageFromObject(params, true);
            //当已经执行过orderBy的时候
            Page<E> oldPage = getLocalPage();
            if (oldPage != null && oldPage.isOrderByOnly()) {
                page.setOrderBy(oldPage.getOrderBy());
            }
            setLocalPage(page);
            return page;
        }
    
        /**
         * 开始分页
         *
         * @param pageNum  页码
         * @param pageSize 每页显示数量
         */
        public static <E> Page<E> startPage(int pageNum, int pageSize) {
            return startPage(pageNum, pageSize, DEFAULT_COUNT);
        }
    
        /**
         * 开始分页
         *
         * @param pageNum  页码
         * @param pageSize 每页显示数量
         * @param count    是否进行count查询
         */
        public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count) {
            return startPage(pageNum, pageSize, count, null, null);
        }
    
        /**
         * 开始分页
         *
         * @param pageNum  页码
         * @param pageSize 每页显示数量
         * @param orderBy  排序
         */
        public static <E> Page<E> startPage(int pageNum, int pageSize, String orderBy) {
            Page<E> page = startPage(pageNum, pageSize);
            page.setOrderBy(orderBy);
            return page;
        }
    
        /**
         * 开始分页
         *
         * @param pageNum      页码
         * @param pageSize     每页显示数量
         * @param count        是否进行count查询
         * @param reasonable   分页合理化,null时用默认配置
         * @param pageSizeZero true且pageSize=0时返回全部结果,false时分页,null时用默认配置
         */
        public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {
            Page<E> page = new Page<E>(pageNum, pageSize, count);
            page.setReasonable(reasonable);
            page.setPageSizeZero(pageSizeZero);
            //当已经执行过orderBy的时候
            Page<E> oldPage = getLocalPage();
            if (oldPage != null && oldPage.isOrderByOnly()) {
                page.setOrderBy(oldPage.getOrderBy());
            }
            setLocalPage(page);
            return page;
        }
    
        /**
         * 开始分页
         *
         * @param offset 起始位置,偏移位置
         * @param limit  每页显示数量
         */
        public static <E> Page<E> offsetPage(int offset, int limit) {
            return offsetPage(offset, limit, DEFAULT_COUNT);
        }
    
        /**
         * 开始分页
         *
         * @param offset 起始位置,偏移位置
         * @param limit  每页显示数量
         * @param count  是否进行count查询
         */
        public static <E> Page<E> offsetPage(int offset, int limit, boolean count) {
            Page<E> page = new Page<E>(new int[]{offset, limit}, count);
            //当已经执行过orderBy的时候
            Page<E> oldPage = getLocalPage();
            if (oldPage != null && oldPage.isOrderByOnly()) {
                page.setOrderBy(oldPage.getOrderBy());
            }
            setLocalPage(page);
            return page;
        }
    
        /**
         * 排序
         *
         * @param orderBy
         */
        public static void orderBy(String orderBy) {
            Page<?> page = getLocalPage();
            if (page != null) {
                page.setOrderBy(orderBy);
            } else {
                page = new Page();
                page.setOrderBy(orderBy);
                page.setOrderByOnly(true);
                setLocalPage(page);
            }
        }
    
        /**
         * 设置参数
         *
         * @param properties 插件属性
         */
        protected static void setStaticProperties(Properties properties){
            //defaultCount,这是一个全局生效的参数,多数据源时也是统一的行为
            if(properties != null){
                DEFAULT_COUNT = Boolean.valueOf(properties.getProperty("defaultCount", "true"));
            }
        }
    
    }

    效果

  • 相关阅读:
    selenium入门:基于界面的自动化测试
    selenium入门:环境搭建
    Python入门:迭代器
    Python入门:生成器并行(协程)
    Python入门:生成器应用-斐波拉契数列
    Python入门:生成器定义
    Python入门:装饰器案例3
    Python入门:装饰器案例2
    Python入门:装饰器案例1
    Spring MVC 上传文件---依赖引用
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13417048.html
Copyright © 2011-2022 走看看