Search in sources :

Example 11 with RowBounds

use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.

the class NestedResultHandlerAssociationTest method shouldHandleRowBounds.

@Test
public void shouldHandleRowBounds() throws Exception {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    final SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    Date targetMonth = fmt.parse("2014-01-01");
    final List<Account> accounts = new ArrayList<Account>();
    try {
        sqlSession.select("collectPageByBirthMonth", targetMonth, new RowBounds(1, 2), new ResultHandler() {

            @Override
            public void handleResult(ResultContext context) {
                Account account = (Account) context.getResultObject();
                accounts.add(account);
            }
        });
    } finally {
        sqlSession.close();
    }
    assertEquals(2, accounts.size());
    assertEquals("Bob2", accounts.get(0).getAccountName());
    assertEquals("Bob3", accounts.get(1).getAccountName());
}
Also used : ResultContext(org.apache.ibatis.session.ResultContext) SqlSession(org.apache.ibatis.session.SqlSession) ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 12 with RowBounds

use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.

the class DuplicateStatementsTest method shouldGetFirstFourUsers_Annotated.

@Test
@Ignore("fails currently - issue 507")
public void shouldGetFirstFourUsers_Annotated() {
    sqlSessionFactory.getConfiguration().addMapper(AnnotatedMapper.class);
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
        List<User> users = mapper.getAllUsers(new RowBounds(0, 4));
        Assert.assertEquals(4, users.size());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) RowBounds(org.apache.ibatis.session.RowBounds) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with RowBounds

use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.

the class DuplicateStatementsTest method shouldGetFirstFourUsers.

@Test
public void shouldGetFirstFourUsers() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        Mapper mapper = sqlSession.getMapper(Mapper.class);
        List<User> users = mapper.getAllUsers(new RowBounds(0, 4));
        Assert.assertEquals(4, users.size());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) RowBounds(org.apache.ibatis.session.RowBounds) Test(org.junit.Test)

Example 14 with RowBounds

use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.

the class CursorSimpleTest method testCursorWithBadRowBound.

@Test
public void testCursorWithBadRowBound() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        // Trying to start at offset 10 (which does not exist, since there is only 4 items)
        Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(10, 2));
        Iterator<User> iterator = usersCursor.iterator();
        Assert.assertFalse(iterator.hasNext());
        Assert.assertFalse(usersCursor.isOpen());
        Assert.assertTrue(usersCursor.isConsumed());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) RowBounds(org.apache.ibatis.session.RowBounds) Test(org.junit.Test)

Example 15 with RowBounds

use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.

the class CursorSimpleTest method testCursorWithRowBound.

@Test
public void testCursorWithRowBound() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        // RowBound starting at offset 1 and limiting to 2 items
        Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 3));
        Iterator<User> iterator = usersCursor.iterator();
        User user = iterator.next();
        Assert.assertEquals("User2", user.getName());
        Assert.assertEquals(1, usersCursor.getCurrentIndex());
        // Calling hasNext() before next()
        Assert.assertTrue(iterator.hasNext());
        user = iterator.next();
        Assert.assertEquals("User3", user.getName());
        Assert.assertEquals(2, usersCursor.getCurrentIndex());
        // Calling next() without a previous hasNext() call
        user = iterator.next();
        Assert.assertEquals("User4", user.getName());
        Assert.assertEquals(3, usersCursor.getCurrentIndex());
        Assert.assertFalse(iterator.hasNext());
        Assert.assertFalse(usersCursor.isOpen());
        Assert.assertTrue(usersCursor.isConsumed());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) RowBounds(org.apache.ibatis.session.RowBounds) Test(org.junit.Test)

Aggregations

RowBounds (org.apache.ibatis.session.RowBounds)45 Test (org.junit.Test)35 SqlSession (org.apache.ibatis.session.SqlSession)31 Country (com.github.pagehelper.model.Country)14 CountryMapper (com.github.pagehelper.mapper.CountryMapper)7 BaseDataTest (org.apache.ibatis.BaseDataTest)7 MappedStatement (org.apache.ibatis.mapping.MappedStatement)7 HashMap (java.util.HashMap)6 PageInfo (com.github.pagehelper.PageInfo)4 Executor (org.apache.ibatis.executor.Executor)4 BoundSql (org.apache.ibatis.mapping.BoundSql)4 MetaObject (org.apache.ibatis.reflection.MetaObject)4 ResultHandler (org.apache.ibatis.session.ResultHandler)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 CacheKey (org.apache.ibatis.cache.CacheKey)3 Blog (org.apache.ibatis.domain.blog.Blog)3 DraftPost (org.apache.ibatis.domain.blog.DraftPost)3 Post (org.apache.ibatis.domain.blog.Post)3