Example usage for org.apache.ibatis.plugin Invocation getArgs

List of usage examples for org.apache.ibatis.plugin Invocation getArgs

Introduction

In this page you can find the example usage for org.apache.ibatis.plugin Invocation getArgs.

Prototype

public Object[] getArgs() 

Source Link

Usage

From source file:cn.myblog.uitl.MybatisPageHelper.PageHelper.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    RowBounds rowBounds = (RowBounds) args[2];
    if (localPage.get() == null && rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    } else {/* www  .  j  a  v a  2s .c om*/
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        MappedStatement ms = (MappedStatement) args[0];
        Object parameterObject = args[1];
        BoundSql boundSql = ms.getBoundSql(parameterObject);

        //?
        Page page = localPage.get();
        //??
        localPage.remove();

        if (page == null) {
            if (offsetAsPageNum) {
                page = new Page(rowBounds.getOffset(), rowBounds.getLimit(),
                        rowBoundsWithCount ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
            } else {
                page = new Page(rowBounds, rowBoundsWithCount ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
            }
        }
        MappedStatement qs = newMappedStatement(ms, new BoundSqlSqlSource(boundSql));
        //?MappedStatement?qs?
        args[0] = qs;
        MetaObject msObject = SystemMetaObject.forObject(qs);
        String sql = (String) msObject.getValue("sqlSource.boundSql.sql");
        //?total??count
        if (page.getTotal() > Page.NO_SQL_COUNT) {
            //count - ?sql
            msObject.setValue("sqlSource.boundSql.sql", getCountSql(sql));
            //
            Object result = invocation.proceed();
            int totalCount = (Integer) ((List) result).get(0);
            page.setTotal(totalCount);
            int totalPage = totalCount / page.getPageSize() + ((totalCount % page.getPageSize() == 0) ? 0 : 1);
            page.setPages(totalPage);
            //sql - ?sql
            msObject.setValue("sqlSource.boundSql.sql", getPageSql(sql, page));
            //??
            msObject.setValue("resultMaps", ms.getResultMaps());
            //
            result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            return page;
        } else {
            //sql - ?sql
            msObject.setValue("sqlSource.boundSql.sql", getPageSql(sql, page));
            //??
            msObject.setValue("resultMaps", ms.getResultMaps());
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
            //
            return page;
        }
    }
}

From source file:cn.org.awcp.core.mybatis.mapper.MapperInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    Object[] objects = invocation.getArgs();
    MappedStatement ms = (MappedStatement) objects[0];
    String msId = ms.getId();//from   w w w.j  a v a  2s.  co  m
    //??
    if (mapperHelper.isMapperMethod(msId)) {
        //????ProviderSqlSource??????
        if (ms.getSqlSource() instanceof ProviderSqlSource) {
            mapperHelper.setSqlSource(ms);
        }
    }
    Object result = invocation.proceed();
    return result;
}

From source file:cn.org.awcp.core.mybatis.page.PageHelper.java

License:Open Source License

/**
 * Mybatis//from w  ww .j a v a  2 s.co  m
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
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 = null;
            //???sql??boundSql
            if (!SQLUTIL.isDynamic(ms)) {
                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 = null;
            //???sql??boundSql
            if (!SQLUTIL.isDynamic(ms)) {
                boundSql = ms.getBoundSql(parameterObject);
            }
            //?MappedStatement?qs
            args[0] = SQLUTIL.getPageMappedStatement(ms, boundSql);
            //?sqlboundSqlms?
            if (boundSql == null) {
                boundSql = ((MappedStatement) args[0]).getBoundSql(parameterObject);
            }
            //parameterObject?
            args[1] = SQLUTIL.setPageParameter(ms, parameterObject, boundSql, page);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
        //
        return page;
    }
}

From source file:com.appleframework.orm.mybatis.pagehelper.PageHelper.java

License:Open Source License

/**
 * ?datasourcesqlUtil/*from   w  ww .  java 2s . com*/
 *
 * @param invocation
 */
