Example usage for org.apache.ibatis.mapping BoundSql getParameterMappings

List of usage examples for org.apache.ibatis.mapping BoundSql getParameterMappings

Introduction

In this page you can find the example usage for org.apache.ibatis.mapping BoundSql getParameterMappings.

Prototype

public List<ParameterMapping> getParameterMappings() 

Source Link

Usage

From source file:com.funtl.framework.smoke.core.commons.persistence.interceptor.PaginationInterceptor.java

License:Apache License

@Override
public Object intercept(Invocation invocation) throws Throwable {

    final MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];

    //        //?SQL
    ////        if (mappedStatement.getId().matches(_SQL_PATTERN)) {
    //        if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
    Object parameter = invocation.getArgs()[1];
    BoundSql boundSql = mappedStatement.getBoundSql(parameter);
    Object parameterObject = boundSql.getParameterObject();

    //??// w w w.  j a  va2s. co m
    Page<Object> page = null;
    if (parameterObject != null) {
        page = convertParameter(parameterObject, page);
    }

    //
    if (page != null && page.getPageSize() != -1) {

        if (StringUtils.isBlank(boundSql.getSql())) {
            return null;
        }
        String originalSql = boundSql.getSql().trim();

        //
        page.setCount(SQLHelper.getCount(originalSql, null, mappedStatement, parameterObject, boundSql, log));

        //  ??
        String pageSql = SQLHelper.generatePageSql(originalSql, page, DIALECT);
        //                if (log.isDebugEnabled()) {
        //                    log.debug("PAGE SQL:" + StringUtils.replace(pageSql, "\n", ""));
        //                }
        invocation.getArgs()[2] = new RowBounds(RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
        BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), pageSql,
                boundSql.getParameterMappings(), boundSql.getParameterObject());
        //MyBatis foreach ? start
        if (Reflections.getFieldValue(boundSql, "metaParameters") != null) {
            MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");
            Reflections.setFieldValue(newBoundSql, "metaParameters", mo);
        }
        //MyBatis foreach ? end
        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));

        invocation.getArgs()[0] = newMs;
    }
    //        }
    return invocation.proceed();
}

From source file:com.funtl.framework.smoke.core.commons.persistence.interceptor.SQLHelper.java

License:Apache License

/**
 * //from www  . j  a  v  a  2  s  . co  m
 *
 * @param sql             SQL?
 * @param connection      ?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @return 
 * @throws SQLException sql
 */
public static int getCount(final String sql, final Connection connection, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Log log) throws SQLException {
    String dbName = Global.getConfig("jdbc.type");
    final String countSql;
    if ("oracle".equals(dbName)) {
        countSql = "select count(1) from (" + sql + ") tmp_count";
    } else {
        countSql = "select count(1) from (" + removeOrders(sql) + ") tmp_count";
        //           countSql = "select count(1) " + removeSelect(removeOrders(sql));
    }
    Connection conn = connection;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("COUNT SQL: " + StringUtils.replaceEach(countSql, new String[] { "\n", "\t" },
                    new String[] { " ", " " }));
        }
        if (conn == null) {
            conn = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
        }
        ps = conn.prepareStatement(countSql);
        BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql,
                boundSql.getParameterMappings(), parameterObject);
        //MyBatis foreach ? start
        if (Reflections.getFieldValue(boundSql, "metaParameters") != null) {
            MetaObject mo = (MetaObject) Reflections.getFieldValue(boundSql, "metaParameters");
            Reflections.setFieldValue(countBS, "metaParameters", mo);
        }
        //MyBatis foreach ? end
        SQLHelper.setParameters(ps, mappedStatement, countBS, parameterObject);
        rs = ps.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        return count;
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (ps != null) {
            ps.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

From source file:com.github.ibole.infrastructure.persistence.db.mybatis.pagination.PaginationInterceptor.java

License:Apache License

private BoundSql copyFromBoundSql(MappedStatement ms, BoundSql boundSql, String sql) {
    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));
        }/*from w  w w  .  j a va2 s  .  c om*/
    }
    return newBoundSql;
}

