1

I followed this example : https://code.google.com/p/mybatis/wiki/ResultHandlerExample This is my interface:

public interface CountryDirRdbMapper {
    public static class CountryDirBaseItemWithText {
        public CountryDirBaseItem baseItem;
    }
    public List<CountryDirBaseItem> select(ResultHandler handler);
}

This is my xml mapper

  <resultMap id="readItemsRM" type="CountryDirRdbMapper$CountryDirBaseItemWithText">
        <association property="baseItem" javaType="CountryDirBaseItem">
            <id property="id" column="Id"/>
            <result property="comment" column="Comment"/>
        </association>
    </resultMap>

This code form my DAO:

SqlSession session = MyBatisConnectionFactory.getSqlSessionFactory().openSession(true);
List<CountryDirBaseItem> list;
  try{
       CountryDirRdbMapper mapper = session.getMapper(CountryDirRdbMapper.class);
       class MyResultHandler implements ResultHandler {
           @Override
           public void handleResult(ResultContext context) {
           System.out.println("#########################");
           }
       }

    MyResultHandler handler=new MyResultHandler();
      list= mapper.select(handler);
 }  
  finally {
   session.close();
}

However the result handler is never invoked. At the example I follow people say that have the same problem. So how to make it work? Or result handler is not supported in mybatis 3?

2 Answers2

3

I found the answer. Unfortunately MyBatis developers don't care about users at all. Shame on them. The truth is when we use custom result handlers we must use not interface but session.

MyResultHandler handler=new MyResultHandler();
session.select("select", handler);

After that the result must be taken from handler.

  • That's not accurate - you can use the interface to execute a mapped statement with a result handler without using the session. – Larry Apr 29 '16 at 21:56
  • 3
    @Larry if you write comments like that please provide some useful information as others - like me -may be looking for the very same answers... – Dariusz Mar 07 '17 at 12:14
3

Figured it out from pieces of information

  1. you need to get instance of SqlSession from your setup

    sqlSession = sqlSessionFactory.openSession();

  2. to use mapper with result handler, you need to make the mapper return void (important) and setup the result type for this mapper (important)

    @Select("select * from user") void selectAllUser(ResultHandler handler)

  3. get mapper from your sqlSession

    UserMapper mapper = sqlSession.getMapper(User.class);

  4. setup the session method with handler, the first parameter is the method name of the mapper you want to associate with your handler

    sqlSession.select("selectAllUser", yourResultHandler);

  5. start using your mapper

    mapper.selectAllUser(yourResultHandler);

  6. don't forget to release(close) session when done

Community
  • 1
  • 1
Leo Wei
  • 31
  • 1
  • Thank you for this. It took me so long to find a solution. The mybatis site is no help on this topic. – jimmy Nov 22 '20 at 21:21