Search code examples
javaxmlpaginationmybatismapper

How to connect a ResultHandler in MyBatis Mapper XML


I found several examples how to connect a custom ResultHandler to a MyBatis Query:

e.g. https://code.google.com/p/mybatis/wiki/ResultHandlerExample

Unfortunately the ResultHandler given in the example never gets invoked. (As the last comment already stated)

So I searched for a solution and found this: MyBatis - ResultHandler is not invoked

But this does not quite fit to my problem since I'm using MyBatis the xml-style way rather than the API-style way. So in my case I have no

SqlSession session = MyBatisConnectionFactory.getSqlSessionFactory().openSession(true);

Is there a way to connect my custom handler in the xml file, for example the <resultMap /> oder <select /> node?


Solution

  • You can define method with ResultHandler in your mapper:

    public interface YourMapper {
        void getObjects(@Param("param1") Object param1, ResultHandler handler);
    }
    

    Then you can use it:

    List<Object> getObjects(object param1) {
        YourResultHandler resultHandler = new YourResultHandler();
        yourMapper.getObjects(param1, resultHandler);
        return resultHandler.getResults();
    }