From source file:com.github.pagehelper.PageInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    try {//from w  ww  .jav  a  2 s  . c  o m
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        Object parameter = args[1];
        RowBounds rowBounds = (RowBounds) args[2];
        ResultHandler resultHandler = (ResultHandler) args[3];
        Executor executor = (Executor) invocation.getTarget();
        CacheKey cacheKey;
        BoundSql boundSql;
        //?
        if (args.length == 4) {
            //4 ?
            boundSql = ms.getBoundSql(parameter);
            cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
        } else {
            //6 ?
            cacheKey = (CacheKey) args[4];
            boundSql = (BoundSql) args[5];
        }
        List resultList;
        //????
        if (!dialect.skip(ms, parameter, rowBounds)) {
            //?????
            Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField
                    .get(boundSql);
            //?? count 
            if (dialect.beforeCount(ms, parameter, rowBounds)) {
                // count  key
                CacheKey countKey = executor.createCacheKey(ms, parameter, RowBounds.DEFAULT, boundSql);
                countKey.update(MSUtils.COUNT);
                MappedStatement countMs = msCountMap.get(countKey);
                if (countMs == null) {
                    //?? ms  Long  ms
                    countMs = MSUtils.newCountMappedStatement(ms);
                    msCountMap.put(countKey, countMs);
                }
                //? count sql
                String countSql = dialect.getCountSql(ms, boundSql, parameter, rowBounds, countKey);
                BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql,
                        boundSql.getParameterMappings(), parameter);
                //? SQL ???? BoundSql 
                for (String key : additionalParameters.keySet()) {
                    countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
                // count 
                Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler,
                        countKey, countBoundSql);
                Long count = (Long) ((List) countResultList).get(0);
                //?
                // true false 
                if (!dialect.afterCount(count, parameter, rowBounds)) {
                    // 0 
                    return dialect.afterPage(new ArrayList(), parameter, rowBounds);
                }
            }
            //??
            if (dialect.beforePage(ms, parameter, rowBounds)) {
                //? key
                CacheKey pageKey = cacheKey;
                //??
                parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
                //? sql
                String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
                BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql,
                        boundSql.getParameterMappings(), parameter);
                //??
                for (String key : additionalParameters.keySet()) {
                    pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
                }
                //
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey,
                        pageBoundSql);
            } else {
                //??
                resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey,
                        boundSql);
            }
        } else {
            //rowBounds??????
            resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
        }
        return dialect.afterPage(resultList, parameter, rowBounds);
    } finally {
        dialect.afterAll();
    }
}

From source file:com.github.pagehelper.util.ExecutorUtil.java

License:Open Source License

/**
 * ? count // w  ww  .  j a  v  a 2 s  . c om
 *
 * @param dialect
 * @param executor
 * @param countMs
 * @param parameter
 * @param boundSql
 * @param rowBounds
 * @param resultHandler
 * @return
 * @throws SQLException
 */
public static Long executeAutoCount(Dialect dialect, Executor executor, MappedStatement countMs,
        Object parameter, BoundSql boundSql, RowBounds rowBounds, ResultHandler resultHandler)
        throws SQLException {
    Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);
    // count  key
    CacheKey countKey = executor.createCacheKey(countMs, parameter, RowBounds.DEFAULT, boundSql);
    //? count sql
    String countSql = dialect.getCountSql(countMs, boundSql, parameter, rowBounds, countKey);
    //countKey.update(countSql);
    BoundSql countBoundSql = new BoundSql(countMs.getConfiguration(), countSql, boundSql.getParameterMappings(),
            parameter);
    //? SQL ???? BoundSql 
    for (String key : additionalParameters.keySet()) {
        countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
    }
    // count 
    Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey,
            countBoundSql);
    Long count = (Long) ((List) countResultList).get(0);
    return count;
}

From source file:com.github.pagehelper.util.ExecutorUtil.java

License:Open Source License

/**
 * //from   ww w  . j a  v a 2  s . c  o m
 *
 * @param dialect
 * @param executor
 * @param ms
 * @param parameter
 * @param rowBounds
 * @param resultHandler
 * @param boundSql
 * @param cacheKey
 * @param <E>
 * @return
 * @throws SQLException
 */
public static <E> List<E> pageQuery(Dialect dialect, Executor executor, MappedStatement ms, Object parameter,
        RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql, CacheKey cacheKey)
        throws SQLException {
    //??
    if (dialect.beforePage(ms, parameter, rowBounds)) {
        //? key
        CacheKey pageKey = cacheKey;
        //??
        parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
        //? sql
        String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
        BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(),
                parameter);

        Map<String, Object> additionalParameters = getAdditionalParameter(boundSql);
        //??
        for (String key : additionalParameters.keySet()) {
            pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
        }
        //
        return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
    } else {
        //??
        return executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
    }
}

