Search in sources :

Example 11 with PageInfo

use of com.github.pagehelper.PageInfo in project Mybatis-PageHelper by pagehelper.

the class PageInfoTest method testNavigatePages.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testNavigatePages() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list, 20);
        assertEquals(1, page.getPageNum());
        assertEquals(10, page.getPageSize());
        assertEquals(1, page.getStartRow());
        assertEquals(10, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(19, page.getPages());
        assertEquals(1, page.getFirstPage());
        assertEquals(19, page.getLastPage());
        assertEquals(true, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(false, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        //获取第2页,50条内容,默认查询总数count
        PageHelper.startPage(2, 50);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list, 2);
        assertEquals(2, page.getPageNum());
        assertEquals(50, page.getPageSize());
        assertEquals(51, page.getStartRow());
        assertEquals(100, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(4, page.getPages());
        assertEquals(1, page.getFirstPage());
        assertEquals(2, page.getLastPage());
        assertEquals(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
    } finally {
        sqlSession.close();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Aggregations

PageInfo (com.github.pagehelper.PageInfo)11 CountryMapper (com.github.pagehelper.mapper.CountryMapper)11 Country (com.github.pagehelper.model.Country)11 SqlSession (org.apache.ibatis.session.SqlSession)11 Test (org.junit.Test)11 RowBounds (org.apache.ibatis.session.RowBounds)4