Example usage for org.apache.ibatis.session SqlSession selectList

List of usage examples for org.apache.ibatis.session SqlSession selectList

Introduction

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

Prototype

<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);

Source Link

Document

Retrieve a list of mapped objects from the statement key and parameter, within the specified row bounds.

Usage

From source file:com.glaf.core.entity.mybatis.MyBatisEntityDAOImpl.java

License:Apache License

public Paging getPage(int pageNo, int pageSize, SqlExecutor countExecutor, SqlExecutor queryExecutor) {
    if (pageNo < 1) {
        pageNo = 1;/*from  w  w  w .j  a va2  s  .  c  o m*/
    }
    if (pageSize <= 0) {
        pageSize = Paging.DEFAULT_PAGE_SIZE;
    }

    Object object = null;
    int totalCount = 0;
    Paging page = new Paging();
    SqlSession session = getSqlSession();

    Object parameter = countExecutor.getParameter();
    if (parameter != null) {
        object = session.selectOne(countExecutor.getStatementId(), parameter);
    } else {
        object = session.selectOne(countExecutor.getStatementId());
    }

    if (object instanceof Integer) {
        Integer iCount = (Integer) object;
        totalCount = iCount.intValue();
    } else if (object instanceof Long) {
        Long iCount = (Long) object;
        totalCount = iCount.intValue();
    } else if (object instanceof BigDecimal) {
        BigDecimal bg = (BigDecimal) object;
        totalCount = bg.intValue();
    } else if (object instanceof BigInteger) {
        BigInteger bi = (BigInteger) object;
        totalCount = bi.intValue();
    } else {
        String value = object.toString();
        totalCount = Integer.parseInt(value);
    }

    if (totalCount == 0) {
        page.setRows(new java.util.ArrayList<Object>());
        page.setCurrentPage(0);
        page.setPageSize(0);
        page.setTotal(0);
        return page;
    }

    page.setTotal(totalCount);

    int maxPageNo = (page.getTotal() + (pageSize - 1)) / pageSize;
    if (pageNo > maxPageNo) {
        pageNo = maxPageNo;
    }

    List<Object> rows = null;

    Object queryParams = queryExecutor.getParameter();

    int begin = (pageNo - 1) * pageSize;

    logger.debug("begin:" + begin);
    logger.debug("pageSize:" + pageSize);

    RowBounds rowBounds = new RowBounds(begin, pageSize);

    if (queryParams != null) {
        rows = session.selectList(queryExecutor.getStatementId(), queryParams, rowBounds);
    } else {
        rows = session.selectList(queryExecutor.getStatementId(), null, rowBounds);
    }

    page.setRows(rows);
    page.setPageSize(pageSize);
    page.setCurrentPage(pageNo);

    if (LogUtils.isDebug()) {
        logger.debug("params:" + queryParams);
        logger.debug("rows size:" + rows.size());
    }

    return page;
}

From source file:com.google.enterprise.connector.db.DBClient.java

License:Apache License

/**
 * @param skipRows number of rows to skip in the database.
 * @param maxRows max number of rows to return.
 * @return rows - subset of the result of executing the SQL query. E.g.,
 *         result table with columns id and lastName and two rows will be
 *         returned as//w w  w. ja v a2 s  .  c  o  m
 *
 *         <pre>
 *         [{id=1, lastName=last_01}, {id=2, lastName=last_02}]
 * </pre>
 * @throws DBException
 */
public List<Map<String, Object>> executePartialQuery(int skipRows, int maxRows)
        throws SnapshotRepositoryRuntimeException {
    // TODO(meghna): Think about a better way to scroll through the result set.
    List<Map<String, Object>> rows;
    LOG.info("Executing partial query with skipRows = " + skipRows + " and " + "maxRows = " + maxRows);
    SqlSession session = getSqlSession();
    try {
        rows = session.selectList("IbatisDBClient.getAll", null, new RowBounds(skipRows, maxRows));
        LOG.info(
                "Sucessfully executed partial query with skipRows = " + skipRows + " and maxRows = " + maxRows);
    } catch (RuntimeException e) {
        checkDBConnection(session, e);
        rows = new ArrayList<Map<String, Object>>();
    } finally {
        session.close();
    }
    LOG.info("Number of rows returned " + rows.size());
    return rows;
}

From source file:com.google.enterprise.connector.db.DBClient.java

License:Apache License

/**
 * Executes the partial parameterized query for given keyValue and
 * returns the list of records having their key value greater than keyValue
 * parameter./*www . j ava 2s  .  c  o  m*/
 *
 * @param keyValue
 * @return list of documents
 */
public List<Map<String, Object>> executeParameterizePartialQuery(Integer keyValue)
        throws SnapshotRepositoryRuntimeException {
    List<Map<String, Object>> rows;
    int skipRows = 0;
    int maxRows = dbContext.getNumberOfRows();
    // Create a hashmap as to provide input parameters minvalue and maxvalue to
    // the query.
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("value", keyValue);
    LOG.info("Executing partial parametrized query with keyValue = " + keyValue);
    SqlSession session = getSqlSession();
    try {
        rows = session.selectList("IbatisDBClient.getAll", paramMap, new RowBounds(skipRows, maxRows));
        LOG.info("Sucessfully executed partial parametrized query with keyValue = " + keyValue);
    } catch (RuntimeException e) {
        checkDBConnection(session, e);
        rows = new ArrayList<Map<String, Object>>();
    } finally {
        session.close();
    }
    LOG.info("Number of rows returned " + rows.size());
    return rows;
}

