Search in sources :

Example 71 with User

use of com.github.pagehelper.model.User in project Mybatis-PageHelper by pagehelper.

the class TestParameterList method testMapperWithStartPage.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testMapperWithStartPage() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第1页,10条内容,默认查询总数count
        List<Integer> paList = new ArrayList<Integer>();
        paList.add(1);
        paList.add(2);
        PageHelper.startPage(1, 10);
        List<User> list = userMapper.selectAllOrderByList(paList);
        assertEquals(3, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(181, ((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) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 72 with User

use of com.github.pagehelper.model.User in project Mybatis-PageHelper by pagehelper.

the class TestLeftjoin method testLeftjoin.

/**
 * left join的count查询sql特殊
 */
@Test
public void testLeftjoin() {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<User> list = userMapper.selectLeftjoin();
        assertEquals(1, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(183, ((Page<?>) list).getTotal());
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10);
        list = userMapper.selectLeftjoin();
        assertEquals(11, 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 73 with User

use of com.github.pagehelper.model.User in project Mybatis-PageHelper by pagehelper.

the class TestWith method testUnion.

/**
 * with的count查询sql特殊 - 只测试oracle,别的可能不支持
 */
@Test
public void testUnion() throws Exception {
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    // 只测试oracle
    if (!TestUtil.getXmlPath().equalsIgnoreCase("oracle")) {
        return;
    }
    try {
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10);
        List<User> list = userMapper.selectWith();
        assertEquals(151, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(33, ((Page<?>) list).getTotal());
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10);
        list = userMapper.selectWith();
        assertEquals(161, list.get(0).getId());
        assertEquals(10, list.size());
        assertEquals(33, ((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 74 with User

use of com.github.pagehelper.model.User in project Mybatis-PageHelper by pagehelper.

the class UsingDialectClassTest method test.

@Test
public void test() {
    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());
        // 获取第1页,10条内容,默认查询总数count
        PageHelper.startPage(1, 10).using("test1");
        list = userMapper.selectAll();
        page = new PageInfo<User>(list);
        assertEquals(183, list.size());
        assertEquals(183, page.getTotal());
        serializable = PageSerializable.of(list);
        assertEquals(183, serializable.getTotal());
        // 获取第2页,10条内容,默认查询总数count
        PageHelper.startPage(2, 10).using(TEST2_DIALECT_CLASS);
        list = userMapper.selectAll();
        page = new PageInfo<User>(list);
        assertEquals(100, 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 75 with User

use of com.github.pagehelper.model.User in project Mybatis-PageHelper by pagehelper.

the class PageSizeLessThenOrEqualZeroTest method testWithRowbounds.

/**
 * 使用Mapper接口调用时,使用PageHelper.startPage效果更好,不需要添加Mapper接口参数
 */
@Test
public void testWithRowbounds() {
    // 注意这里是MybatisRowBoundsHelper,会求count
    SqlSession sqlSession = MybatisHelper.getSqlSession();
    UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    try {
        // limit=0,这时候相当于用分页插件求count,但是前提必须是配置rounbounds方式求count,否则都是-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());
        // limit<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

User (com.github.pagehelper.model.User)87 SqlSession (org.apache.ibatis.session.SqlSession)87 Test (org.junit.Test)87 UserMapper (com.github.pagehelper.mapper.UserMapper)73 PageInfo (com.github.pagehelper.PageInfo)17 RowBounds (org.apache.ibatis.session.RowBounds)14 HashMap (java.util.HashMap)13 ArrayList (java.util.ArrayList)5 PageRowBounds (com.github.pagehelper.PageRowBounds)4 Map (java.util.Map)3 UserExample (com.github.pagehelper.model.UserExample)2 ISelect (com.github.pagehelper.ISelect)1 UserQueryModel (com.github.pagehelper.model.UserQueryModel)1