public SqlUtil getSqlUtil(Invocation invocation) {
    MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
    //dataSource?
    DataSource dataSource = ms.getConfiguration().getEnvironment().getDataSource();
    String url = getUrl(dataSource);
    if (urlSqlUtilMap.containsKey(url)) {
        return urlSqlUtilMap.get(url);
    }
    try {
        lock.lock();
        if (urlSqlUtilMap.containsKey(url)) {
            return urlSqlUtilMap.get(url);
        }
        if (StringUtil.isEmpty(url)) {
            throw new RuntimeException("?jdbcUrl??dialect?!");
        }
        String dialect = Dialect.fromJdbcUrl(url);
        if (dialect == null) {
            throw new RuntimeException("??dialect?!");
        }
        SqlUtil sqlUtil = new SqlUtil(dialect);
        if (this.properties != null) {
            sqlUtil.setProperties(properties);
        } else if (this.sqlUtilConfig != null) {
            sqlUtil.setSqlUtilConfig(this.sqlUtilConfig);
        }
        urlSqlUtilMap.put(url, sqlUtil);
        return sqlUtil;
    } finally {
        lock.unlock();
    }
}

From source file:com.appleframework.orm.mybatis.pagehelper.SqlUtil.java

License:Open Source License

/**
 * Mybatis// w  w w  .j  a  va 2  s . co m
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
private Object _processPage(Invocation invocation) throws Throwable {
    final Object[] args = invocation.getArgs();
    Page page = null;
    //????Page
    if (supportMethodsArguments) {
        page = getPage(args);
    }
    //?
    RowBounds rowBounds = (RowBounds) args[2];
    //??page == null???
    if ((supportMethodsArguments && page == null)
            //???LocalPageRowBounds??
            || (!supportMethodsArguments && SqlUtil.getLocalPage() == null && rowBounds == RowBounds.DEFAULT)) {
        return invocation.proceed();
    } else {
        //???page==null??
        if (!supportMethodsArguments && page == null) {
            page = getPage(args);
        }
        return doProcessPage(invocation, page, args);
    }
}

From source file:com.autonavi.tsp.workbackend.util.page.PageHelper.java

License:Open Source License

/**
 * Mybatis//from   w ww  . java2 s.co  m
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
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 {
        //?ms
        MappedStatement ms = (MappedStatement) args[0];
        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        //?
        Page page = getPage(rowBounds);
        //pageSizeZero
        if ((page.getPageSizeZero() != null && page.getPageSizeZero()) && 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;
        }
        SqlSource sqlSource = ((MappedStatement) args[0]).getSqlSource();
        //?total??count
        if (page.isCount()) {
            //?MappedStatement?qs
            SQLUTIL.processCountMappedStatement(ms, sqlSource, args);
            //
            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)) {
            //?MappedStatement?qs
            SQLUTIL.processPageMappedStatement(ms, sqlSource, page, args);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
        //
        return page;
    }
}

From source file:com.badminton.interceptors.mySqlHelper.pagehelper.util.SqlUtil.java

License:Open Source License

/**
 * /*from www.ja va2 s  .  c om*/
 *
 * @param invocation
 * @return
 * @throws Throwable
 */
