Search in sources :

Example 11 with CountryMapper

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

the class PageInfoTest method testPageSize10.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testPageSize10() {
    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);
        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(8, page.getLastPage());
        assertEquals(true, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(false, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        //获取第2页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(2, page.getPageNum());
        assertEquals(10, page.getPageSize());
        assertEquals(11, page.getStartRow());
        assertEquals(20, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(19, page.getPages());
        assertEquals(1, page.getFirstPage());
        assertEquals(8, page.getLastPage());
        assertEquals(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        //获取第19页,10条内容,默认查询总数count
        PageHelper.startPage(19, 10);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(19, page.getPageNum());
        assertEquals(10, page.getPageSize());
        assertEquals(181, page.getStartRow());
        assertEquals(183, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(19, page.getPages());
        assertEquals(12, page.getFirstPage());
        assertEquals(19, page.getLastPage());
        assertEquals(false, page.isIsFirstPage());
        assertEquals(true, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(false, 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)

Example 12 with CountryMapper

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

the class PageInfoTest method testPageSize50.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testPageSize50() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第1页,50条内容,默认查询总数count
        PageHelper.startPage(1, 50);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(1, page.getPageNum());
        assertEquals(50, page.getPageSize());
        assertEquals(1, page.getStartRow());
        assertEquals(50, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(4, page.getPages());
        assertEquals(1, page.getFirstPage());
        assertEquals(4, 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);
        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(4, page.getLastPage());
        assertEquals(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        //获取第3页,50条内容,默认查询总数count
        PageHelper.startPage(3, 50);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(3, page.getPageNum());
        assertEquals(50, page.getPageSize());
        assertEquals(101, page.getStartRow());
        assertEquals(150, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(4, page.getPages());
        assertEquals(1, page.getFirstPage());
        assertEquals(4, page.getLastPage());
        assertEquals(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        //获取第4页,50条内容,默认查询总数count
        PageHelper.startPage(4, 50);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(4, page.getPageNum());
        assertEquals(50, page.getPageSize());
        assertEquals(151, page.getStartRow());
        assertEquals(183, page.getEndRow());
        assertEquals(183, page.getTotal());
        assertEquals(4, page.getPages());
        assertEquals(1, page.getFirstPage());
        assertEquals(4, page.getLastPage());
        assertEquals(false, page.isIsFirstPage());
        assertEquals(true, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(false, 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)

Example 13 with CountryMapper

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

the class TestISelect method testGroupBy2.

@Test
public void testGroupBy2() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    final CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        Page<Country> page = PageHelper.startPage(1, 10).doSelectPage(new ISelect() {

            @Override
            public void doSelect() {
                countryMapper.selectGroupBy();
            }
        });
        //下面是该方法的lambda用法
        //Page<Country> page = PageHelper.startPage(1, 10).setOrderBy("id desc").doSelectPage(()-> countryMapper.selectGroupBy());
        //1,'Angola','AO'
        assertEquals(1, page.get(0).getId());
        assertEquals(10, page.size());
        assertEquals(183, page.getTotal());
        PageInfo<Country> pageInfo = page.toPageInfo();
        System.out.println(pageInfo);
        pageInfo = PageHelper.startPage(1, 10).doSelectPageInfo(new ISelect() {

            @Override
            public void doSelect() {
                countryMapper.selectGroupBy();
            }
        });
        //lambda
        //pageInfo = PageHelper.startPage(1, 10).setOrderBy("id desc").doSelectPageInfo(() -> countryMapper.selectGroupBy());
        System.out.println(pageInfo);
        final Country country = new Country();
        country.setCountryname("c");
        long total = PageHelper.count(new ISelect() {

            @Override
            public void doSelect() {
                countryMapper.selectLike(country);
            }
        });
        //lambda
        //long total = PageHelper.count(()->countryMapper.selectLike(country));
        System.out.println(total);
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) ISelect(com.github.pagehelper.ISelect) Test(org.junit.Test)

Example 14 with CountryMapper

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

the class TestDynamicChoose method testMapperWithStartPage.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectChoose(1, 2);
        assertEquals(2, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(182, ((Page<?>) list).getTotal());
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        list = countryMapper.selectChoose(1, 2);
        assertEquals(2, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(182, ((Page<?>) list).getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 15 with CountryMapper

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

the class TestDynamicIf method testMapper.

/**
     * 单个POJO参数情况特殊
     */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testMapper() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        Map map = new HashMap();
        Country country = new Country();
        country.setId(1);
        map.put("country", country);
        //获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<Country> list = countryMapper.selectIf3(country);
        assertEquals(2, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(182, ((Page<?>) list).getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) CountryMapper(com.github.pagehelper.mapper.CountryMapper) Country(com.github.pagehelper.model.Country) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

CountryMapper (com.github.pagehelper.mapper.CountryMapper)57 Country (com.github.pagehelper.model.Country)57 SqlSession (org.apache.ibatis.session.SqlSession)57 Test (org.junit.Test)57 PageInfo (com.github.pagehelper.PageInfo)11 RowBounds (org.apache.ibatis.session.RowBounds)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 PageRowBounds (com.github.pagehelper.PageRowBounds)2 CountryExample (com.github.pagehelper.model.CountryExample)2 ISelect (com.github.pagehelper.ISelect)1 CountryQueryModel (com.github.pagehelper.model.CountryQueryModel)1 Map (java.util.Map)1