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.github.pagehelper.rowbounds.test.PageRowBoundsTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count//ww w. j  av a2 s  . c o  m
 * ??count??
 * ?count?startPage
 * <p/>
 * ?startPagestartPage?startPage??
 */
@Test
public void testNamespaceWithPageRowBounds() {
    SqlSession sqlSession = RowBoundsHelper.getSqlSession();
    try {
        //?010?
        PageRowBounds pageRowBounds = new PageRowBounds(0, 10);
        List<Country> list = sqlSession.selectList("selectAll", null, pageRowBounds);
        assertEquals(10, list.size());
        assertEquals(183L, pageRowBounds.getTotal().longValue());
        //??
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());

        //?1010?
        pageRowBounds = new PageRowBounds(90, 10);
        list = sqlSession.selectList("selectAll", null, pageRowBounds);
        assertEquals(10, list.size());
        assertEquals(183L, pageRowBounds.getTotal().longValue());
        //??
        assertEquals(91, list.get(0).getId());
        assertEquals(100, list.get(list.size() - 1).getId());

        //?2020?
        pageRowBounds = new PageRowBounds(100, 20);
        list = sqlSession.selectList("selectAll", null, pageRowBounds);
        assertEquals(20, list.size());
        assertEquals(183L, pageRowBounds.getTotal().longValue());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(120, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.rowbounds.test.PageRowBoundsTest.java

License:Open Source License

@Test
public void testNamespaceWithRowBounds2() {
    SqlSession sqlSession = RowBoundsHelper.getSqlSession();
    try {//from   w w w  .  j  a va  2  s .  c o  m
        //?010?
        PageRowBounds pageRowBounds = new PageRowBounds(0, 10);
        List<Country> list = sqlSession.selectList("selectIf", null, pageRowBounds);
        assertEquals(10, list.size());
        assertEquals(183L, pageRowBounds.getTotal().longValue());
        //??
        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?
        pageRowBounds = new PageRowBounds(90, 10);
        list = sqlSession.selectList("selectIf", map, pageRowBounds);
        assertEquals(10, list.size());
        assertEquals(173L, pageRowBounds.getTotal().longValue());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(110, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.rowbounds.test.RowBoundsTest.java

License:Open Source License

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

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

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

From source file:com.github.pagehelper.rowbounds.test.RowBoundsTest.java

License:Open Source License

@Test
public void testNamespaceWithRowBounds2() {
    SqlSession sqlSession = RowBoundsHelper.getSqlSession();
    try {/*www .  j  a va  2s .  c  o m*/
        //?010?
        List<Country> list = sqlSession.selectList("selectIf", null, new RowBounds(0, 10));
        assertEquals(10, list.size());
        //??
        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(90, 10));
        assertEquals(10, list.size());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(110, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.basic.PageHelperTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count/*from  ww  w .  ja v  a  2s . 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, ((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(-1, ((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(-1, ((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.github.pagehelper.test.namespace.BasicTest.java

License:Open Source License

@Test
public void testNamespace1() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {//from w ww . jav a 2 s. co m
        Map<String, Object> map = new HashMap<String, Object>();
        Country country = new Country();
        country.setCountryname("China");
        map.put("country", country);
        //????Map
        map = Collections.unmodifiableMap(map);
        List<Country> list = sqlSession.selectList("select1", map, new RowBounds(1, 10));
        assertEquals(1, list.size());
        //??
        assertEquals(35, list.get(0).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.namespace.BasicTest.java

License:Open Source License

@Test
public void testNamespace3() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {//from  www .java  2 s. c om
        Map<String, Object> map = new HashMap<String, Object>();
        Country country = new Country();
        map.put("country", country);
        //????Map
        map = Collections.unmodifiableMap(map);
        List<Country> list = sqlSession.selectList("select1", map, new RowBounds(1, 10));
        assertEquals(10, list.size());
        //??
        assertEquals(1, list.get(0).getId());

        map = new HashMap<String, Object>();
        country = new Country();
        country.setCountryname("China");
        map.put("country", country);
        //????Map
        map = Collections.unmodifiableMap(map);
        list = sqlSession.selectList("select1", map, new RowBounds(1, 10));
        assertEquals(1, list.size());
        //??
        assertEquals(35, list.get(0).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

/**
 * ???RowBoundsRowBounds?count//from w  ww  .j ava  2  s.  co  m
 * ??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, ((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(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, ((Page<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(120, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.github.pagehelper.test.rowbounds.RowBoundsTest.java

License:Open Source License

@Test
public void testNamespaceWithRowBounds2() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    try {/*w ww . j a  v a  2  s  . c o m*/
        //?010?
        List<Country> list = sqlSession.selectList("selectIf", null, new RowBounds(1, 10));
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) 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, ((Page<?>) list).getTotal());
        //??
        assertEquals(101, list.get(0).getId());
        assertEquals(110, list.get(list.size() - 1).getId());
    } finally {
        sqlSession.close();
    }
}

From source file:com.glaf.core.dao.MyBatisEntityDAO.java

License:Apache License

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

    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;

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

    logger.debug("params:" + queryParams);
    logger.debug("rows size:" + rows.size());

    return page;
}