org.apache.ibatis.executor.result.DefaultResultHandler Java Examples

The following examples show how to use org.apache.ibatis.executor.result.DefaultResultHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BindingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerInSameSession() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example #2
Source File: BindingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerInSameSession() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example #3
Source File: BindingTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerBetweenSessions() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    //new session
    session.close();
    session = sqlSessionFactory.openSession();

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example #4
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 6 votes vote down vote up
private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
  try {
    if (parentMapping != null) {
      handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
    } else {
      if (resultHandler == null) {
        //如果没有resultHandler
        //新建DefaultResultHandler
        DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
        //调用自己的handleRowValues
        handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
        //得到记录的list
        multipleResults.add(defaultResultHandler.getResultList());
      } else {
        //如果有resultHandler
        handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
      }
    }
  } finally {
    //最后别忘了关闭结果集,这个居然出bug了
    // issue #228 (close resultsets)
    closeResultSet(rsw.getResultSet());
  }
}
 
Example #5
Source File: SqlSessionTestBase.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
protected final void testAndVerifySelect() throws Exception {
    // Given
    final String selectId = "selectId";
    SqlSession sqlSession = getSqlSession();
    // When
    sqlSession.select(selectId, new DefaultResultHandler());
    sqlSession.select(selectId, new Object(), new DefaultResultHandler());
    sqlSession.select(selectId, new Object(), RowBounds.DEFAULT, new DefaultResultHandler());
    // Then
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    Method select1 = sqlSession.getClass().getDeclaredMethod("select", String.class, ResultHandler.class);
    verifier.verifyTrace(event("MYBATIS", select1, Expectations.cachedArgs(selectId)));
    Method select2 = sqlSession.getClass().getDeclaredMethod("select", String.class, Object.class,
            ResultHandler.class);
    verifier.verifyTrace(event("MYBATIS", select2, Expectations.cachedArgs(selectId)));
    Method select3 = sqlSession.getClass().getDeclaredMethod("select", String.class, Object.class, RowBounds.class,
            ResultHandler.class);
    verifier.verifyTrace(event("MYBATIS", select3, Expectations.cachedArgs(selectId)));
}
 
Example #6
Source File: BindingTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldExecuteMultipleBoundSelectOfBlogsByIdInWithProvidedResultHandlerBetweenSessions() {
  SqlSession session = sqlSessionFactory.openSession();
  try {
    final DefaultResultHandler handler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", handler);

    //new session
    session.close();
    session = sqlSessionFactory.openSession();

    final DefaultResultHandler moreHandler = new DefaultResultHandler();
    session.select("selectBlogsAsMapById", moreHandler);

    assertEquals(2, handler.getResultList().size());
    assertEquals(2, moreHandler.getResultList().size());

  } finally {
    session.close();
  }
}
 
Example #7
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 6 votes vote down vote up
private void handleResultSet(ResultSetWrapper rsw, ResultMap resultMap, List<Object> multipleResults, ResultMapping parentMapping) throws SQLException {
  try {
    if (parentMapping != null) {
      handleRowValues(rsw, resultMap, null, RowBounds.DEFAULT, parentMapping);
    } else {
      if (resultHandler == null) {
        //如果没有resultHandler
        //新建DefaultResultHandler
        DefaultResultHandler defaultResultHandler = new DefaultResultHandler(objectFactory);
        //调用自己的handleRowValues
        handleRowValues(rsw, resultMap, defaultResultHandler, rowBounds, null);
        //得到记录的list
        multipleResults.add(defaultResultHandler.getResultList());
      } else {
        //如果有resultHandler
        handleRowValues(rsw, resultMap, resultHandler, rowBounds, null);
      }
    }
  } finally {
    //最后别忘了关闭结果集,这个居然出bug了
    // issue #228 (close resultsets)
    closeResultSet(rsw.getResultSet());
  }
}
 
Example #8
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSelectAuthorsUsingMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAllAuthors(handler);
    assertEquals(2, handler.getResultList().size());
  } finally {
    session.close();
  }
}
 
Example #9
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    DefaultResultHandler handler1 = new DefaultResultHandler();
    DefaultResultHandler handler2 = new DefaultResultHandler();
    AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
    mapper.selectAuthor(101, handler1, handler2);
  } finally {
    sqlSession.close();
  }
}
 
Example #10
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test(expected=BindingException.class)
public void shouldFailExecutingAnAnnotatedMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAuthor2(101, handler);
    Author author = (Author) handler.getResultList().get(0);
    assertEquals(101, author.getId());
  } finally {
    session.close();
  }
}
 
Example #11
Source File: BlogMain.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void selectHandler(SqlSession session, Configuration configuration) {
	BlogMapper mapper = session.getMapper(BlogMapper.class);
	// DefaultResultHandler内置结果处理器
	DefaultResultHandler defaultHandler = new DefaultResultHandler();
	// System.out.println(mapper.selectBlogsByHandler("zhaohui",
	// defaultHandler));
	System.out.println(defaultHandler.getResultList());

	// DefaultMapResultHandler内置结果处理器
	DefaultMapResultHandler<Long, Blog> defaultMapResultHandler = new DefaultMapResultHandler<Long, Blog>("id",
			configuration.getObjectFactory(), configuration.getObjectWrapperFactory(),
			configuration.getReflectorFactory());
	mapper.selectBlogsByHandler("zhaohui", defaultMapResultHandler);
	System.out.println(defaultMapResultHandler.getMappedResults());
}
 
