Java Code Examples for org.apache.ibatis.session.RowBounds#DEFAULT

The following examples show how to use org.apache.ibatis.session.RowBounds#DEFAULT . 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: PaginationInterceptor.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
    BoundSql boundSql = statementHandler.getBoundSql();
    if (log.isDebugEnabled()) {
        log.debug("raw SQL : " + boundSql.getSql());
    }
    if (boundSql.getSql() == null || boundSql.getSql().isEmpty() || boundSql.getSql().contains(" limit ")) {
        return invocation.proceed();
    }
    MetaObject metaStatementHandler = null;
    RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    }
    String originalSql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");
    metaStatementHandler.setValue("delegate.boundSql.sql",
            getLimitString(originalSql, rowBounds.getOffset(), rowBounds.getLimit()));
    metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
    metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    if (log.isDebugEnabled()) {
        log.debug("pagination SQL : " + boundSql.getSql());
    }
    return invocation.proceed();
}
 
Example 2
Source File: MySqlPagingPlugin.java    From mybatis-test with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] args = invocation.getArgs();
    RowBounds rb = (RowBounds) args[ROW_BOUNDS_INDEX];
    if (rb == RowBounds.DEFAULT) {
        return invocation.proceed();
    }
    args[ROW_BOUNDS_INDEX] = RowBounds.DEFAULT;

    MappedStatement ms = (MappedStatement) args[MAPPED_STATEMENT_INDEX];
    BoundSql boundSql = ms.getBoundSql(args[PARAMETER_INDEX]);

    System.out.println();
    String sql = boundSql.getSql();
    String limit = String.format("LIMIT %d,%d", rb.getOffset(), rb.getLimit());
    sql = sql + " " + limit;

    SqlSource sqlSource = new StaticSqlSource(ms.getConfiguration(), sql, boundSql.getParameterMappings());

    Field field = MappedStatement.class.getDeclaredField("sqlSource");
    field.setAccessible(true);
    field.set(ms, sqlSource);

    return invocation.proceed();
}
 
Example 3
Source File: PageInterceptor.java    From QuickProject with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {
	StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
	BoundSql boundSql = statementHandler.getBoundSql();
	MetaObject metaStatementHandler = MetaObject.forObject(statementHandler, new DefaultObjectFactory(), new DefaultObjectWrapperFactory());
	RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
	if ((rowBounds != null) && (rowBounds != RowBounds.DEFAULT)) {
           Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration");
           Dialect dialect = DialectParser.parse(configuration);
           String sql = (String) metaStatementHandler.getValue("delegate.boundSql.sql");
           sql = dialect.addLimitString(sql, rowBounds.getOffset(), rowBounds.getLimit());

           metaStatementHandler.setValue("delegate.boundSql.sql", sql);
           metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
           metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
	}

	log.debug("SQL : " + boundSql.getSql());
	return invocation.proceed();
}
 
