Example usage for org.apache.ibatis.transaction Transaction getConnection

List of usage examples for org.apache.ibatis.transaction Transaction getConnection

Introduction

In this page you can find the example usage for org.apache.ibatis.transaction Transaction getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Retrieve inner database connection.

Usage

From source file:co.com.xoftix.paginacionxoftix.SQLHelp.java

License:Apache License

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

    Connection connection = transaction.getConnection();
    PreparedStatement countStmt = connection.prepareStatement(count_sql);
    DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
    handler.setParameters(countStmt);

    ResultSet rs = countStmt.executeQuery();
    int count = 0;
    if (rs.next()) {
        count = rs.getInt(1);
    }
    logger.debug("Total count: {}", count);
    return count;

}

From source file:com.bigbata.craftsman.dao.mybatis.support.SQLHelp.java

License:Apache License

/**
 * /*from   www.ja v a 2 s  . c om*/
 *
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql        boundSql
 * @param dialect         database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static long getCount(final MappedStatement mappedStatement, final Transaction transaction,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String count_sql = dialect.getCountSQL();
    logger.debug("Total count SQL [{}] ", count_sql);
    logger.debug("Total count Parameters: {} ", parameterObject);

    Connection connection = transaction.getConnection();
    PreparedStatement countStmt = connection.prepareStatement(count_sql);
    DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
    handler.setParameters(countStmt);

    ResultSet rs = countStmt.executeQuery();
    long count = 0;
    if (rs.next()) {
        count = rs.getLong(1);
    }
    logger.debug("Total count: {}", count);
    return count;

}

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

License:Apache License

/**
 * // w  ww.ja v  a2s  . c  o  m
 *
 * @param mappedStatement mapped
 * @param parameterObject ?
 * @param boundSql boundSql
 * @param dialect database dialect
 * @return 
 * @throws java.sql.SQLException sql
 */
public static int getCount(String sql, final MappedStatement mappedStatement, final Transaction transaction,
        final Object parameterObject, final BoundSql boundSql, Dialect dialect) throws SQLException {
    final String countSql = dialect.getCountString(sql);
    if (logger.isDebugEnabled()) {
        logger.debug("Total count SQL [{}] ", countSql);
        logger.debug("Total count Parameters: {} ", parameterObject);
    }
    Connection connection = transaction.getConnection();
    PreparedStatement countStmt = connection.prepareStatement(countSql);
    DefaultParameterHandler handler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
    handler.setParameters(countStmt);

    ResultSet rs = countStmt.executeQuery();
    int count = 0;
    if (rs.next()) {
        count = rs.getInt(1);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Total count: {}", count);
    }
    return count;

}

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

License:Open Source License

void processMybatisIntercept(final Object[] queryArgs, Invocation invocation) {
    MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
    Object parameter = queryArgs[PARAMETER_INDEX];

    Page<?> page = null;//from   ww  w  . j a  v  a  2s .c  o  m

    if (parameter != null) {
        page = convertParameter(page, parameter);
    }

    if (dialect.supportsLimit() && page != null) {

        BoundSql boundSql = ms.getBoundSql(parameter);
        String sql = boundSql.getSql().trim();

        final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
        int offset = rowBounds.getOffset();
        int limit = rowBounds.getLimit();
        offset = page.getOffset();
        limit = page.getPageSize();

        CachingExecutor executor = (CachingExecutor) invocation.getTarget();

        Transaction transaction = executor.getTransaction();
        try {
            Connection connection = transaction.getConnection();
            /**
             * 
             */
            this.setTotalRecord(page, ms, connection, parameter);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (dialect.supportsLimitOffset()) {

            sql = dialect.getLimitString(sql, offset, limit);
            offset = RowBounds.NO_ROW_OFFSET;

        } else {

            sql = dialect.getLimitString(sql, 0, limit);

        }
        limit = RowBounds.NO_ROW_LIMIT;

        queryArgs[ROWBOUNDS_INDEX] = new RowBounds(offset, limit);

        BoundSql newBoundSql = copyFromBoundSql(ms, boundSql, sql);

        MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));

        queryArgs[MAPPED_STATEMENT_INDEX] = newMs;

    }
}