Search in sources :

Example 71 with UserMapper

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

the class PageSizeLessThenOrEqualZeroTest method testWithStartPage.

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

Example 72 with UserMapper

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

the class PageTest method testMapperWithStartPageAndPageSizeZeroTrue.

@Test
public void testMapperWithStartPageAndPageSizeZeroTrue() {
    SqlSession sqlSession = MybatisReasonableHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第20页,2条内容
        // 分页插件会自动改为查询最后一页
        PageHelper.startPage(1, 0, true, true, true);
        List<User> list = userMapper.selectAll();
        PageInfo<User> page = new PageInfo<User>(list);
        assertEquals(183, list.size());
        assertEquals(1, page.getStartRow());
        assertEquals(1, page.getPageNum());
        assertEquals(183, page.getTotal());
        PageHelper.startPage(1, -1, true, true, true);
        list = userMapper.selectAll();
        page = new PageInfo<User>(list);
        assertEquals(0, list.size());
        assertEquals(0, page.getStartRow());
        assertEquals(1, page.getPageNum());
        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 73 with UserMapper

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

the class PageTest method testMapperWithStartPage.

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

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

the class PageRowBoundsTest method testWithRowboundsAndCountTrue.

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

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

the class ArgumentsObjTest method testArgumentsObj.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testArgumentsObj() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        UserQueryModel queryModel = new UserQueryModel();
        queryModel.setPageNum(1);
        queryModel.setPageSize(10);
        queryModel.setOrderBy("id desc");
        List<User> list = userMapper.selectByQueryModel(queryModel);
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        queryModel.setPageNum(2);
        queryModel.setOrderBy(null);
        list = userMapper.selectByQueryModel(queryModel);
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        queryModel.setPageNum(3);
        queryModel.setPageSize(20);
        list = userMapper.selectByQueryModel(queryModel);
        assertEquals(20, 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) UserQueryModel(com.github.pagehelper.model.UserQueryModel) 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