Search in sources :

Example 36 with RowBounds

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

the class DefaultResultSetHandler method handleRefCursorOutputParameter.

private void handleRefCursorOutputParameter(ResultSet rs, ParameterMapping parameterMapping, MetaObject metaParam) throws SQLException {
    if (rs == null) {
        return;
    }
    try {
        final String resultMapId = parameterMapping.getResultMapId();
        final ResultMap resultMap = configuration.getResultMap(resultMapId);
        final DefaultResultHandler resultHandler = new DefaultResultHandler(objectFactory);
        final ResultSetWrapper rsw = new ResultSetWrapper(rs, configuration);
        handleRowValues(rsw, resultMap, resultHandler, new RowBounds(), null);
        metaParam.setValue(parameterMapping.getProperty(), resultHandler.getResultList());
    } finally {
        // issue #228 (close resultsets)
        closeResultSet(rs);
    }
}
Also used : ResultMap(org.apache.ibatis.mapping.ResultMap) RowBounds(org.apache.ibatis.session.RowBounds) DefaultResultHandler(org.apache.ibatis.executor.result.DefaultResultHandler)

Example 37 with RowBounds

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

the class DefaultResultSetHandlerTest method shouldThrowExceptionWithColumnName.

@Test
public void shouldThrowExceptionWithColumnName() throws Exception {
    final MappedStatement ms = getMappedStatement();
    final RowBounds rowBounds = new RowBounds(0, 100);
    final DefaultResultSetHandler defaultResultSetHandler = new DefaultResultSetHandler(null, /*executor*/
    ms, null, /*parameterHandler*/
    null, /*resultHandler*/
    null, /*boundSql*/
    rowBounds);
    final ResultSetWrapper rsw = mock(ResultSetWrapper.class);
    when(rsw.getResultSet()).thenReturn(mock(ResultSet.class));
    final ResultMapping resultMapping = mock(ResultMapping.class);
    final TypeHandler typeHandler = mock(TypeHandler.class);
    when(resultMapping.getColumn()).thenReturn("column");
    when(resultMapping.getTypeHandler()).thenReturn(typeHandler);
    when(typeHandler.getResult(any(ResultSet.class), any(String.class))).thenThrow(new SQLException("exception"));
    List<ResultMapping> constructorMappings = Collections.singletonList(resultMapping);
    try {
        defaultResultSetHandler.createParameterizedResultObject(rsw, null, /*resultType*/
        constructorMappings, null, /*constructorArgTypes*/
        null, /*constructorArgs*/
        null);
        Assert.fail("Should have thrown ExecutorException");
    } catch (Exception e) {
        Assert.assertTrue("Expected ExecutorException", e instanceof ExecutorException);
        Assert.assertTrue("", e.getMessage().contains("mapping: " + resultMapping.toString()));
    }
}
Also used : ExecutorException(org.apache.ibatis.executor.ExecutorException) SQLException(java.sql.SQLException) ResultMapping(org.apache.ibatis.mapping.ResultMapping) ResultSet(java.sql.ResultSet) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) TypeHandler(org.apache.ibatis.type.TypeHandler) SQLException(java.sql.SQLException) ExecutorException(org.apache.ibatis.executor.ExecutorException) Test(org.junit.Test)

Example 38 with RowBounds

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

the class BindingTest method shouldSelectListOfPostsLike.

@Test
public void shouldSelectListOfPostsLike() {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        BoundBlogMapper mapper = session.getMapper(BoundBlogMapper.class);
        List<Post> posts = mapper.selectPostsLike(new RowBounds(1, 1), "%a%");
        assertEquals(1, posts.size());
    } finally {
        session.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) DraftPost(org.apache.ibatis.domain.blog.DraftPost) Post(org.apache.ibatis.domain.blog.Post) RowBounds(org.apache.ibatis.session.RowBounds) BaseDataTest(org.apache.ibatis.BaseDataTest) Test(org.junit.Test)

Example 39 with RowBounds

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

the class CursorNestedTest method testCursorWithRowBound.

@Test
public void testCursorWithRowBound() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(2, 1));
        Iterator<User> iterator = usersCursor.iterator();
        Assert.assertTrue(iterator.hasNext());
        User user = iterator.next();
        Assert.assertEquals("User3", user.getName());
        Assert.assertEquals(2, 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)

Example 40 with RowBounds

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

the class FlatJava5Test method testPKFieldsSelectByExampleBetweenWithRowbounds.

@Test
public void testPKFieldsSelectByExampleBetweenWithRowbounds() {
    SqlSession sqlSession = sqlSessionFactory.openSession();
    try {
        PkfieldsMapper mapper = sqlSession.getMapper(PkfieldsMapper.class);
        Pkfields record = new Pkfields();
        record.setFirstname("Fred");
        record.setLastname("Flintstone");
        record.setId1(1);
        record.setId2(1);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Wilma");
        record.setLastname("Flintstone");
        record.setId1(1);
        record.setId2(2);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Pebbles");
        record.setLastname("Flintstone");
        record.setId1(1);
        record.setId2(3);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Barney");
        record.setLastname("Rubble");
        record.setId1(2);
        record.setId2(1);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Betty");
        record.setLastname("Rubble");
        record.setId1(2);
        record.setId2(2);
        mapper.insert(record);
        record = new Pkfields();
        record.setFirstname("Bamm Bamm");
        record.setLastname("Rubble");
        record.setId1(2);
        record.setId2(3);
        mapper.insert(record);
        PkfieldsExample example = new PkfieldsExample();
        example.createCriteria().andId2Between(1, 3);
        example.setOrderByClause("ID1, ID2");
        RowBounds rb = new RowBounds(2, 3);
        List<Pkfields> answer = mapper.selectByExampleWithRowbounds(example, rb);
        assertEquals(3, answer.size());
        assertEquals("Pebbles", answer.get(0).getFirstname());
        assertEquals("Barney", answer.get(1).getFirstname());
        assertEquals("Betty", answer.get(2).getFirstname());
    } finally {
        sqlSession.close();
    }
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) PkfieldsMapper(mbg.test.mb3.generated.flat.mapper.PkfieldsMapper) RowBounds(org.apache.ibatis.session.RowBounds) Pkfields(mbg.test.mb3.generated.flat.model.Pkfields) PkfieldsExample(mbg.test.mb3.generated.flat.model.PkfieldsExample) Test(org.junit.Test)

Aggregations

RowBounds (org.apache.ibatis.session.RowBounds)46 Test (org.junit.Test)35 SqlSession (org.apache.ibatis.session.SqlSession)31 Country (com.github.pagehelper.model.Country)14 MappedStatement (org.apache.ibatis.mapping.MappedStatement)8 CountryMapper (com.github.pagehelper.mapper.CountryMapper)7 BaseDataTest (org.apache.ibatis.BaseDataTest)7 HashMap (java.util.HashMap)6 Executor (org.apache.ibatis.executor.Executor)5 BoundSql (org.apache.ibatis.mapping.BoundSql)5 MetaObject (org.apache.ibatis.reflection.MetaObject)5 ResultHandler (org.apache.ibatis.session.ResultHandler)5 PageInfo (com.github.pagehelper.PageInfo)4 CacheKey (org.apache.ibatis.cache.CacheKey)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)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