Example 4
Source File: MapperInstrument.java    From mybatis-boost with MIT License 5 votes vote down vote up
private static void addRowBoundsParameter(CtMethod ctMethod, RowBounds rowBounds) {
    if (rowBounds == RowBounds.DEFAULT) return;
    try {
        String newMethodName = ctMethod.getName() + "$" +
                UUID.randomUUID().toString().replace("-", "");
        CtMethod ctNewMethod = CtNewMethod.copy(ctMethod, newMethodName, ctMethod.getDeclaringClass(), null);
        ctNewMethod.addParameter(ClassPool.getDefault().get("org.apache.ibatis.session.RowBounds"));
        ctNewMethod.getMethodInfo().addAttribute
                (ctMethod.getMethodInfo().removeAttribute(AnnotationsAttribute.visibleTag));

        String body;
        if (rowBounds.getLimit() == 1) {
            MethodInfo methodInfo = ctNewMethod.getMethodInfo();
            String descriptor = methodInfo.getDescriptor();
            String returnType = descriptor.substring(descriptor.lastIndexOf(')') + 1);
            methodInfo.setDescriptor(descriptor =
                    descriptor.substring(0, descriptor.length() - returnType.length()) + "Ljava/util/List;");
            ctNewMethod.setGenericSignature
                    (descriptor.substring(0, descriptor.length() - 1) + "<" + returnType + ">;");

            body = "{ java.util.List list = %s($$, new org.apache.ibatis.session.RowBounds(%s, %s));" +
                    "return !list.isEmpty() ? (%s) list.get(0) : null; }";
            body = String.format(body, newMethodName, rowBounds.getOffset(), rowBounds.getLimit(),
                    returnType.substring(1, returnType.length() - 1).replace('/', '.'));
        } else {
            String signature = ctMethod.getGenericSignature();
            int index = signature.lastIndexOf(')');
            ctNewMethod.setGenericSignature(signature.substring(0, index) +
                    "Lorg/apache/ibatis/session/RowBounds;" + signature.substring(index));
            body = String.format("{ return %s($$, new org.apache.ibatis.session.RowBounds(%s, %s)); }",
                    newMethodName, rowBounds.getOffset(), rowBounds.getLimit());
        }
        ctMethod.getDeclaringClass().addMethod(ctNewMethod);
        ctMethod.setBody(body);
    } catch (CannotCompileException | NotFoundException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: PaginationInterceptor.java    From Mario with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Invocation invocation) throws Throwable {

    StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
    BoundSql boundSql = statementHandler.getBoundSql();

    String sql = boundSql.getSql();
    if (StringUtils.isBlank(sql)) {
        return invocation.proceed();
    }

    //select sql do 
    if (sql.matches(SQL_SELECT_REGEX) && !Pattern.matches(SQL_COUNT_REGEX, sql)) {
        Object obj = FieldUtils.readField(statementHandler, "delegate", true);
        // 反射获取 RowBounds 对象。
        RowBounds rowBounds = (RowBounds) FieldUtils.readField(obj, "rowBounds", true);

        // 分页参数存在且不为默认值时进行分页SQL构造
        if (rowBounds != null && rowBounds != RowBounds.DEFAULT) {
            FieldUtils.writeField(boundSql, "sql", newSql(sql, rowBounds), true);

            // 一定要还原否则将无法得到下一组数据(第一次的数据被缓存了)
            FieldUtils.writeField(rowBounds, "offset", RowBounds.NO_ROW_OFFSET, true);
            FieldUtils.writeField(rowBounds, "limit", RowBounds.NO_ROW_LIMIT, true);
        }
    }

    return invocation.proceed();
}
 
Example 6
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 7
Source File: MethodNameParser.java    From mybatis-boost with MIT License 4 votes vote down vote up
public RowBounds toRowBounds() {
    if (parsedSql == null) toSql();
    return offset > 0 || limit > 0 ? new RowBounds(offset, limit) : RowBounds.DEFAULT;
}
 
Example 8
Source File: MybatisOrmImpl.java    From tephra with MIT License 4 votes vote down vote up
private RowBounds getRowBounds(MybatisBuilder builder) {
    return builder.getRowBounds() == null ? RowBounds.DEFAULT : builder.getRowBounds();
}
 
Example 9
Source File: AbstractRowBoundsDialect.java    From Mybatis-PageHelper with MIT License 4 votes vote down vote up
@Override
public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
    return rowBounds == RowBounds.DEFAULT;
}
 
Example 10
Source File: PaginationInterceptor.java    From platform with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object intercept(Invocation invocation) throws Throwable {
    Executor executor = MyBatisUtils.getRealTarget(invocation.getTarget());
    Object[] args = invocation.getArgs();
    MappedStatement mappedStatement = (MappedStatement) args[MAPPED_STATEMENT_INDEX];

    // 查询操作或者存储过程,无需分页
    if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType()
            || StatementType.CALLABLE == mappedStatement.getStatementType()) {
        return invocation.proceed();
    }

    Object parameterObject = invocation.getArgs()[PARAMETER_INDEX];

    // 不包含PageableRequest或者Pageable参数时,无需分页
    PageableRequest<?> pageableRequest = MyBatisUtils.findPageableRequest(parameterObject).orElse(null);
    if (null == pageableRequest || pageableRequest.getPageable() == null ||
            pageableRequest.getPageable().getPageSize() < 0) {
        return invocation.proceed();
    }

    Connection connection = executor.getTransaction().getConnection();

    DbType dbType = this.dbType == null ? JdbcUtils.getDbType(connection) : this.dbType;
    DbDialect dialect = Optional.ofNullable(this.dbDialect).orElseGet(() -> JdbcUtils.getDialect(dbType));

    // 针对定义了rowBounds,做为mapper接口方法的参数
    BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
    String originalSql = boundSql.getSql();

    // 查询总记录数
    long total = 0;
    if (pageableRequest.isQueryTotalCount()) {
        String countSql = dialect.buildCountSql(boundSql.getSql());

        total = this.queryTotal(countSql, mappedStatement, boundSql, connection);

        if (total <= 0) {
            return null;
        }
    }

    Pageable pageable = pageableRequest.getPageable();
    String buildSql = concatOrderBy(originalSql, pageable);
    String paginationSql = dialect.buildPaginationSql(buildSql, pageable.getOffset(), pageable.getPageSize());

    BoundSql newBs = MyBatisUtils.copyFromBoundSql(mappedStatement, boundSql, paginationSql, parameterObject);
    MappedStatement newMs = MyBatisUtils.copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBs));
    args[MAPPED_STATEMENT_INDEX] = newMs;
    args[ROWBOUNDS_INDEX] = RowBounds.DEFAULT;

    Object result = invocation.proceed();
    pageableRequest.setTotal(total);
    pageableRequest.setRecords((List) result);
    return result;
}