Search in sources :

Example 6 with PageInfo

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

the class RowBoundsTest method testWithRowboundsAndCountTrue.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testWithRowboundsAndCountTrue() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //limit=0,这时候相当于用分页插件求count,但是前提必须是配置rounbounds方式求count,否则都是-1
        //这里由于没有配置,应该都是-1
        List<Country> list = countryMapper.selectAll(new RowBounds(1, -1));
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
        //pageSize<0的时候同上
        list = countryMapper.selectAll(new RowBounds(1, -100));
        page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) RowBounds(org.apache.ibatis.session.RowBounds) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 7 with PageInfo

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

the class PageSizeLessThenOrEqualZeroTest method testWithStartPage.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //pageSize=0,这时候相当于用分页插件求count
        PageHelper.startPage(1, 0);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
        //limit<0的时候同上
        PageHelper.startPage(1, -100);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
    } 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 8 with PageInfo

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

the class PageSizeLessThenOrEqualZeroTest method testWithRowbounds.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testWithRowbounds() {
    //注意这里是MybatisRowBoundsHelper,会求count
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //limit=0,这时候相当于用分页插件求count,但是前提必须是配置rounbounds方式求count,否则都是-1
        List<Country> list = countryMapper.selectAll(new RowBounds(1, -1));
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
        //limit<0的时候同上
        list = countryMapper.selectAll(new RowBounds(1, -100));
        page = new PageInfo<Country>(list);
        assertEquals(0, list.size());
        assertEquals(183, page.getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) RowBounds(org.apache.ibatis.session.RowBounds) Country(com.github.pagehelper.model.Country) Test(org.junit.Test)

Example 9 with PageInfo

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

the class PageTest method testMapperWithStartPage.

/**
     * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
     */
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisReasonableHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第20页,2条内容
        //分页插件会自动改为查询最后一页
        PageHelper.startPage(20, 50);
        List<Country> list = countryMapper.selectAll();
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(33, list.size());
        assertEquals(151, page.getStartRow());
        assertEquals(4, page.getPageNum());
        assertEquals(183, page.getTotal());
        //获取第-3页,2条内容
        //由于只有7天数据,分页插件会自动改为查询最后一页
        PageHelper.startPage(-3, 50);
        list = countryMapper.selectAll();
        page = new PageInfo<Country>(list);
        assertEquals(50, list.size());
        assertEquals(1, page.getStartRow());
        assertEquals(1, page.getPageNum());
        assertEquals(183, page.getTotal());
    } 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 10 with PageInfo

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

the class RowBoundsTest method testMapperWithRowBounds.

/**
     * 使用Mapper接口调用时,对接口增加RowBounds参数,不需要修改对应的xml配置(或注解配置)
     * <p/>
     * RowBounds方式不进行count查询,可以通过修改Page代码实现
     * <p/>
     * 这种情况下如果同时使用startPage方法,以startPage为准
     */
@Test
public void testMapperWithRowBounds() {
    SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
    CountryMapper countryMapper = sqlSession.getMapper(CountryMapper.class);
    try {
        //获取第1页,10条内容,默认查询总数count
        List<Country> list = countryMapper.selectAll(new RowBounds(1, 10));
        //新增PageInfo对象,对返回结果进行封装
        PageInfo<Country> page = new PageInfo<Country>(list);
        assertEquals(10, list.size());
        assertEquals(183, page.getTotal());
        //判断查询结果的位置是否正确
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.get(list.size() - 1).getId());
        //获取第10页,10条内容,显式查询总数count
        list = countryMapper.selectAll(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());
        //获取第3页,20条内容,默认查询总数count
        list = countryMapper.selectAll(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();
    }
}
Also used : PageInfo(com.github.pagehelper.PageInfo) SqlSession(org.apache.ibatis.session.SqlSession) CountryMapper(com.github.pagehelper.mapper.CountryMapper) RowBounds(org.apache.ibatis.session.RowBounds) 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