use of com.github.pagehelper.model.User in project Mybatis-PageHelper by pagehelper.
the class BasicTest method testNamespace3.
@Test
public void testNamespace3() {
SqlSession sqlSession = MybatisRowBoundsHelper.getSqlSession();
try {
Map<String, Object> map = new HashMap<String, Object>();
User user = new User();
map.put("user", user);
// 同时测试不可变Map
map = Collections.unmodifiableMap(map);
List<User> list = sqlSession.selectList("select1", map, new RowBounds(1, 10));
assertEquals(10, list.size());
// 判断查询结果的位置是否正确
assertEquals(1, list.get(0).getId());
map = new HashMap<String, Object>();
user = new User();
user.setName("刘睿");
map.put("user", user);
// 同时测试不可变Map
map = Collections.unmodifiableMap(map);
list = sqlSession.selectList("select1", map, new RowBounds(1, 10));
assertEquals(1, list.size());
// 判断查询结果的位置是否正确
assertEquals(78, list.get(0).getId());
} finally {
sqlSession.close();
}
}
use of com.github.pagehelper.model.User 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();
}
}
use of com.github.pagehelper.model.User 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();
}
}
use of com.github.pagehelper.model.User 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();
}
}
use of com.github.pagehelper.model.User 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();
}
}
Aggregations