use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.
the class CursorSimpleTest method testCursorIteratorNoSuchElementExceptionWithHasNext.
@Test
public void testCursorIteratorNoSuchElementExceptionWithHasNext() {
SqlSession sqlSession = sqlSessionFactory.openSession();
Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 1));
try {
Iterator<User> iterator = usersCursor.iterator();
User user = iterator.next();
Assert.assertEquals("User2", user.getName());
Assert.assertEquals(1, usersCursor.getCurrentIndex());
Assert.assertFalse(iterator.hasNext());
iterator.next();
Assert.fail("We should have failed since we call next() when hasNext() returned false");
} catch (NoSuchElementException e) {
Assert.assertFalse(usersCursor.isOpen());
Assert.assertTrue(usersCursor.isConsumed());
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.RowBounds in project mybatis-3 by mybatis.
the class CursorSimpleTest method testCursorIteratorNoSuchElementExceptionNoHasNext.
@Test
public void testCursorIteratorNoSuchElementExceptionNoHasNext() {
SqlSession sqlSession = sqlSessionFactory.openSession();
Cursor<User> usersCursor = sqlSession.selectCursor("getAllUsers", null, new RowBounds(1, 1));
try {
Iterator<User> iterator = usersCursor.iterator();
User user = iterator.next();
Assert.assertEquals("User2", user.getName());
Assert.assertEquals(1, usersCursor.getCurrentIndex());
// Trying next() without hasNext()
iterator.next();
Assert.fail("We should have failed since we call next() when is no more items");
} catch (NoSuchElementException e) {
Assert.assertFalse(usersCursor.isOpen());
Assert.assertTrue(usersCursor.isConsumed());
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.RowBounds in project Mybatis-PageHelper by pagehelper.
the class PageInterceptor method intercept.
@Override
public Object intercept(Invocation invocation) throws Throwable {
try {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
//由于逻辑关系,只会进入一次
if (args.length == 4) {
//4 个参数时
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
//6 个参数时
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
List resultList;
//调用方法判断是否需要进行分页,如果不需要,直接返回结果
if (!dialect.skip(ms, parameter, rowBounds)) {
//反射获取动态参数
Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);
//判断是否需要进行 count 查询
if (dialect.beforeCount(ms, parameter, rowBounds)) {
//创建 count 查询的缓存 key
CacheKey countKey = executor.createCacheKey(ms, parameter, RowBounds.DEFAULT, boundSql);
countKey.update(MSUtils.COUNT);
MappedStatement countMs = msCountMap.get(countKey);
if (countMs == null) {
//根据当前的 ms 创建一个返回值为 Long 类型的 ms
countMs = MSUtils.newCountMappedStatement(ms);
msCountMap.put(countKey, countMs);
}
//调用方言获取 count sql
String countSql = dialect.getCountSql(ms, boundSql, parameter, rowBounds, countKey);
BoundSql countBoundSql = new BoundSql(ms.getConfiguration(), countSql, boundSql.getParameterMappings(), parameter);
//当使用动态 SQL 时,可能会产生临时的参数,这些参数需要手动设置到新的 BoundSql 中
for (String key : additionalParameters.keySet()) {
countBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
}
//执行 count 查询
Object countResultList = executor.query(countMs, parameter, RowBounds.DEFAULT, resultHandler, countKey, countBoundSql);
Long count = (Long) ((List) countResultList).get(0);
//返回 true 时继续分页查询,false 时直接返回
if (!dialect.afterCount(count, parameter, rowBounds)) {
//当查询总数为 0 时,直接返回空的结果
return dialect.afterPage(new ArrayList(), parameter, rowBounds);
}
}
//判断是否需要进行分页查询
if (dialect.beforePage(ms, parameter, rowBounds)) {
//生成分页的缓存 key
CacheKey pageKey = cacheKey;
//处理参数对象
parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
//调用方言获取分页 sql
String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
BoundSql pageBoundSql = new BoundSql(ms.getConfiguration(), pageSql, boundSql.getParameterMappings(), parameter);
//设置动态参数
for (String key : additionalParameters.keySet()) {
pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
}
//执行分页查询
resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
} else {
//不执行分页的情况下,也不执行内存分页
resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
}
} else {
//rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页
resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
return dialect.afterPage(resultList, parameter, rowBounds);
} finally {
dialect.afterAll();
}
}
use of org.apache.ibatis.session.RowBounds in project Mybatis-PageHelper by pagehelper.
the class QueryInterceptor method intercept.
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
//由于逻辑关系,只会进入一次
if (args.length == 4) {
//4 个参数时
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
//6 个参数时
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
//注:下面的方法可以根据自己的逻辑调用多次,在分页插件中,count 和 page 各调用了一次
return executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
use of org.apache.ibatis.session.RowBounds in project Mybatis-PageHelper by pagehelper.
the class RowBoundsTest method testNamespaceWithRowBounds2.
@Test
public void testNamespaceWithRowBounds2() {
SqlSession sqlSession = RowBoundsHelper.getSqlSession();
try {
//获取从0开始,10条内容
List<Country> list = sqlSession.selectList("selectIf", null, new RowBounds(0, 10));
assertEquals(10, list.size());
//判断查询结果的位置是否正确
assertEquals(1, list.get(0).getId());
assertEquals(10, list.get(list.size() - 1).getId());
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 10);
//获取从10开始,10条内容
list = sqlSession.selectList("selectIf", map, new RowBounds(90, 10));
assertEquals(10, list.size());
//判断查询结果的位置是否正确
assertEquals(101, list.get(0).getId());
assertEquals(110, list.get(list.size() - 1).getId());
} finally {
sqlSession.close();
}
}
Aggregations