zoukankan      html  css  js  c++  java
  • 分页工具类

      1 /*
      2  * The MIT License (MIT)
      3  *
      4  * Copyright (c) 2014 abel533@gmail.com
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     22  * THE SOFTWARE.
     23  */
     24 
     25 package com.github.pagehelper;
     26 
     27 import org.apache.ibatis.executor.Executor;
     28 import org.apache.ibatis.mapping.MappedStatement;
     29 import org.apache.ibatis.plugin.*;
     30 import org.apache.ibatis.session.ResultHandler;
     31 import org.apache.ibatis.session.RowBounds;
     32 
     33 import java.util.Properties;
     34 
     35 /**
     36  * Mybatis - 通用分页拦截器
     37  *
     38  * @author liuzh/abel533/isea533
     39  * @version 3.3.0
     40  *          项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
     41  */
     42 @SuppressWarnings({"rawtypes", "unchecked"})
     43 @Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}))
     44 public class PageHelper implements Interceptor {
     45     //sql工具类
     46     private SqlUtil sqlUtil;
     47 
     48     /**
     49      * 开始分页
     50      *
     51      * @param pageNum  页码
     52      * @param pageSize 每页显示数量
     53      */
     54     public static Page startPage(int pageNum, int pageSize) {
     55         return startPage(pageNum, pageSize, true);
     56     }
     57 
     58     /**
     59      * 开始分页
     60      *
     61      * @param pageNum  页码
     62      * @param pageSize 每页显示数量
     63      * @param orderBy  针对sqlserver - 建议在sql中直接包含order by
     64      */
     65     public static Page startPage(int pageNum, int pageSize, String orderBy) {
     66         return startPage(pageNum, pageSize, true).orderBy(orderBy);
     67     }
     68 
     69     /**
     70      * 开始分页
     71      *
     72      * @param pageNum  页码
     73      * @param pageSize 每页显示数量
     74      * @param count    是否进行count查询
     75      */
     76     public static Page startPage(int pageNum, int pageSize, boolean count) {
     77         return startPage(pageNum, pageSize, count, null);
     78     }
     79 
     80     /**
     81      * 开始分页
     82      *
     83      * @param pageNum    页码
     84      * @param pageSize   每页显示数量
     85      * @param count      是否进行count查询
     86      * @param reasonable 分页合理化,null时用默认配置
     87      */
     88     public static Page startPage(int pageNum, int pageSize, boolean count, Boolean reasonable) {
     89         return startPage(pageNum, pageSize, count, reasonable, null);
     90     }
     91 
     92     /**
     93      * 开始分页
     94      *
     95      * @param pageNum      页码
     96      * @param pageSize     每页显示数量
     97      * @param count        是否进行count查询
     98      * @param reasonable   分页合理化,null时用默认配置
     99      * @param pageSizeZero true且pageSize=0时返回全部结果,false时分页,null时用默认配置
    100      */
    101     public static Page startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {
    102         Page page = new Page(pageNum, pageSize, count);
    103         page.setReasonable(reasonable);
    104         page.setPageSizeZero(pageSizeZero);
    105         SqlUtil.setLocalPage(page);
    106         return page;
    107     }
    108 
    109     /**
    110      * 开始分页
    111      *
    112      * @param params
    113      */
    114     public static Page startPage(Object params) {
    115         Page page = SqlUtil.getPageFromObject(params);
    116         SqlUtil.setLocalPage(page);
    117         return page;
    118     }
    119 
    120 
    121     /**
    122      * Mybatis拦截器方法
    123      *
    124      * @param invocation 拦截器入参
    125      * @return 返回执行结果
    126      * @throws Throwable 抛出异常
    127      */
    128     public Object intercept(Invocation invocation) throws Throwable {
    129         return sqlUtil.processPage(invocation);
    130     }
    131 
    132     /**
    133      * 只拦截Executor
    134      *
    135      * @param target
    136      * @return
    137      */
    138     public Object plugin(Object target) {
    139         if (target instanceof Executor) {
    140             return Plugin.wrap(target, this);
    141         } else {
    142             return target;
    143         }
    144     }
    145 
    146     /**
    147      * 设置属性值
    148      *
    149      * @param p 属性值
    150      */
    151     public void setProperties(Properties p) {
    152         //数据库方言
    153         String dialect = p.getProperty("dialect");
    154         sqlUtil = new SqlUtil(dialect);
    155         sqlUtil.setProperties(p);
    156     }
    157 }
      1 /*
      2  * The MIT License (MIT)
      3  *
      4  * Copyright (c) 2014 abel533@gmail.com
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a copy
      7  * of this software and associated documentation files (the "Software"), to deal
      8  * in the Software without restriction, including without limitation the rights
      9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  * copies of the Software, and to permit persons to whom the Software is
     11  * furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be included in
     14  * all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     22  * THE SOFTWARE.
     23  */
     24 
     25 package com.github.pagehelper;
     26 
     27 import java.io.Serializable;
     28 import java.util.List;
     29 
     30 /**
     31  * 对Page<E>结果进行包装
     32  * <p/>
     33  * 新增分页的多项属性,主要参考:http://bbs.csdn.net/topics/360010907
     34  *
     35  * @author liuzh/abel533/isea533
     36  * @version 3.3.0
     37  * @since 3.2.2
     38  * 项目地址 : http://git.oschina.net/free/Mybatis_PageHelper
     39  */
     40 @SuppressWarnings({"rawtypes", "unchecked"})
     41 public class PageInfo<T> implements Serializable {
     42     private static final long serialVersionUID = 1L;
     43     //当前页
     44     private int pageNum;
     45     //每页的数量
     46     private int pageSize;
     47     //当前页的数量
     48     private int size;
     49     //由于startRow和endRow不常用,这里说个具体的用法
     50     //可以在页面中"显示startRow到endRow 共size条数据"
     51 
     52     //当前页面第一个元素在数据库中的行号
     53     private int startRow;
     54     //当前页面最后一个元素在数据库中的行号
     55     private int endRow;
     56     //总记录数
     57     private long total;
     58     //总页数
     59     private int pages;
     60     //结果集
     61     private List<T> list;
     62 
     63     //第一页
     64     private int firstPage;
     65     //前一页
     66     private int prePage;
     67     //下一页
     68     private int nextPage;
     69     //最后一页
     70     private int lastPage;
     71 
     72     //是否为第一页
     73     private boolean isFirstPage = false;
     74     //是否为最后一页
     75     private boolean isLastPage = false;
     76     //是否有前一页
     77     private boolean hasPreviousPage = false;
     78     //是否有下一页
     79     private boolean hasNextPage = false;
     80     //导航页码数
     81     private int navigatePages;
     82     //所有导航页号
     83     private int[] navigatepageNums;
     84 
     85     /**
     86      * 包装Page对象
     87      *
     88      * @param list
     89      */
     90     public PageInfo(List<T> list) {
     91         this(list, 8);
     92     }
     93 
     94     /**
     95      * 包装Page对象
     96      *
     97      * @param list          page结果
     98      * @param navigatePages 页码数量
     99      */
    100     public PageInfo(List<T> list, int navigatePages) {
    101         if (list instanceof Page) {
    102             Page page = (Page) list;
    103             this.pageNum = page.getPageNum();
    104             this.pageSize = page.getPageSize();
    105 
    106             this.total = page.getTotal();
    107             this.pages = page.getPages();
    108             this.list = page;
    109             this.size = page.size();
    110             //由于结果是>startRow的,所以实际的需要+1
    111             if (this.size == 0) {
    112                 this.startRow = 0;
    113                 this.endRow = 0;
    114             } else {
    115                 this.startRow = page.getStartRow() + 1;
    116                 //计算实际的endRow(最后一页的时候特殊)
    117                 this.endRow = this.startRow - 1 + this.size;
    118             }
    119             this.navigatePages = navigatePages;
    120             //计算导航页
    121             calcNavigatepageNums();
    122             //计算前后页,第一页,最后一页
    123             calcPage();
    124             //判断页面边界
    125             judgePageBoudary();
    126         }
    127     }
    128 
    129     /**
    130      * 计算导航页
    131      */
    132     private void calcNavigatepageNums() {
    133         //当总页数小于或等于导航页码数时
    134         if (pages <= navigatePages) {
    135             navigatepageNums = new int[pages];
    136             for (int i = 0; i < pages; i++) {
    137                 navigatepageNums[i] = i + 1;
    138             }
    139         } else { //当总页数大于导航页码数时
    140             navigatepageNums = new int[navigatePages];
    141             int startNum = pageNum - navigatePages / 2;
    142             int endNum = pageNum + navigatePages / 2;
    143 
    144             if (startNum < 1) {
    145                 startNum = 1;
    146                 //(最前navigatePages页
    147                 for (int i = 0; i < navigatePages; i++) {
    148                     navigatepageNums[i] = startNum++;
    149                 }
    150             } else if (endNum > pages) {
    151                 endNum = pages;
    152                 //最后navigatePages页
    153                 for (int i = navigatePages - 1; i >= 0; i--) {
    154                     navigatepageNums[i] = endNum--;
    155                 }
    156             } else {
    157                 //所有中间页
    158                 for (int i = 0; i < navigatePages; i++) {
    159                     navigatepageNums[i] = startNum++;
    160                 }
    161             }
    162         }
    163     }
    164 
    165     /**
    166      * 计算前后页,第一页,最后一页
    167      */
    168     private void calcPage() {
    169         if (navigatepageNums != null && navigatepageNums.length > 0) {
    170             firstPage = navigatepageNums[0];
    171             lastPage = navigatepageNums[navigatepageNums.length - 1];
    172             if (pageNum > 1) {
    173                 prePage = pageNum - 1;
    174             }
    175             if (pageNum < pages) {
    176                 nextPage = pageNum + 1;
    177             }
    178         }
    179     }
    180 
    181     /**
    182      * 判定页面边界
    183      */
    184     private void judgePageBoudary() {
    185         isFirstPage = pageNum == 1;
    186         isLastPage = pageNum == pages;
    187         hasPreviousPage = pageNum > 1;
    188         hasNextPage = pageNum < pages;
    189     }
    190 
    191     public void setPageNum(int pageNum) {
    192         this.pageNum = pageNum;
    193     }
    194 
    195     public int getPageNum() {
    196         return pageNum;
    197     }
    198 
    199     public int getPageSize() {
    200         return pageSize;
    201     }
    202 
    203     public int getSize() {
    204         return size;
    205     }
    206 
    207     public int getStartRow() {
    208         return startRow;
    209     }
    210 
    211     public int getEndRow() {
    212         return endRow;
    213     }
    214 
    215     public long getTotal() {
    216         return total;
    217     }
    218 
    219     public int getPages() {
    220         return pages;
    221     }
    222 
    223     public List<T> getList() {
    224         return list;
    225     }
    226 
    227     public int getFirstPage() {
    228         return firstPage;
    229     }
    230 
    231     public int getPrePage() {
    232         return prePage;
    233     }
    234 
    235     public int getNextPage() {
    236         return nextPage;
    237     }
    238 
    239     public int getLastPage() {
    240         return lastPage;
    241     }
    242 
    243     public boolean isIsFirstPage() {
    244         return isFirstPage;
    245     }
    246 
    247     public boolean isIsLastPage() {
    248         return isLastPage;
    249     }
    250 
    251     public boolean isHasPreviousPage() {
    252         return hasPreviousPage;
    253     }
    254 
    255     public boolean isHasNextPage() {
    256         return hasNextPage;
    257     }
    258 
    259     public int getNavigatePages() {
    260         return navigatePages;
    261     }
    262 
    263     public int[] getNavigatepageNums() {
    264         return navigatepageNums;
    265     }
    266 
    267     @Override
    268     public String toString() {
    269         final StringBuffer sb = new StringBuffer("PageInfo{");
    270         sb.append("pageNum=").append(pageNum);
    271         sb.append(", pageSize=").append(pageSize);
    272         sb.append(", size=").append(size);
    273         sb.append(", startRow=").append(startRow);
    274         sb.append(", endRow=").append(endRow);
    275         sb.append(", total=").append(total);
    276         sb.append(", pages=").append(pages);
    277         sb.append(", list=").append(list);
    278         sb.append(", firstPage=").append(firstPage);
    279         sb.append(", prePage=").append(prePage);
    280         sb.append(", nextPage=").append(nextPage);
    281         sb.append(", lastPage=").append(lastPage);
    282         sb.append(", isFirstPage=").append(isFirstPage);
    283         sb.append(", isLastPage=").append(isLastPage);
    284         sb.append(", hasPreviousPage=").append(hasPreviousPage);
    285         sb.append(", hasNextPage=").append(hasNextPage);
    286         sb.append(", navigatePages=").append(navigatePages);
    287         sb.append(", navigatepageNums=");
    288         if (navigatepageNums == null) sb.append("null");
    289         else {
    290             sb.append('[');
    291             for (int i = 0; i < navigatepageNums.length; ++i)
    292                 sb.append(i == 0 ? "" : ", ").append(navigatepageNums[i]);
    293             sb.append(']');
    294         }
    295         sb.append('}');
    296         return sb.toString();
    297     }
    298 }
  • 相关阅读:
    只需 4 步,手把手教你如何实现滤镜功能
    paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制
    socket网络编程的一些基础知识
    图像处理、模式识别、模式分类、机器视觉推荐图书
    数据挖掘、机器学习和模式识别关系与区别
    Qt4_VS10 程序打包发布
    Qt之多线程
    QT中.pro文件的写法
    高斯定理的证明
    《Linear Algebra and Its Applications》-chaper3-行列式-行列式初等变换
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/10038628.html
Copyright © 2011-2022 走看看