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

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

Introduction

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

Prototype

public BoundSql(Configuration configuration, String sql, List<ParameterMapping> parameterMappings,
            Object parameterObject) 

Source Link

Usage

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  a2s .  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 /*from w ww. java 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   w  w w.ja 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.  co 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) {//  www .ja  v a 2 s  .  c om
    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// w  ww  .j a  v a 2  s  .com
 */
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.ibatis.sqlmap.engine.builder.DynamicSqlSource.java

License:Apache License

public BoundSql getBoundSql(Object parameterObject) {
    return new BoundSql(configuration, getSql(parameterObject), getParameterMappings(parameterObject),
            parameterObject);//from  w  w  w  . ja  v a  2 s.co m
}

From source file:com.ibatis.sqlmap.engine.builder.SimpleSqlSource.java

License:Apache License

public BoundSql getBoundSql(Object parameterObject) {
    return new BoundSql(configuration, getSql(parameterObject), parameterMappings, parameterObject);
}

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

License:Open Source License

@Override
protected BoundSql getCountBoundSql(Object parameterObject) {
    BoundSql boundSql;/*from   ww  w.  ja  va2 s.c  o m*/
    SqlSource sqlSource = createSqlSource(parameterObject);
    boundSql = sqlSource.getBoundSql(parameterObject);
    return new BoundSql(configuration, parser.getCountSql(boundSql.getSql()), boundSql.getParameterMappings(),
            parameterObject);
}

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

License:Open Source License

@Override
protected BoundSql getPageBoundSql(Object parameterObject) {
    BoundSql boundSql;//from www.  ja v  a 2  s.c om
    if (parameterObject instanceof Map && ((Map) parameterObject).containsKey(PROVIDER_OBJECT)) {
        SqlSource sqlSource = createSqlSource(((Map) parameterObject).get(PROVIDER_OBJECT));
        boundSql = sqlSource.getBoundSql(((Map) parameterObject).get(PROVIDER_OBJECT));
    } else {
        SqlSource sqlSource = createSqlSource(parameterObject);
        boundSql = sqlSource.getBoundSql(parameterObject);
    }
    return new BoundSql(configuration, parser.getPageSql(boundSql.getSql()),
            parser.getPageParameterMapping(configuration, boundSql), parameterObject);
}