From source file:com.sinotopia.mybatis.pagehelper.test.basic.PageHelperTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count//w w  w  .j a v a 2s.  c o  m
 * ??count??
 * ?count?startPage
 * <p/>
 * ?startPagestartPage?startPage??
 */
@Test
public void testNamespaceWithRowBounds() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        //?010?
        List<Country> list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        //?1010?
        list = sqlSession.selectList("selectAll", null, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(11, list.get(0).getId());
        assertEquals(20, list.get(list.size() - 1).getId());

        //?2020?
        list = sqlSession.selectList("selectAll", null, new RowBounds(20, 20));
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(21, list.get(0).getId());
        assertEquals(40, list.get(list.size() - 1).getId());

        //?startPageRowBoundsstartPage
        PageHelper.startPage(1, 20);
        list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(20, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.tiamaes.mybatis.test.basic.PageHelperTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count//from w w  w  .j  a  v  a 2 s .c  om
 * ??count??
 * ?count?startPage
 * <p/>
 * ?startPagestartPage?startPage??
 */
@Test
public void testNamespaceWithRowBounds() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    try {
        //?010?
        List<Country> list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(10, list.size());
        assertEquals(-1, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        //?1010?
        list = sqlSession.selectList("selectAll", null, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(-1, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(11, list.get(0).getId());
        assertEquals(20, list.get(list.size() - 1).getId());

        //?2020?
        list = sqlSession.selectList("selectAll", null, new RowBounds(20, 20));
        assertEquals(20, list.size());
        assertEquals(-1, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(21, list.get(0).getId());
        assertEquals(40, list.get(list.size() - 1).getId());

        //?startPageRowBoundsstartPage
        PageHelper.startPage(1, 20);
        list = sqlSession.selectList("selectAll", null, new RowBounds(0, 10));
        assertEquals(20, list.size());
        assertEquals(183, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(20, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.tiamaes.mybatis.test.rowbounds.RowBoundsTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count/*  w  w w. java  2s.  c om*/
 * ??count??
 * ?count?startPage
 * <p/>
 * ?startPagestartPage?startPage??
 */
@Test
public void testNamespaceWithRowBounds() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {
        //?010?
        List<Country> list = sqlSession.selectList("selectAll", null, new RowBounds(1, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        //?1010?
        list = sqlSession.selectList("selectAll", null, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(91, list.get(0).getId());
        assertEquals(100, list.get(list.size() - 1).getId());

        //?2020?
        list = sqlSession.selectList("selectAll", null, new RowBounds(6, 20));
        assertEquals(20, list.size());
        assertEquals(183, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(120, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.tiamaes.mybatis.test.rowbounds.RowBoundsTest.java

License:Open Source License

@Test
public void testNamespaceWithRowBounds2() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {//from ww  w  . j  av a2 s.co m
        //?010?
        List<Country> list = sqlSession.selectList("selectIf", null, new RowBounds(1, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 10);
        //?1010?
        list = sqlSession.selectList("selectIf", map, new RowBounds(10, 10));
        assertEquals(10, list.size());
        assertEquals(173, ((Pagination<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(110, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.yimidida.shards.select.impl.ShardSelectImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public <E> List<E> getResultList() {
    ShardOperation<List<Object>> shardOp = new ShardOperation<List<Object>>() {
        public List<Object> execute(SqlSession session, ShardId shardId) {

            return session.selectList(selectFactory.getStatement(),
                    ParameterUtil.resolve(selectFactory.getParameter(), shardId), selectFactory.getRowBounds());
        }/*from www .  ja v a2  s  .  c om*/

        public String getOperationName() {
            return "getResultList()";
        }
    };

    return (List<E>) shardAccessStrategy.apply(shards, shardOp, new ConcatenateListsExitStrategy(),
            selectCollector);
}

From source file:net.hasor.db.orm.mybatis3.SqlExecutorTemplate.java

License:Apache License

public <E> List<E> selectList(final String statement, final Object parameter, final RowBounds rowBounds)
        throws SQLException {
    return this.execute(new SqlSessionCallback<List<E>>() {
        public List<E> doSqlSession(SqlSession sqlSession) {
            return sqlSession.selectList(statement, parameter, rowBounds);
        }//  ww  w .ja v a 2  s . c  o m
    });
}

From source file:org.sonar.core.permission.PermissionDao.java

License:Open Source License

/**
 * @return a paginated list of users.//from   ww w .j  a  va2  s.c  om
 */
public List<UserWithPermissionDto> selectUsers(PermissionQuery query, @Nullable Long componentId, int offset,
        int limit) {
    SqlSession session = myBatis.openSession(false);
    try {
        Map<String, Object> params = newHashMap();
        params.put(QUERY_PARAMETER, query);
        params.put(COMPONENT_ID_PARAMETER, componentId);
        return session.selectList("org.sonar.core.permission.PermissionMapper.selectUsers", params,
                new RowBounds(offset, limit));
    } finally {
        MyBatis.closeQuietly(session);
    }
}