Java Code Examples for org.apache.ibatis.session.RowBounds#getOffset()

The following examples show how to use org.apache.ibatis.session.RowBounds#getOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OracleRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    int startRow = rowBounds.getOffset();
    int endRow = rowBounds.getOffset() + rowBounds.getLimit();
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 120);
    if (startRow > 0) {
        sqlBuilder.append("SELECT * FROM ( ");
    }
    if (endRow > 0) {
        sqlBuilder.append(" SELECT TMP_PAGE.*, ROWNUM ROW_ID FROM ( ");
    }
    sqlBuilder.append(sql);
    if (endRow > 0) {
        sqlBuilder.append(" ) TMP_PAGE WHERE ROWNUM <= ");
        sqlBuilder.append(endRow);
        pageKey.update(endRow);
    }
    if (startRow > 0) {
        sqlBuilder.append(" ) WHERE ROW_ID > ");
        sqlBuilder.append(startRow);
        pageKey.update(startRow);
    }
    return sqlBuilder.toString();
}
 
Example 2
Source File: HerdDBRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
    sqlBuilder.append(sql);
    if (rowBounds.getOffset() == 0) {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getLimit());
    } else {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getOffset());
        sqlBuilder.append(",");
        sqlBuilder.append(rowBounds.getLimit());
        pageKey.update(rowBounds.getOffset());
    }
    pageKey.update(rowBounds.getLimit());
    return sqlBuilder.toString();
}
 
Example 3
Source File: HsqldbRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 20);
    sqlBuilder.append(sql);
    if (rowBounds.getLimit() > 0) {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getLimit());
        pageKey.update(rowBounds.getLimit());
    }
    if (rowBounds.getOffset() > 0) {
        sqlBuilder.append(" OFFSET ");
        sqlBuilder.append(rowBounds.getOffset());
        pageKey.update(rowBounds.getOffset());
    }
    return sqlBuilder.toString();
}
 
Example 4
Source File: InformixRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 40);
    sqlBuilder.append("SELECT ");
    if (rowBounds.getOffset() > 0) {
        sqlBuilder.append(" SKIP ");
        sqlBuilder.append(rowBounds.getOffset());
        pageKey.update(rowBounds.getOffset());
    }
    if (rowBounds.getLimit() > 0) {
        sqlBuilder.append(" FIRST ");
        sqlBuilder.append(rowBounds.getLimit());
        pageKey.update(rowBounds.getLimit());
    }
    sqlBuilder.append(" * FROM ( ");
    sqlBuilder.append(sql);
    sqlBuilder.append(" ) TEMP_T");
    return sqlBuilder.toString();
}
 
Example 5
Source File: MySqlRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 6 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 14);
    sqlBuilder.append(sql);
    if (rowBounds.getOffset() == 0) {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getLimit());
    } else {
        sqlBuilder.append(" LIMIT ");
        sqlBuilder.append(rowBounds.getOffset());
        sqlBuilder.append(",");
        sqlBuilder.append(rowBounds.getLimit());
        pageKey.update(rowBounds.getOffset());
    }
    pageKey.update(rowBounds.getLimit());
    return sqlBuilder.toString();
}
 
Example 6
Source File: SqlUtil.java    From genericdao with Artistic License 2.0 6 votes vote down vote up
/**
 * 获取分页参数
 *
 * @param params RowBounds参数
 * @return 返回Page对象
 */
public PageHelper getPage(Object params) {
    PageHelper page = getLocalPage();
    if (page == null) {
        if (params instanceof RowBounds) {
            RowBounds rowBounds = (RowBounds) params;
            if (offsetAsPageNum) {
                page = new PageHelper(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
            } else {
                page = new PageHelper(rowBounds, rowBoundsWithCount);
            }
        } 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 7
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
  if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
    if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {
      rs.absolute(rowBounds.getOffset());
    }
  } else {
    for (int i = 0; i < rowBounds.getOffset(); i++) {
      rs.next();
    }
  }
}
 
Example 8
Source File: Db2RowBoundsDialect.java    From Mybatis-PageHelper with MIT License 5 votes vote down vote up
@Override
public String getPageSql(String sql, RowBounds rowBounds, CacheKey pageKey) {
    int startRow = rowBounds.getOffset() + 1;
    int endRow = rowBounds.getOffset() + rowBounds.getLimit();
    StringBuilder sqlBuilder = new StringBuilder(sql.length() + 120);
    sqlBuilder.append("SELECT * FROM (SELECT TMP_PAGE.*,ROWNUMBER() OVER() AS ROW_ID FROM ( ");
    sqlBuilder.append(sql);
    sqlBuilder.append(" ) AS TMP_PAGE) TMP_PAGE WHERE ROW_ID BETWEEN ");
    sqlBuilder.append(startRow);
    sqlBuilder.append(" AND ");
    sqlBuilder.append(endRow);
    pageKey.update(startRow);
    pageKey.update(endRow);
    return sqlBuilder.toString();
}
 
Example 9
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
  if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {
    if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {
      rs.absolute(rowBounds.getOffset());
    }
  } else {
    for (int i = 0; i < rowBounds.getOffset(); i++) {
      rs.next();
    }
  }
}
 
