Search in sources :

Example 41 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 42 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 43 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 44 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 45 with RowBounds

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

the class OffsetLimitInterceptor method intercept.

public Object intercept(final Invocation invocation) throws Throwable {
    final Executor executor = (Executor) invocation.getTarget();
    final Object[] queryArgs = invocation.getArgs();
    final MappedStatement ms = (MappedStatement) queryArgs[MAPPED_STATEMENT_INDEX];
    final Object parameter = queryArgs[PARAMETER_INDEX];
    final RowBounds rowBounds = (RowBounds) queryArgs[ROWBOUNDS_INDEX];
    //DAO接口中没有传PageBounds参量
    if (!(rowBounds instanceof PageBounds)) {
        return invocation.proceed();
    }
    //DAO接口传有PageBounds参量
    PageBounds pageBounds = (PageBounds) rowBounds;
    if (pageBounds.notValid()) {
        return new PageList((List) invocation.proceed());
    }
    final Dialect dialect;
    try {
        Class clazz = Class.forName(dialectClass);
        Constructor constructor = clazz.getConstructor(MappedStatement.class, Object.class, PageBounds.class);
        dialect = (Dialect) constructor.newInstance(new Object[] { ms, parameter, pageBounds });
    } catch (Exception e) {
        throw new ClassNotFoundException("Cannot create dialect instance: " + dialectClass, e);
    }
    final BoundSql boundSql = ms.getBoundSql(parameter);
    queryArgs[MAPPED_STATEMENT_INDEX] = copyFromNewSql(ms, boundSql, dialect.getPageSQL(), dialect.getParameterMappings(), dialect.getParameterObject());
    queryArgs[PARAMETER_INDEX] = dialect.getParameterObject();
    queryArgs[ROWBOUNDS_INDEX] = new RowBounds();
    //采用同步方式,执行分页查询
    //修复bug: 提交后台线程破坏了spring 事务管理,参见TransactionSynchronizationManager
    Callable<List> queryThread = new Callable<List>() {

        public List call() throws Exception {
            return (List) invocation.proceed();
        }
    };
    Future<List> queryFuture = call(queryThread, false);
    //如果不需要count总的结果集,则直接返回分页查询结果
    if (!pageBounds.isIfCount()) {
        return new PageList(queryFuture.get());
    }
    //对总的结果集进行count
    Callable<Integer> countThread = new Callable<Integer>() {

        public Integer call() throws Exception {
            Cache cache = ms.getCache();
            Integer count = null;
            if (cache != null && ms.isUseCache() && ms.getConfiguration().isCacheEnabled()) {
                CacheKey cacheKey = executor.createCacheKey(ms, parameter, new RowBounds(), copyFromBoundSql(ms, boundSql, dialect.getCountSQL(), boundSql.getParameterMappings(), boundSql.getParameterObject()));
                count = (Integer) cache.getObject(cacheKey);
                if (count == null) {
                    count = SQLHelp.getCount(ms, parameter, boundSql, dialect);
                    cache.putObject(cacheKey, count);
                }
            } else {
                count = SQLHelp.getCount(ms, parameter, boundSql, dialect);
            }
            return count;
        }
    };
    Future<Integer> countFutrue = call(countThread, false);
    return new PageList(queryFuture.get(), countFutrue.get().intValue());
}
Also used : Constructor(java.lang.reflect.Constructor) RowBounds(org.apache.ibatis.session.RowBounds) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) Dialect(com.github.walker.mybatis.paginator.dialect.Dialect) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey) Cache(org.apache.ibatis.cache.Cache)

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