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.joey.Fujikom.common.persistence.interceptor.PaginationInterceptor.java

License:Open Source 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();

    //??/*from   w  w  w . ja v a  2 s . c  o  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());
        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));

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

From source file:com.linju.framework.pager.sqlsource.PageProviderSqlSource.java

License:Open Source License

@Override
public BoundSql getBoundSql(Object parameterObject) {
    BoundSql boundSql = null;/*from   w w  w  . ja  va2  s.c  om*/
    if (parameterObject instanceof Map && ((Map) parameterObject).containsKey(PROVIDER_OBJECT)) {
        boundSql = providerSqlSource.getBoundSql(((Map) parameterObject).get(PROVIDER_OBJECT));
    } else {
        boundSql = providerSqlSource.getBoundSql(parameterObject);
    }
    if (count) {
        return new BoundSql(configuration, parser.getCountSql(boundSql.getSql()),
                boundSql.getParameterMappings(), parameterObject);
    } else {
        return new BoundSql(configuration, parser.getPageSql(boundSql.getSql()),
                parser.getPageParameterMapping(configuration, boundSql), parameterObject);
    }
}

From source file:com.lushapp.common.orm.mybatis.interceptor.PaginationInterceptor.java

License:Open Source License

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 ww. j a v a  2 s  .  com
    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.setTotalCount(
                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());
        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));

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

From source file:com.luxoft.mybatis.splitter.SwitchingSqlSource.java

License:Apache License

@Override
public BoundSql getBoundSql(Object parameterObject) {
    BoundSql subBoundSql = new BoundSql(configuration, sql, parameterMappings, parameterObject);
    for (ParameterMapping parameterMapping : subBoundSql.getParameterMappings()) {
        String property = new PropertyTokenizer(parameterMapping.getProperty()).getName();
        if (parentBoundSql.hasAdditionalParameter(property)) {
            subBoundSql.setAdditionalParameter(property, parentBoundSql.getAdditionalParameter(property));
        }/*from www . ja va  2  s  . com*/
    }
    return subBoundSql;
}

From source file:com.monee1988.core.mybatis.pageinterceptor.PageInterceptor.java

License:Open Source License

/**
 *
 * @param page//from   ww w . j  av  a2s .c  o m
 * @param mappedStatement
 * @param connection
 * @param parameterObject
  */
private void setTotalRecord(Page<?> page, MappedStatement mappedStatement, Connection connection,
        Object parameterObject) {

    BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
    String sql = boundSql.getSql();
    String countSql = removeBreakingWhitespace(this.getCountSql(sql));
    List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
    BoundSql countBoundSql = new BoundSql(mappedStatement.getConfiguration(), countSql, parameterMappings,
            parameterObject);
    ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject,
            countBoundSql);
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    logger.debug("Total count SQL [{}] ", countSql.toString());
    logger.debug("Total count Parameters: {} ", parameterObject);

    try {
        pstmt = connection.prepareStatement(countSql);
        parameterHandler.setParameters(pstmt);
        rs = pstmt.executeQuery();
        if (rs.next()) {
            int totalRecord = rs.getInt(1);
            logger.debug("Total count: {}", totalRecord);
            page.setTotalCount(totalRecord);
        }
    } 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.mybatisX.plugins.PaginationInterceptor.java

License:Apache License

/**
 * ?/*from w  ww.  ja v  a 2 s.c om*/
 *
 * @param sql
 * @param connection
 * @param mappedStatement
 * @param boundSql
 * @param page
 */
public Pagination count(String sql, Connection connection, MappedStatement mappedStatement, BoundSql boundSql,
        Pagination page) {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = connection.prepareStatement(sql);
        BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), sql,
                boundSql.getParameterMappings(), boundSql.getParameterObject());
        ParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement,
                boundSql.getParameterObject(), countBS);
        parameterHandler.setParameters(pstmt);
        rs = pstmt.executeQuery();
        int total = 0;
        if (rs.next()) {
            total = rs.getInt(1);
        }
        page.setTotal(total);
        /*
         * 
         */
        if (overflowCurrent && (page.getCurrent() > page.getPages())) {
            page = new Pagination(1, page.getSize());
            page.setTotal(total);
        }
    } catch (Exception e) {
        // ignored
    } finally {
        IOUtils.closeQuietly(pstmt, rs);
    }
    return page;
}

From source file:com.saituo.talk.common.persistence.interceptor.PaginationInterceptor.java

License:Open Source License

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

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

    // //?SQL// w w w.  java2 s .  c  om
    // // 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();

    // ??
    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());
        MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));

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

From source file:com.sinotopia.mybatis.pagehelper.PageInterceptor.java

License:Open Source License

@Override
public Object intercept(Invocation invocation) throws Throwable {
    try {//w w  w. j  av  a  2 s  .  com
        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)) {
            //?????
            String msId = ms.getId();
            Configuration configuration = ms.getConfiguration();
            Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField
                    .get(boundSql);
            //?? count 
            if (dialect.beforeCount(ms, parameter, rowBounds)) {
                String countMsId = msId + countSuffix;
                Long count;
                //? count 
                MappedStatement countMs = getExistedMappedStatement(configuration, countMsId);
                if (countMs != null) {
                    count = executeManualCount(executor, countMs, parameter, boundSql, resultHandler);
                } else {
                    countMs = msCountMap.get(countMsId);
                    //
                    if (countMs == null) {
                        //?? ms  Long  ms
                        countMs = MSUtils.newCountMappedStatement(ms, countMsId);
                        msCountMap.put(countMsId, countMs);
                    }
                    count = executeAutoCount(executor, countMs, parameter, boundSql, rowBounds, resultHandler);
                }
                //?
                // 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(configuration, 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.sinotopia.mybatis.pagehelper.PageInterceptor.java

License:Open Source License

/**
 * ? count //from ww w.jav  a 2s.  c  o m
 *
 * @param executor
 * @param countMs
 * @param parameter
 * @param boundSql
 * @param rowBounds
 * @param resultHandler
 * @return
 * @throws IllegalAccessException
 * @throws SQLException
 */
private Long executeAutoCount(Executor executor, MappedStatement countMs, Object parameter, BoundSql boundSql,
        RowBounds rowBounds, ResultHandler resultHandler) throws IllegalAccessException, SQLException {
    Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(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.sjtu.icare.common.persistence.interceptor.SQLHelper.java

License:Open Source License

/**
 * //  ww  w . j  a va  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 {
    //        final String countSql = "select count(1) from (" + sql + ") tmp_count";
    final String 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);
        //?metaParameters..
        MetaObject countBsObject = SystemMetaObject.forObject(countBS);
        MetaObject boundSqlObject = SystemMetaObject.forObject(boundSql);
        countBsObject.setValue("metaParameters", boundSqlObject.getValue("metaParameters"));

        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();
        }
    }
}