Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 743
Mahatma_Fatal_Error Avatar asked Jan 08 '23 22:01

Mahatma_Fatal_Error


1 Answers

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();
}
like image 142
ocmwdt Avatar answered Jan 26 '23 09:01

ocmwdt