public Object doIntercept(Invocation invocation) throws Throwable {
    //??
    Object[] args = invocation.getArgs();
    MappedStatement ms = (MappedStatement) args[0];
    Object parameterObject = args[1];
    RowBounds rowBounds = (RowBounds) args[2];
    List resultList;
    if (autoDialect) {
        lock.lock();
        try {
            if (autoDialect) {
                autoDialect = false;
                this.dialect = getDialect(ms);
            }
        } finally {
            lock.unlock();
        }
    }
    Dialect runtimeDialect = dialect;
    if (autoRuntimeDialect) {
        runtimeDialect = getDialect(ms);
    }
    //????
    if (!runtimeDialect.skip(ms, parameterObject, rowBounds)) {
        ResultHandler resultHandler = (ResultHandler) args[3];
        //?
        Executor executor = (Executor) invocation.getTarget();
        BoundSql boundSql = ms.getBoundSql(parameterObject);
        //?????
        Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField
                .get(boundSql);
        //?? count 
        if (runtimeDialect.beforeCount(ms, parameterObject, rowBounds)) {
            // count  key
            CacheKey countKey = executor.createCacheKey(ms, parameterObject, RowBounds.DEFAULT, boundSql);
            countKey.update("_Count");
            MappedStatement countMs = msCountMap.get(countKey);
            if (countMs == null) {
                //?? ms  Long  ms
                countMs = MSUtils.newCountMappedStatement(ms);
                msCountMap.put(countKey, countMs);
            }
            //? count sql
            String countSql = runtimeDialect.getCountSql(ms, boundSql, parameterObject, rowBounds, countKey);
            BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql,
                    boundSql.getParameterMappings(), parameterObject);
            //? SQL ???? BoundSql 
            for (String key : additionalParameters.keySet()) {
                countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
            }
            // count 
            Object countResultList = executor.query(countMs, parameterObject, RowBounds.DEFAULT, resultHandler,
                    countKey, countBoundSql);
            Long count = (Long) ((List) countResultList).get(0);
            //?
            runtimeDialect.afterCount(count, parameterObject, rowBounds);
            if (count == 0L) {
                // 0 
                return runtimeDialect.afterPage(new ArrayList(), parameterObject, rowBounds);
            }
        }
        //??
        if (runtimeDialect.beforePage(ms, parameterObject, rowBounds)) {
            //? key
            CacheKey pageKey = executor.createCacheKey(ms, parameterObject, rowBounds, boundSql);
            //??
            parameterObject = runtimeDialect.processParameterObject(ms, parameterObject, boundSql, pageKey);
            //? sql
            String pageSql = runtimeDialect.getPageSql(ms, boundSql, parameterObject, rowBounds, pageKey);
            BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql,
                    boundSql.getParameterMappings(), parameterObject);
            //??
            for (String key : additionalParameters.keySet()) {
                pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
            }
            //
            resultList = executor.query(ms, parameterObject, RowBounds.DEFAULT, resultHandler, pageKey,
                    pageBoundSql);
        } else {
            resultList = new ArrayList();
        }
    } else {
        args[2] = RowBounds.DEFAULT;
        resultList = (List) invocation.proceed();
    }
    //
    return runtimeDialect.afterPage(resultList, parameterObject, rowBounds);
}

From source file:com.baidu.dpop.ctp.common.mybatis.page.PageHelper.java

License:Open Source License

/**
 * Mybatis/* w  w  w  .j a  v  a 2  s  .c  o  m*/
 *
 * @param invocation ?
 * @return 
 * @throws Throwable 
 */
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 {

        //?ms
        MappedStatement ms = (MappedStatement) args[0];

        //RowBounds-?Mybatis
        args[2] = RowBounds.DEFAULT;
        //?
        Page page = getPage(rowBounds);
        //pageSizeZero
        if ((page.getPageSizeZero() != null && page.getPageSizeZero()) && 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;
        }
        SqlSource sqlSource = ((MappedStatement) args[0]).getSqlSource();
        //?total??count
        if (page.isCount()) {
            //?MappedStatement?qs
            SQLUTIL.processCountMappedStatement(ms, sqlSource, args);
            //
            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)) {
            //?MappedStatement?qs
            SQLUTIL.processPageMappedStatement(ms, sqlSource, page, args);
            //
            Object result = invocation.proceed();
            //?
            page.addAll((List) result);
        }
        //
        return page;
    }
}

From source file:com.baomidou.mybatisplus.plugins.CachePaginationInterceptor.java

