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);
}
}
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()));
}
}
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();
}
}
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();
}
}
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());
}
Aggregations