From source file:com.gj.test.base.paginator.mybatis.SQLHelp.java

License:Apache License

/**
 * /*  w w  w .  j  a  v a 2 s .c o m*/
 * 
 * @param sql SQL?
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql boundSql
 * @param dialect database dialect
 * @return 
 * @throws SQLException sql
 */
public static int getCount(final String sql, final MappedStatement mappedStatement,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String count_sql = dialect.getCountString(sql);
    logger.debug("Total count SQL [{" + count_sql + "}] ");
    logger.debug("Total count Parameters: {" + parameterObject + "} ");

    Connection connection = null;
    PreparedStatement countStmt = null;
    ResultSet rs = null;
    try {
        connection = mappedStatement.getConfiguration().getEnvironment().getDataSource().getConnection();
        countStmt = connection.prepareStatement(count_sql);
        final BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), count_sql,
                boundSql.getParameterMappings(), parameterObject);

        // Field metaParamsField = ReflectUtil.getFieldByFieldName(boundSql, "metaParameters");
        // if (metaParamsField != null) {
        // MetaObject mo = (MetaObject) ReflectUtil.getValueByFieldName(boundSql, "metaParameters");
        // ReflectUtil.setValueByFieldName(countBS, "metaParameters", mo);
        // }
        // setParameters(prepStat, configuration, countBS, parameterObject);
        //
        DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject,
                countBS);
        handler.setParameters(countStmt);

        rs = countStmt.executeQuery();
        int count = 0;
        if (rs.next()) {
            count = rs.getInt(1);
        }
        logger.debug("Total count: {" + count + "}");
        return count;
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
        } finally {
            try {
                if (countStmt != null) {
                    countStmt.close();
                }
            } finally {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
        }
    }
}

From source file:com.hj.blog.common.orm.PageInterceptor.java

License:Apache License

private void setTotalCount(DigitalPage page, Object parameterObject, MappedStatement mappedStatement,
        Connection connection) {/*from w  w w  . j a va  2 s  .c o m*/
    BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
    String sql = boundSql.getSql();
    String countSql = getTotalCountSql(sql);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings,
            parameterObject);
    ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject,
            countBoundSql);
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        preparedStatement = connection.prepareStatement(countSql);
        parameterHandler.setParameters(preparedStatement);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            int totalCount = resultSet.getInt(1);
            page.setTotalCount(totalCount);
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (resultSet != null)
                resultSet.close();
            if (preparedStatement != null)
                preparedStatement.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.huang.rp.common.persistence.PageInterceptor.java

License:Apache License

/**
 * ??page//from ww w  .  ja v a2s.c o m
 */
private void setTotalRecord(QueryFilter filter, MappedStatement mappedStatement, Connection connection) {
    BoundSql boundSql = mappedStatement.getBoundSql(filter);
    String sql = boundSql.getSql();
    String countSql = this.getCountSql(sql);
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings,
            boundSql.getParameterObject());
    MetaObject metaParameters = (MetaObject) ReflectionUtils.getFieldValue(boundSql, "metaParameters");
    ReflectionUtils.setFieldValue(countBoundSql, "metaParameters", metaParameters);
    ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, filter, countBoundSql);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = connection.prepareStatement(countSql);
        parameterHandler.setParameters(pstmt);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            int totalRecord = rs.getInt(1);
            filter.setRecords(totalRecord);
            int rows = filter.getRows();
            // 
            int total = totalRecord / rows;
            total = totalRecord % rows == 0 ? total : total + 1;
            filter.setTotal(total);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.icfcc.db.pagehelper.sqlsource.PageDynamicSqlSource.java

License:Open Source License

@Override
protected BoundSql getCountBoundSql(Object parameterObject) {
    DynamicContext context = new DynamicContext(configuration, parameterObject);
    rootSqlNode.apply(context);/*from  w w  w .  j  a va  2 s .c om*/
    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
    Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
    SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());
    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);
    sqlSource = new StaticSqlSource(configuration, parser.getCountSql(boundSql.getSql()),
            boundSql.getParameterMappings());
    boundSql = sqlSource.getBoundSql(parameterObject);
    //??
    for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {
        boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());
    }
    return boundSql;
}