License:Apache License

/**
 * Physical Pagination Interceptor for all the queries with parameter
 * {@link org.apache.ibatis.session.RowBounds}
 *///from ww  w .jav  a2s.  c o  m
public Object intercept(Invocation invocation) throws Throwable {

    Object target = invocation.getTarget();
    if (target instanceof StatementHandler) {
        StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
        RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");

        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }
        BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
        String originalSql = boundSql.getSql();

        if (rowBounds instanceof Pagination) {
            Pagination page = (Pagination) rowBounds;
            boolean orderBy = true;
            if (page.isSearchCount()) {
                CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                        page.isOptimizeCount());
                orderBy = countOptimize.isOrderBy();
            }
            String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
            originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
        } else {
            // support physical Pagination for RowBounds
            originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
        }

        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
        metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
        metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    } else {
        RowBounds rowBounds = (RowBounds) invocation.getArgs()[2];
        if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
            return invocation.proceed();
        }
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        Executor executor = (Executor) invocation.getTarget();
        Connection connection = executor.getTransaction().getConnection();
        Object parameterObject = invocation.getArgs()[1];
        BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
        String originalSql = boundSql.getSql();
        if (rowBounds instanceof Pagination) {
            Pagination page = (Pagination) rowBounds;
            if (page.isSearchCount()) {
                CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                        page.isOptimizeCount());
                super.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection);
                if (page.getTotal() <= 0) {
                    return invocation.proceed();
                }
            }
        }
    }
    return invocation.proceed();
}

From source file:com.baomidou.mybatisplus.plugins.PaginationInterceptor.java

License:Apache License

/**
 * Physical Pagination Interceptor for all the queries with parameter {@link org.apache.ibatis.session.RowBounds}
 *///from  ww  w.j  av  a  2 s  .c  o  m
public Object intercept(Invocation invocation) throws Throwable {
    StatementHandler statementHandler = (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
    MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
    // ?SELECT?
    MappedStatement mappedStatement = (MappedStatement) metaStatementHandler
            .getValue("delegate.mappedStatement");
    if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
        return invocation.proceed();
    }
    RowBounds rowBounds = (RowBounds) metaStatementHandler.getValue("delegate.rowBounds");
    /* ??? */
    if (rowBounds == null || rowBounds == RowBounds.DEFAULT) {
        return invocation.proceed();
    }
    BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
    String originalSql = boundSql.getSql();
    Connection connection = (Connection) invocation.getArgs()[0];
    if (isDynamicDataSource()) {
        dialectType = JdbcUtils.getDbType(connection.getMetaData().getURL()).getDb();
    }
    if (rowBounds instanceof Pagination) {
        Pagination page = (Pagination) rowBounds;
        boolean orderBy = true;
        if (page.isSearchCount()) {
            CountOptimize countOptimize = SqlUtils.getCountOptimize(originalSql, optimizeType, dialectType,
                    page.isOptimizeCount());
            orderBy = countOptimize.isOrderBy();
            this.queryTotal(countOptimize.getCountSQL(), mappedStatement, boundSql, page, connection);
            if (page.getTotal() <= 0) {
                return invocation.proceed();
            }
        }
        String buildSql = SqlUtils.concatOrderBy(originalSql, page, orderBy);
        originalSql = DialectFactory.buildPaginationSql(page, buildSql, dialectType, dialectClazz);
    } else {
        // support physical Pagination for RowBounds
        originalSql = DialectFactory.buildPaginationSql(rowBounds, originalSql, dialectType, dialectClazz);
    }

    /*
       * <p> ? </p> <p> ???????</p>
     */
    metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
    metaStatementHandler.setValue("delegate.rowBounds.offset", RowBounds.NO_ROW_OFFSET);
    metaStatementHandler.setValue("delegate.rowBounds.limit", RowBounds.NO_ROW_LIMIT);
    return invocation.proceed();
}