Example #1
0
 /**
  * 对象中获取分页参数
  *
  * @param params
  * @return
  */
 public static Page getPageFromObject(Object params) {
   int pageNum;
   int pageSize;
   MetaObject paramsObject = null;
   if (params == null) {
     throw new NullPointerException("分页查询参数params不能为空!");
   }
   if (hasRequest && requestClass.isAssignableFrom(params.getClass())) {
     try {
       paramsObject = SystemMetaObject.forObject(getParameterMap.invoke(params, new Object[] {}));
     } catch (Exception e) {
       // 忽略
     }
   } else {
     paramsObject = SystemMetaObject.forObject(params);
   }
   if (paramsObject == null) {
     throw new NullPointerException("分页查询参数params处理失败!");
   }
   try {
     pageNum = Integer.parseInt(String.valueOf(getParamValue(paramsObject, "pageNum", true)));
     pageSize = Integer.parseInt(String.valueOf(getParamValue(paramsObject, "pageSize", true)));
   } catch (NumberFormatException e) {
     throw new IllegalArgumentException("分页参数不是合法的数字类型!");
   }
   Object _count = getParamValue(paramsObject, "count", false);
   boolean count = true;
   if (_count != null) {
     count = Boolean.valueOf(String.valueOf(_count));
   }
   Page page = new Page(pageNum, pageSize, count);
   Object reasonable = getParamValue(paramsObject, "reasonable", false);
   if (reasonable != null) {
     page.setReasonable(Boolean.valueOf(String.valueOf(reasonable)));
   }
   Object pageSizeZero = getParamValue(paramsObject, "pageSizeZero", false);
   if (pageSizeZero != null) {
     page.setPageSizeZero(Boolean.valueOf(String.valueOf(pageSizeZero)));
   }
   return page;
 }
Example #2
0
 /**
  * 获取分页参数
  *
  * @param params RowBounds参数
  * @return 返回Page对象
  */
 public Page getPage(Object params) {
   Page page = getLocalPage();
   if (page == null) {
     if (params instanceof RowBounds) {
       RowBounds rowBounds = (RowBounds) params;
       if (offsetAsPageNum) {
         page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
       } else {
         page = new Page(rowBounds, rowBoundsWithCount);
         // offsetAsPageNum=false的时候,由于PageNum问题,不能使用reasonable,这里会强制为false
         page.setReasonable(false);
       }
     } else {
       page = getPageFromObject(params);
     }
     setLocalPage(page);
   }
   // 分页合理化
   if (page.getReasonable() == null) {
     page.setReasonable(reasonable);
   }
   // 当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
   if (page.getPageSizeZero() == null) {
     page.setPageSizeZero(pageSizeZero);
   }
   return page;
 }
Example #3
0
  /**
   * Mybatis拦截器方法
   *
   * @param invocation 拦截器入参
   * @return 返回执行结果
   * @throws Throwable 抛出异常
   */
  private Object _processPage(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    if (SqlUtil.getLocalPage() == null && rowBounds == RowBounds.DEFAULT) {
      if (OrderByHelper.getOrderBy() != null) {
        OrderByHelper.processIntercept(invocation);
      }
      return invocation.proceed();
    } else {
      // 获取原始的ms
      MappedStatement ms = (MappedStatement) args[0];
      // 判断并处理为PageSqlSource
      if (!isPageSqlSource(ms)) {
        processMappedStatement(ms, parser);
      }
      // 忽略RowBounds-否则会进行Mybatis自带的内存分页
      args[2] = RowBounds.DEFAULT;
      // 分页信息
      Page page = getPage(rowBounds);
      // pageSizeZero的判断
      if ((page.getPageSizeZero() != null && page.getPageSizeZero()) && page.getPageSize() == 0) {
        COUNT.set(null);
        // 执行正常(不分页)查询
        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()) {
        COUNT.set(Boolean.TRUE);
        // 替换MS
        args[0] = msCountMap.get(ms.getId());
        // 查询总数
        Object result = invocation.proceed();
        // 还原ms
        args[0] = ms;
        // 设置总数
        page.setTotal((Integer) ((List) result).get(0));
        if (page.getTotal() == 0) {
          return page;
        }
      }
      // pageSize>0的时候执行分页查询,pageSize<=0的时候不执行相当于可能只返回了一个count
      if (page.getPageSize() > 0
          && ((rowBounds == RowBounds.DEFAULT && page.getPageNum() > 0)
              || rowBounds != RowBounds.DEFAULT)) {
        // 将参数中的MappedStatement替换为新的qs
        COUNT.set(null);
        BoundSql boundSql = ms.getBoundSql(args[1]);
        args[1] = parser.setPageParameter(ms, args[1], boundSql, page);
        COUNT.set(Boolean.FALSE);
        // 执行分页查询
        Object result = invocation.proceed();
        // 得到处理结果
        page.addAll((List) result);
      }
      // 返回结果
      return page;
    }
  }