Example usage for org.apache.ibatis.cursor Cursor getCurrentIndex

List of usage examples for org.apache.ibatis.cursor Cursor getCurrentIndex

Introduction

In this page you can find the example usage for org.apache.ibatis.cursor Cursor getCurrentIndex.

Prototype

int getCurrentIndex();

Source Link

Document

Get the current item index.

Usage

From source file:cn.com.git.udmp.test.mybatis.cursor.UdmpCursorSimpleTest.java

License:Apache License

@Test
public void shouldGetAllUser() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    BatchTestSouceMapper mapper = sqlSession.getMapper(BatchTestSouceMapper.class);
    Cursor<BatchTestSouce> usersCursor = mapper.findListByCursor();
    try {//  w w  w . j  a  v  a  2  s  .  c o  m
        Assert.assertFalse(usersCursor.isOpen());

        // Cursor is just created, current index is -1
        Assert.assertEquals(-1, usersCursor.getCurrentIndex());

        Iterator<BatchTestSouce> iterator = usersCursor.iterator();

        // Check if hasNext, fetching is started
        Assert.assertTrue(iterator.hasNext());
        Assert.assertTrue(usersCursor.isOpen());
        Assert.assertFalse(usersCursor.isConsumed());

        // next() has not been called, index is still -1
        Assert.assertEquals(-1, usersCursor.getCurrentIndex());

        BatchTestSouce user = iterator.next();
        //            Assert.assertEquals("User1", user.getVcharC());
        System.out.println(user.getVcharC());
        Assert.assertEquals(0, usersCursor.getCurrentIndex());

        user = iterator.next();
        System.out.println(user.getVcharC());
        //            Assert.assertEquals("User2", user.getVcharC());
        Assert.assertEquals(1, usersCursor.getCurrentIndex());

        user = iterator.next();
        System.out.println(user.getVcharC());
        //            Assert.assertEquals("User3", user.getVcharC());
        Assert.assertEquals(2, usersCursor.getCurrentIndex());

        user = iterator.next();
        System.out.println(user.getVcharC());
        //            Assert.assertEquals("User4", user.getVcharC());
        Assert.assertEquals(3, usersCursor.getCurrentIndex());

        user = iterator.next();
        System.out.println(user.getVcharC());
        //            Assert.assertEquals("User5", user.getVcharC());
        Assert.assertEquals(4, usersCursor.getCurrentIndex());

        // Check no more elements
        Assert.assertFalse(iterator.hasNext());
        Assert.assertFalse(usersCursor.isOpen());
        Assert.assertTrue(usersCursor.isConsumed());
    } finally {
        sqlSession.close();
    }
}