Search in sources :

Example 11 with UserMapper

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

the class TestParameterNone method testMapperWithStartPage.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<User> list = userMapper.selectAll();
        assertEquals(1, 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)

Example 12 with UserMapper

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

the class TestProviderInteceptor method testInterceptor.

@Test
public void testInterceptor() {
    SqlSession sqlSession = MybatisInterceptorHelper.getSqlSession();
    final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        PageHelper.startPage(1, 10).boundSqlInterceptor(new BoundSqlInterceptor() {

            @Override
            public BoundSql boundSql(Type type, BoundSql boundSql, CacheKey cacheKey, Chain chain) {
                System.out.println("[" + Thread.currentThread().getName() + "] - before: " + boundSql.getSql());
                BoundSql doBoundSql = chain.doBoundSql(type, boundSql, cacheKey);
                System.out.println("[" + Thread.currentThread().getName() + "] - after: " + doBoundSql.getSql());
                if (type == Type.ORIGINAL) {
                    Assert.assertTrue(doBoundSql.getSql().contains(TestBoundSqlInterceptor.COMMENT));
                }
                return doBoundSql;
            }
        });
        final String str = "飞";
        userMapper.selectSimple(str);
        assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());
        userMapper.selectSimple(str);
        assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());
        userMapper.selectSimple(str);
        assertEquals(new ProviderMethod().selectSimple(str), SqlCache.get());
    } finally {
        SqlCache.remove();
        sqlSession.close();
    }
}
Also used : UserMapper(com.github.pagehelper.mapper.UserMapper) SqlSession(org.apache.ibatis.session.SqlSession) BoundSql(org.apache.ibatis.mapping.BoundSql) BoundSqlInterceptor(com.github.pagehelper.BoundSqlInterceptor) ProviderMethod(com.github.pagehelper.mapper.ProviderMethod) CacheKey(org.apache.ibatis.cache.CacheKey) Test(org.junit.Test)

Example 13 with UserMapper

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

the class AutoDialectTest method test.

@Test
public void test() {
    SqlSession sqlSession = MybatisAutoDialectHelper.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 14 with UserMapper

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

the class PageRowBoundsTest method testMapperWithPageRowBounds.

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

Example 15 with UserMapper

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

the class OffsetTest method testPageNum.

@Test
public void testPageNum() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        PageHelper.offsetPage(5, 5);
        List<User> list = userMapper.selectAll();
        assertEquals(2, ((Page<?>) list).getPageNum());
        assertEquals(5, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        PageHelper.offsetPage(15, 5);
        list = userMapper.selectAll();
        assertEquals(4, ((Page<?>) list).getPageNum());
        assertEquals(5, 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