use of org.apache.ibatis.session.ResultHandler in project sonarqube by SonarSource.
the class RuleDaoTest method selectEnabled_with_ResultHandler.
@Test
public void selectEnabled_with_ResultHandler() {
dbTester.prepareDbUnit(getClass(), "selectEnabled.xml");
final List<RuleDto> rules = new ArrayList<>();
ResultHandler resultHandler = new ResultHandler() {
@Override
public void handleResult(ResultContext resultContext) {
rules.add((RuleDto) resultContext.getResultObject());
}
};
underTest.selectEnabled(dbTester.getSession(), resultHandler);
assertThat(rules.size()).isEqualTo(1);
RuleDto ruleDto = rules.get(0);
assertThat(ruleDto.getId()).isEqualTo(1);
}
use of org.apache.ibatis.session.ResultHandler in project mybatis-3 by mybatis.
the class CommonPropertyDeferLoadError method testDeferLoadAfterResultHandler.
@Test
public void testDeferLoadAfterResultHandler() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
class MyResultHandler implements ResultHandler {
List<Child> children = new ArrayList<Child>();
@Override
public void handleResult(ResultContext context) {
Child child = (Child) context.getResultObject();
children.add(child);
}
}
;
MyResultHandler myResultHandler = new MyResultHandler();
sqlSession.select("org.apache.ibatis.submitted.deferload_common_property.ChildMapper.selectAll", myResultHandler);
for (Child child : myResultHandler.children) {
assertNotNull(child.getFather());
}
} finally {
sqlSession.close();
}
}
use of org.apache.ibatis.session.ResultHandler 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];
}
checkDialectExists();
// 对 boundSql 的拦截处理
if (dialect instanceof BoundSqlInterceptor.Chain) {
boundSql = ((BoundSqlInterceptor.Chain) dialect).doBoundSql(BoundSqlInterceptor.Type.ORIGINAL, boundSql, cacheKey);
}
List resultList;
// 调用方法判断是否需要进行分页,如果不需要,直接返回结果
if (!dialect.skip(ms, parameter, rowBounds)) {
// 判断是否需要进行 count 查询
if (dialect.beforeCount(ms, parameter, rowBounds)) {
// 查询总数
Long count = count(executor, ms, parameter, rowBounds, null, boundSql);
// 处理查询总数,返回 true 时继续分页查询,false 时直接返回
if (!dialect.afterCount(count, parameter, rowBounds)) {
// 当查询总数为 0 时,直接返回空的结果
return dialect.afterPage(new ArrayList(), parameter, rowBounds);
}
}
resultList = ExecutorUtil.pageQuery(dialect, executor, ms, parameter, rowBounds, resultHandler, boundSql, cacheKey);
} else {
// rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页
resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
return dialect.afterPage(resultList, parameter, rowBounds);
} finally {
if (dialect != null) {
dialect.afterAll();
}
}
}
use of org.apache.ibatis.session.ResultHandler 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.ResultHandler in project mybatis-3 by mybatis.
the class DefaultResultSetHandlerTest method shouldRetainColumnNameCase.
/**
* Contrary to the spec, some drivers require case-sensitive column names when getting result.
*
* @see <a href="https://github.com/mybatis/old-google-code-issues/issues/557">Issue 557</a>
*/
@Test
void shouldRetainColumnNameCase() throws Exception {
final MappedStatement ms = getMappedStatement();
final Executor executor = null;
final ParameterHandler parameterHandler = null;
final ResultHandler resultHandler = null;
final BoundSql boundSql = null;
final RowBounds rowBounds = new RowBounds(0, 100);
final DefaultResultSetHandler fastResultSetHandler = new DefaultResultSetHandler(executor, ms, parameterHandler, resultHandler, boundSql, rowBounds);
when(stmt.getResultSet()).thenReturn(rs);
when(rs.getMetaData()).thenReturn(rsmd);
when(rs.getType()).thenReturn(ResultSet.TYPE_FORWARD_ONLY);
when(rs.next()).thenReturn(true).thenReturn(false);
when(rs.getInt("CoLuMn1")).thenReturn(100);
when(rsmd.getColumnCount()).thenReturn(1);
when(rsmd.getColumnLabel(1)).thenReturn("CoLuMn1");
when(rsmd.getColumnType(1)).thenReturn(Types.INTEGER);
when(rsmd.getColumnClassName(1)).thenReturn(Integer.class.getCanonicalName());
when(stmt.getConnection()).thenReturn(conn);
when(conn.getMetaData()).thenReturn(dbmd);
// for simplicity.
when(dbmd.supportsMultipleResultSets()).thenReturn(false);
final List<Object> results = fastResultSetHandler.handleResultSets(stmt);
assertEquals(1, results.size());
assertEquals(100, ((HashMap) results.get(0)).get("cOlUmN1"));
}
Aggregations