Example 10
Source File: PartialListResultHandler.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <E> List<E> createPaginatedResult(
        String statement,
        Object parameter,
        RowBounds rowBounds,
        Integer total,
        List<E> result) {
    int offset = rowBounds.getOffset();
    int limit = rowBounds.getLimit();
    return create(offset, limit, total, result, rowBoundsOrigin);
}
 
Example 11
Source File: PageHelper.java    From genericdao with Artistic License 2.0 5 votes vote down vote up
public PageHelper(RowBounds rowBounds, int total) {
    super(rowBounds.getLimit() > -1 ? rowBounds.getLimit() : 0);
    this.pageSize = rowBounds.getLimit();
    this.startRow = rowBounds.getOffset();
    //RowBounds方式默认不求count总数,如果想求count,可以修改这里为SQL_COUNT
    this.total = total;
    this.endRow = this.startRow + this.pageSize;
}
 
Example 12
Source File: PageInterceptor.java    From Zebra with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
	Object[] args = invocation.getArgs();
	Object rowBound = args[2];

	MappedStatement ms = (MappedStatement) args[0];
	if (rowBound != null) {
		RowBounds rb = (RowBounds) rowBound;

		// without pagination
		if (rb.getOffset() == RowBounds.NO_ROW_OFFSET && rb.getLimit() == RowBounds.NO_ROW_LIMIT) {
			return invocation.proceed();
		} else {
			BoundSql boundSql = ms.getBoundSql(args[1]);

			if (rowBound instanceof PageModel) {
				// physical pagination with PageModel
				PageModel pageModel = (PageModel) rowBound;
				Object count = queryCount(invocation, args, ms, boundSql);
				Object records = queryLimit(invocation, args, ms, boundSql, pageModel);

				pageModel.setRecordCount((Integer) ((List<?>) count).get(0));
				pageModel.setRecords((List<?>) records);

				return null;
			} else {
				// physical pagination with RowBounds
				return queryLimit(invocation, args, ms, boundSql, rb);
			}
		}
	} else {
		// without pagination
		return invocation.proceed();
	}
}
 
Example 13
Source File: PageBounds.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
public PageBounds(RowBounds rowBounds) {
    if(rowBounds instanceof PageBounds){
        PageBounds pageBounds = (PageBounds)rowBounds;
        this.page = pageBounds.page;
        this.limit = pageBounds.limit;
        this.orders = pageBounds.orders;
        this.containsTotalCount = pageBounds.containsTotalCount;
        this.asyncTotalCount = pageBounds.asyncTotalCount;
    }else{
        this.page = (rowBounds.getOffset()/rowBounds.getLimit())+1;
        this.limit = rowBounds.getLimit();
    }

}
 
Example 14
Source File: PagePluging.java    From aaden-pay with Apache License 2.0 5 votes vote down vote up
void processIntercept(final Object[] queryArgs) {
	MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
	Object parameter = queryArgs[PARAMETER_INDEX];
	final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
	int offset = rowBounds.getOffset();
	int limit = rowBounds.getLimit();
	if (dialect.supportsLimit() && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) {
		BoundSql boundSql = ms.getBoundSql(parameter);
		String sql = boundSql.getSql().trim();
		if (dialect.supportsLimitOffset()) {
			sql = dialect.getLimitString(sql, offset, limit);
			offset = RowBounds.NO_ROW_OFFSET;
		} else {
			sql = dialect.getLimitString(sql, 0, limit);
		}
		limit = RowBounds.NO_ROW_LIMIT;
		queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);
		BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(),
				boundSql.getParameterObject());
		for (ParameterMapping mapping : boundSql.getParameterMappings()) {
			String prop = mapping.getProperty();
			if (boundSql.hasAdditionalParameter(prop)) {
				newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
			}
		}
		MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
		queryArgs[MAPPED_STATEMENT_INDEX] = newMs;
	}
}
 
Example 15
Source File: MybatisUtils.java    From sqlhelper with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static boolean isPagingRowBounds(RowBounds rowBounds) {
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        return false;
    }
    return rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET || rowBounds.getLimit() != RowBounds.NO_ROW_LIMIT;
}
 
Example 16
Source File: SqlUtils.java    From mybatis-boost with MIT License 4 votes vote down vote up
public static String appendLimitOffset(String sql, RowBounds rowBounds) {
    if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET || rowBounds.getLimit() != RowBounds.NO_ROW_LIMIT) {
        sql += " LIMIT " + rowBounds.getLimit() + " OFFSET " + rowBounds.getOffset();
    }
    return sql;
}
 
Example 17
Source File: SqlUtils.java    From mybatis-boost with MIT License 4 votes vote down vote up
public static String appendLimit(String sql, RowBounds rowBounds) {
    if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET || rowBounds.getLimit() != RowBounds.NO_ROW_LIMIT) {
        sql += " LIMIT " + rowBounds.getOffset() + ", " + rowBounds.getLimit();
    }
    return sql;
}
 
Example 18
Source File: MysqlDialect.java    From Aooms with Apache License 2.0 4 votes vote down vote up
@Override
public String pagingQuery(String originalSql, RowBounds rowBounds) {
    int start = (rowBounds.getOffset() - 1) * rowBounds.getLimit();
    String countsql = "select * from (" + originalSql + ") _table limit " + start + "," + rowBounds.getLimit();
    return  countsql;
}