Search in sources :

Example 6 with UserMapper

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

the class CloseableTest method testCloseable.

@Test
public void testCloseable() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    // 下面注释代码只能在jdk7+中进行测试
    // try(Page<Object> page = PageHelper.startPage(1, 10)) {
    // int a = 10/0;
    // userMapper.selectAll();
    // Assert.fail();
    // } catch (Exception e){
    // e.printStackTrace();
    // }
    List<User> user = userMapper.selectAll();
    Assert.assertEquals(183, user.size());
    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)

Example 7 with UserMapper

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

the class CountColumnTest method testMapperWithStartPage.

@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10).countColumn("id");
        User user = new User();
        user.setName("刘");
        List<User> list = userMapper.selectLike(user);
        assertEquals(78, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(15, ((Page<?>) list).getTotal());
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10);
        list = userMapper.selectLike(user);
        assertEquals(88, list.get(0).getId());
        assertEquals(5, list.size());
        assertEquals(15, ((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)

Example 8 with UserMapper

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

the class PageSizeZeroTest method testWithStartPage.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testWithStartPage() {
    SqlSession sqlSession = MybatisPageSizeZeroHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // pageSize=0的时候查询全部结果
        PageHelper.startPage(1, 0);
        List<User> list = userMapper.selectAll();
        PageInfo<User> page = new PageInfo<User>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
        // pageSize=0的时候查询全部结果
        PageHelper.startPage(10, 0);
        list = userMapper.selectAll();
        page = new PageInfo<User>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.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 9 with UserMapper

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

the class PageSizeZeroTest method testWithRowbounds.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testWithRowbounds() {
    SqlSession sqlSession = MybatisPageSizeZeroHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // pageSize=0的时候查询全部结果
        List<User> list = userMapper.selectAll(new RowBounds(1, 0));
        PageInfo<User> page = new PageInfo<User>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
        // pageSize=0的时候查询全部结果
        PageHelper.startPage(10, 0);
        list = userMapper.selectAll(new RowBounds(1000, 0));
        page = new PageInfo<User>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.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) RowBounds(org.apache.ibatis.session.RowBounds) Test(org.junit.Test)

Example 10 with UserMapper

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

the class RowBoundsTest method testWithRowboundsAndCountTrue.

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