Search in sources :

Example 16 with UserMapper

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

the class OffsetTest method testOffset.

@Test
public void testOffset() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        PageHelper.startPage(1, 6);
        List<User> list = userMapper.selectAll();
        assertEquals(6, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        PageHelper.offsetPage(6, 20);
        list = userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        System.out.println(pageInfo.toString());
        assertEquals(2, ((Page<?>) list).getPageNum());
        assertEquals(20, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        PageHelper.offsetPage(1, 180);
        list = userMapper.selectAll();
        pageInfo = new PageInfo<User>(list);
        System.out.println(pageInfo.toString());
        assertEquals(2, ((Page<?>) list).getPageNum());
        assertEquals(180, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        PageHelper.offsetPage(0, Integer.MAX_VALUE);
        list = userMapper.selectAll();
        pageInfo = new PageInfo<User>(list);
        System.out.println(pageInfo.toString());
        assertEquals(1, ((Page<?>) list).getPageNum());
        assertEquals(183, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
    } finally {
        sqlSession.close();
    }
}
Also used : UserMapper(com.github.pagehelper.mapper.UserMapper) PageInfo(com.github.pagehelper.PageInfo) User(com.github.pagehelper.model.User) SqlSession(org.apache.ibatis.session.SqlSession) Test(org.junit.Test)

Example 17 with UserMapper

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

the class PageInfoTest method testPageSize10.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testPageSize10() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<User> list = userMapper.selectAll();
        System.out.println(list);
        PageInfo<User> page = new PageInfo<User>(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(true, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(false, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        PageSerializable<User> serializable = PageSerializable.of(list);
        assertEquals(183, serializable.getTotal());
        // 获取第2页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10);
        list = userMapper.selectAll();
        page = new PageInfo<User>(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(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        // 获取第19页,10条内容,默认查询总数count
        PageHelper.startPage(19, 10);
        list = userMapper.selectAll();
        page = new PageInfo<User>(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(false, page.isIsFirstPage());
        assertEquals(true, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(false, page.isHasNextPage());
    } finally {
        sqlSession.close();
    }
}
Also used : UserMapper(com.github.pagehelper.mapper.UserMapper) PageInfo(com.github.pagehelper.PageInfo) User(com.github.pagehelper.model.User) SqlSession(org.apache.ibatis.session.SqlSession) Test(org.junit.Test)

Example 18 with UserMapper

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

the class PageInfoTest method testPageSize50.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testPageSize50() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第1页,50条内容,默认查询总数count
        PageHelper.startPage(1, 50);
        List<User> list = userMapper.selectAll();
        PageInfo<User> page = new PageInfo<User>(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(true, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(false, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        // 获取第2页,50条内容,默认查询总数count
        PageHelper.startPage(2, 50);
        list = userMapper.selectAll();
        page = new PageInfo<User>(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(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        // 获取第3页,50条内容,默认查询总数count
        PageHelper.startPage(3, 50);
        list = userMapper.selectAll();
        page = new PageInfo<User>(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(false, page.isIsFirstPage());
        assertEquals(false, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(true, page.isHasNextPage());
        // 获取第4页,50条内容,默认查询总数count
        PageHelper.startPage(4, 50);
        list = userMapper.selectAll();
        page = new PageInfo<User>(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(false, page.isIsFirstPage());
        assertEquals(true, page.isIsLastPage());
        assertEquals(true, page.isHasPreviousPage());
        assertEquals(false, page.isHasNextPage());
    } finally {
        sqlSession.close();
    }
}
Also used : UserMapper(com.github.pagehelper.mapper.UserMapper) PageInfo(com.github.pagehelper.PageInfo) User(com.github.pagehelper.model.User) SqlSession(org.apache.ibatis.session.SqlSession) Test(org.junit.Test)

Example 19 with UserMapper

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

the class TestISelect method testGroupBy2.

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

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

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

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

Example 20 with UserMapper

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

the class TestAnnotations method testMapperWithStartPage_OrderBy.

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

Aggregations

UserMapper (com.github.pagehelper.mapper.UserMapper)75 SqlSession (org.apache.ibatis.session.SqlSession)75 Test (org.junit.Test)75 User (com.github.pagehelper.model.User)73 PageInfo (com.github.pagehelper.PageInfo)17 RowBounds (org.apache.ibatis.session.RowBounds)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)3 PageRowBounds (com.github.pagehelper.PageRowBounds)2 UserExample (com.github.pagehelper.model.UserExample)2 BoundSqlInterceptor (com.github.pagehelper.BoundSqlInterceptor)1 ISelect (com.github.pagehelper.ISelect)1 ProviderMethod (com.github.pagehelper.mapper.ProviderMethod)1 UserCode (com.github.pagehelper.model.UserCode)1 UserQueryModel (com.github.pagehelper.model.UserQueryModel)1 CacheKey (org.apache.ibatis.cache.CacheKey)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1