com.dingding.utils.page.PageHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.dingding.utils.page.PageHelper.java

Source

/*
   The MIT License (MIT)
    
   Copyright (c) 2014 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.dingding.utils.page;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.List;
import java.util.Properties;

/**
 * Mybatis - 
 *
 * @author liuzh/abel533/isea533
 * @version 3.3.0
 *          ? : http://git.oschina.net/free/Mybatis_PageHelper
 */
@Intercepts(@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
        RowBounds.class, ResultHandler.class }))
public class PageHelper implements Interceptor {
    private static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();
    //sql
    private static SqlUtil SQLUTIL;
    //RowBounds?offsetPageNum - ?
    private static boolean offsetAsPageNum = false;
    //RowBounds?count - ?
    private static boolean rowBoundsWithCount = false;
    //truepagesize0RowBoundslimit=0?
    private static boolean pageSizeZero = false;
    //??true?????false??
    private static boolean reasonable = false;

    /**
     * ?
     *
     * @param dialect
     */
    public static void setDialect(String dialect) {
        SQLUTIL = new SqlUtil(dialect);
    }

    /**
     * RowBounds?offsetPageNum - ?
     *
     * @param offsetAsPageNum
     */
    public static void setOffsetAsPageNum(String offsetAsPageNum) {
        PageHelper.offsetAsPageNum = Boolean.parseBoolean(offsetAsPageNum);
    }

    /**
     * RowBounds?count - ?
     *
     * @param rowBoundsWithCount
     */
    public static void setRowBoundsWithCount(String rowBoundsWithCount) {
        PageHelper.rowBoundsWithCount = Boolean.parseBoolean(rowBoundsWithCount);
    }

    /**
     * truepagesize0RowBoundslimit=0?
     *
     * @param pageSizeZero
     */
    public static void setPageSizeZero(String pageSizeZero) {
        PageHelper.pageSizeZero = Boolean.parseBoolean(pageSizeZero);
    }

    /**
     * ??
     *
     * @return
     */
    public static boolean isReasonable() {
        return reasonable;
    }

    /**
     * ??true?????false??
     *
     * @param reasonable
     */
    public static void setReasonable(String reasonable) {
        PageHelper.reasonable = Boolean.parseBoolean(reasonable);
    }

    /**
     * 
     *
     * @param pageNum  ?
     * @param pageSize ??
     */
    public static void startPage(int pageNum, int pageSize) {
        startPage(pageNum, pageSize, true);
    }

    /**
     * 
     *
     * @param pageNum  ?
     * @param pageSize ??
     * @param count    ?count
     */
    public static void startPage(int pageNum, int pageSize, boolean count) {
        LOCAL_PAGE.set(new Page(pageNum, pageSize, count));
    }

    /**
     * ??
     *
     * @param rowBounds RowBounds?
     * @return Page
     */
    private Page getPage(RowBounds rowBounds) {
        Page page = LOCAL_PAGE.get();
        //??
        LOCAL_PAGE.remove();

        if (page == null) {
            if (offsetAsPageNum) {
                page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
            } else {
                page = new Page(rowBounds, rowBoundsWithCount);
            }
        }
        return page;
    }

    /**
     * Mybatis
     *
     * @param invocation ?
     * @return 
     * @throws Throwable 
     */
    //@Override
    public Object intercept(Invocation invocation) throws Throwable {
        final Object[] args = invocation.getArgs();
        RowBounds rowBounds = (RowBounds) args[2];
        if (LOCAL_PAGE.get() == null && rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        } else {
            //RowBounds-?Mybatis
            args[2] = RowBounds.DEFAULT;
            MappedStatement ms = (MappedStatement) args[0];
            Object parameterObject = args[1];
            //?
            Page page = getPage(rowBounds);
            //pageSizeZero
            if (pageSizeZero && page.getPageSize() == 0) {
                //?
                Object result = invocation.proceed();
                //?
                page.addAll((List) result);
                //
                page.setPageNum(1);
                //?pageSize=total
                page.setPageSize(page.size());
                //??total
                page.setTotal(page.size());
                //?Page - ???
                return page;
            }
            //?total??count
            if (page.isCount()) {
                BoundSql boundSql = ms.getBoundSql(parameterObject);
                //?MappedStatement?qs
                args[0] = SQLUTIL.getCountMappedStatement(ms, boundSql);
                //
                Object result = invocation.proceed();
                //
                page.setTotal((Integer) ((List) result).get(0));
                if (page.getTotal() == 0) {
                    return page;
                }
            }
            //pageSize>0pageSize<=0???count
            if (page.getPageSize() > 0 && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
                    || rowBounds != RowBounds.DEFAULT)) {
                BoundSql boundSql = ms.getBoundSql(parameterObject);
                //?MappedStatement?qs
                args[0] = SQLUTIL.getPageMappedStatement(ms, boundSql);
                //parameterObject?
                args[1] = SQLUTIL.setPageParameter(parameterObject, boundSql, page);
                //
                Object result = invocation.proceed();
                //?
                page.addAll((List) result);
            }
            //
            return page;
        }
    }

    /**
     * ?Executor
     *
     * @param target
     * @return
     */
    //@Override
    public Object plugin(Object target) {
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        } else {
            return target;
        }
    }

    /**
     * 
     *
     * @param p 
     */
    public void setProperties(Properties p) {
        //?
        String dialect = p.getProperty("dialect");
        setDialect(dialect);
        //offsetPageNum
        String offsetAsPageNum = p.getProperty("offsetAsPageNum");
        setOffsetAsPageNum(offsetAsPageNum);
        //RowBounds???count
        String rowBoundsWithCount = p.getProperty("rowBoundsWithCount");
        setRowBoundsWithCount(rowBoundsWithCount);
        //truepagesize0RowBoundslimit=0?
        String pageSizeZero = p.getProperty("pageSizeZero");
        setPageSizeZero(pageSizeZero);
        //??true?????false??
        String reasonable = p.getProperty("reasonable");
        setReasonable(reasonable);
    }
}