Example #12
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExecuteSelectOneAuthorUsingMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAuthor(101, handler);
    Author author = (Author) handler.getResultList().get(0);
    assertEquals(101, author.getId());
  } finally {
    session.close();
  }
}
 
Example #13
Source File: DefaultResultSetHandler.java    From mybatis with Apache License 2.0 5 votes vote down vote up
private void handleRefCursorOutputParameter(ResultSet rs, ParameterMapping parameterMapping, MetaObject metaParam) throws SQLException {
  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);
    //里面就和一般ResultSet处理没两样了
    handleRowValues(rsw, resultMap, resultHandler, new RowBounds(), null);
    metaParam.setValue(parameterMapping.getProperty(), resultHandler.getResultList());
  } finally {
    // issue #228 (close resultsets)
    closeResultSet(rs);
  }
}
 
Example #14
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExecuteSelectOneAuthorUsingMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAuthor(101, handler);
    Author author = (Author) handler.getResultList().get(0);
    assertEquals(101, author.getId());
  } finally {
    session.close();
  }
}
 
Example #15
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected=BindingException.class)
public void shouldFailExecutingAnAnnotatedMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAuthor2(101, handler);
    Author author = (Author) handler.getResultList().get(0);
    assertEquals(101, author.getId());
  } finally {
    session.close();
  }
}
 
Example #16
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSelectAuthorsUsingMapperClassWithResultHandler() {
  SqlSession session = sqlMapper.openSession();
  try {
    DefaultResultHandler handler = new DefaultResultHandler();
    AuthorMapper mapper = session.getMapper(AuthorMapper.class);
    mapper.selectAllAuthors(handler);
    assertEquals(2, handler.getResultList().size());
  } finally {
    session.close();
  }
}
 
Example #17
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected = BindingException.class)
public void shouldFailSelectOneAuthorUsingMapperClassWithTwoResultHandlers() {
  Configuration configuration = new Configuration(sqlMapper.getConfiguration().getEnvironment());
  configuration.addMapper(AuthorMapperWithMultipleHandlers.class);
  SqlSessionFactory sqlMapperWithMultipleHandlers = new DefaultSqlSessionFactory(configuration);
  SqlSession sqlSession = sqlMapperWithMultipleHandlers.openSession();
  try {
    DefaultResultHandler handler1 = new DefaultResultHandler();
    DefaultResultHandler handler2 = new DefaultResultHandler();
    AuthorMapperWithMultipleHandlers mapper = sqlSession.getMapper(AuthorMapperWithMultipleHandlers.class);
    mapper.selectAuthor(101, handler1, handler2);
  } finally {
    sqlSession.close();
  }
}
 
Example #18
Source File: DefaultResultSetHandler.java    From mybaties with Apache License 2.0 5 votes vote down vote up
private void handleRefCursorOutputParameter(ResultSet rs, ParameterMapping parameterMapping, MetaObject metaParam) throws SQLException {
  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);
    //里面就和一般ResultSet处理没两样了
    handleRowValues(rsw, resultMap, resultHandler, new RowBounds(), null);
    metaParam.setValue(parameterMapping.getProperty(), resultHandler.getResultList());
  } finally {
    // issue #228 (close resultsets)
    closeResultSet(rs);
  }
}
 
Example #19
Source File: CacheHandler.java    From azeroth with Apache License 2.0 4 votes vote down vote up
/**
 * 按更新的查询条件更新缓存
 * @param executor
 * @param mt
 * @param mapperNameSpace
 * @param args
 */
private void removeCacheByUpdateConditon(Executor executor, MappedStatement mt,
                                         String mapperNameSpace, Object[] args) {
    try {
        Object parameterObject = args[1];
        EntityInfo entityInfo = MybatisMapperParser.getEntityInfoByMapper(mapperNameSpace);
        MappedStatement statement = getQueryIdsMappedStatementForUpdateCache(mt, entityInfo);
        if (statement == null) { return; }

        String querySql = statement.getSqlSource().getBoundSql(parameterObject).getSql();

        List<?> idsResult = null;
        if (PARSE_SQL_ERROR_DEFAULT.equals(querySql)) {
            BoundSql boundSql = mt.getBoundSql(parameterObject);
            querySql = "select " + entityInfo.getIdColumn() + " from "
                    + entityInfo.getTableName() + " WHERE "
                    + boundSql.getSql().split(WHERE_REGEX)[1];
            BoundSql queryBoundSql = new BoundSql(statement.getConfiguration(), querySql,
                    boundSql.getParameterMappings(), parameterObject);

            idsResult = executor.query(statement, parameterObject, RowBounds.DEFAULT,
                    new DefaultResultHandler(), null, queryBoundSql);
        } else {
            idsResult = executor.query(statement, parameterObject, RowBounds.DEFAULT, null);
        }

        if (idsResult != null && !idsResult.isEmpty()) {
            for (Object id : idsResult) {
                String cacheKey = entityInfo.getEntityClass().getSimpleName() + ID_CACHEKEY_JOIN
                        + id.toString();
                getCacheProvider().remove(cacheKey);
            }
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "_autocache_ update Method[{}] executed,remove ralate cache {}.id:[{}]",
                        mt.getId(), entityInfo.getEntityClass().getSimpleName(), idsResult);
            }
        }
    } catch (Exception e) {
        logger.error("_autocache_ update Method[{}] remove ralate cache error", e);
    }
}