Search in sources :

Example 1 with ResultHandler

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);
}
Also used : ResultContext(org.apache.ibatis.session.ResultContext) ArrayList(java.util.ArrayList) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test)

Example 2 with ResultHandler

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();
    }
}
Also used : ResultContext(org.apache.ibatis.session.ResultContext) SqlSession(org.apache.ibatis.session.SqlSession) ArrayList(java.util.ArrayList) List(java.util.List) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.Test)

Example 3 with ResultHandler

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();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) RowBounds(org.apache.ibatis.session.RowBounds) ResultHandler(org.apache.ibatis.session.ResultHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) ArrayList(java.util.ArrayList) List(java.util.List) MappedStatement(org.apache.ibatis.mapping.MappedStatement) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 4 with ResultHandler

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);
}
Also used : Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) CacheKey(org.apache.ibatis.cache.CacheKey)

Example 5 with ResultHandler

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"));
}
Also used : ParameterHandler(org.apache.ibatis.executor.parameter.ParameterHandler) Executor(org.apache.ibatis.executor.Executor) BoundSql(org.apache.ibatis.mapping.BoundSql) RowBounds(org.apache.ibatis.session.RowBounds) MappedStatement(org.apache.ibatis.mapping.MappedStatement) ResultHandler(org.apache.ibatis.session.ResultHandler) Test(org.junit.jupiter.api.Test)

Aggregations

ResultHandler (org.apache.ibatis.session.ResultHandler)33 ResultContext (org.apache.ibatis.session.ResultContext)25 ArrayList (java.util.ArrayList)20 List (java.util.List)14 Test (org.junit.Test)13 SqlSession (org.apache.ibatis.session.SqlSession)10 RowBounds (org.apache.ibatis.session.RowBounds)8 Random (java.util.Random)7 Consumer (java.util.function.Consumer)7 RandomStringUtils.randomAlphabetic (org.apache.commons.lang.RandomStringUtils.randomAlphabetic)7 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)6 DataProviderRunner (com.tngtech.java.junit.dataprovider.DataProviderRunner)6 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)6 Arrays (java.util.Arrays)6 Date (java.util.Date)6 HashMap (java.util.HashMap)6 IntStream (java.util.stream.IntStream)6 Executor (org.apache.ibatis.executor.Executor)6 BoundSql (org.apache.ibatis.mapping.BoundSql)6