Example usage for org.apache.ibatis.session RowBounds RowBounds

List of usage examples for org.apache.ibatis.session RowBounds RowBounds

Introduction

In this page you can find the example usage for org.apache.ibatis.session RowBounds RowBounds.

Prototype

public RowBounds(int offset, int limit) 

Source Link

Usage

From source file:plum.mybatis.PaginationInterceptor.java

License:Apache License

/**
 * perform paging intercetion.//from   w w w. ja v  a 2 s  .  c  o m
 *
 * @param queryArgs Executor.query params.
 */
private void processIntercept(final Object[] queryArgs) {
    //queryArgs = query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler)
    final MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];

    final Object parameter = queryArgs[PARAMETER_INDEX];
    //the need for paging intercept.
    boolean interceptor = ms.getId().matches(_sql_regex);
    //obtain paging information.
    final PageQuery pageQuery = interceptor ? PagingParametersFinder.getInstance().findCriteria(parameter)
            : new PageQuery(PageQuery.DEFAULT_PAGE_SIZE);
    if (interceptor) {
        PAGINATION_CRITERIA_THREAD_LOCAL.set(pageQuery);
    }
    final RowBounds rowBounds = (interceptor) ? offset_paging((RowBounds) queryArgs[ROWBOUNDS_INDEX])
            : (RowBounds) queryArgs[ROWBOUNDS_INDEX];
    int offset = rowBounds.getOffset();
    int limit = rowBounds.getLimit();

    if (_dialect.supportsLimit() && (offset != RowBounds.NO_ROW_OFFSET || limit != RowBounds.NO_ROW_LIMIT)) {
        final BoundSql boundSql = ms.getBoundSql(parameter);
        String sql = boundSql.getSql().trim();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Pagination sql is <" + sql + ">");
        }
        //implementation of the access to the total number of SQL,to obtain  the total number and stored in the thread location

        Connection connection = null;
        try {
            //get connection
            connection = ms.getConfiguration().getEnvironment().getDataSource().getConnection();
            int count = SQLHelp.getCount(sql, connection, ms, parameter, boundSql, _dialect);
            final Pager pager = new Pager(pageQuery.getPage(), pageQuery.getPageSize(), count);
            PAGINATION_COUNT.set(pager);
        } catch (SQLException e) {
            LOG.error("The total number of access to the database failure.", e);
            PAGINATION_COUNT.set(null);
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (SQLException e) {
                LOG.error("Close the database connection error.", e);
            }
        }
        if (_dialect.supportsLimit()) {
            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;
    }
}

From source file:snipe.business.dao.base.BaseDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List selectList(final String statementName, final Object parameterObject, final int skipResults,
        final int maxResults) {

    RowBounds bounds = new RowBounds(skipResults, maxResults);

    List result = getSqlSession().selectList(statementName, parameterObject, bounds);
    return result;
}

From source file:uap.workflow.engine.db.DbSqlSession.java

License:Apache License

@SuppressWarnings({ "rawtypes", "unchecked" })
public List selectList(String statement, Object parameter, Page page) {
    statement = dbSqlSessionFactory.mapStatement(statement);
    List loadedObjects;/*from  ww w  . j ava  2  s.c  o  m*/
    if (page != null) {
        loadedObjects = sqlSession.selectList(statement, parameter,
                new RowBounds(page.getFirstResult(), page.getMaxResults()));
    } else {
        loadedObjects = sqlSession.selectList(statement, parameter);
    }
    return filterLoadedObjects(loadedObjects);
}

From source file:uap.workflow.engine.mgr.TableDataManager.java

License:Apache License

@SuppressWarnings("unchecked")
public TablePage getTablePage(TablePageQueryImpl tablePageQuery, int firstResult, int maxResults) {
    TablePage tablePage = new TablePage();
    List<Map<String, Object>> tableData = (List<Map<String, Object>>) getDbSqlSession().getSqlSession()
            .selectList("selectTableData", tablePageQuery, new RowBounds(firstResult, maxResults));
    tablePage.setTableName(tablePageQuery.getTableName());
    tablePage.setTotal(getTableCount(tablePageQuery.getTableName()));
    tablePage.setRows(tableData);//  w  w  w.  j  ava2  s  .  com
    tablePage.setFirstResult(firstResult);
    return tablePage;
}

From source file:webim.dao.ibatis.WebimHistoryDao.java

License:Apache License

/**
 * ?with?MySQL:<br>//w ww . j  av a  2  s  .c om
 * 
 * <pre>
 *     if (type == "chat")
 *       {
 *           
 *           "SELECT * FROM webim_Histories WHERE `type` = 'chat' 
 *           AND ((`to`=%s AND `from`=%s AND `fromdel` != 1) 
 *           OR (`send` = 1 AND `from`=%s AND `to`=%s AND `todel` != 1))  
 *           ORDER BY timestamp DESC LIMIT %d", $with, $uid, $with, $uid, $limit );
 *           
 *       }
 *       else
 *       {
 *           
 *           "SELECT * FROM  webim_histories 
 *               WHERE `to`=%s AND `type`='grpchat' AND send = 1 
 *               ORDER BY timestamp DESC LIMIT %d", , $with, $limit);
 *           
 *       }
 * </pre>
 * 
 * @param uid
 *            ?id
 * @param with
 *            id????long
 * @param type
 *            chat | grpchat
 * 
 * @param limit
 *            ?
 * @return ?
 */
public List<WebimHistory> getHistories(String uid, String with, String type, int limit) {
    List<WebimHistory> histories = null;
    Map<String, String> params = new HashMap<String, String>();
    //with -> to
    params.put("to", with);
    //uid -> from
    params.put("from", uid);
    RowBounds rb = new RowBounds(0, limit);
    SqlSession session = sessionFactory.openSession();
    try {
        if (type.equals("chat")) {
            histories = session.selectList("HistoryMapper.selectChat", params, rb);
        } else if (type.equals("grpchat")) {
            histories = session.selectList("HistoryMapper.selectGrpChat", params, rb);
        } else {
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
    if (histories == null)
        histories = new ArrayList<WebimHistory>();
    Collections.reverse(histories);
    return histories;
}

From source file:webim.dao.ibatis.WebimHistoryDao.java

License:Apache License

/**
 * ??MySQL:<br>//from  w  ww . j  a  v  a2s.c  o  m
 * 
 * "SELECT * FROM  webim_histories WHERE `to` = ? and send != 1 ORDER BY timestamp DESC LIMIT %d"
 * , limit;
 * 
 * @param uid
 *            uid
 * @return ?
 */
public List<WebimHistory> getOfflineHistories(String uid, int limit) {
    List<WebimHistory> histories = null;
    SqlSession session = sessionFactory.openSession();
    try {
        histories = session.selectList("HistoryMapper.selectOffline", uid, new RowBounds(0, limit));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
    if (histories == null)
        histories = new ArrayList<WebimHistory>();
    Collections.reverse(histories);
    return histories;
}