ResultHandler with SQL Map XML files?

557 views
Skip to first unread message

Rajiv Nair

unread,
Dec 13, 2010, 9:11:04 AM12/13/10
to mybatis-user
Hello

I'm using myBatis with an SQL Map XML file. I have a complex query's
result that needs a ResultHandler. I was hoping for a ResultHandler
annotation, but didn't find any. Can someone suggest an approach where
I can continue using my SQL Map but use a ResultHandler for this
particular query?

Thanks,
Rajiv

Poitras Christian

unread,
Dec 13, 2010, 10:53:52 AM12/13/10
to mybati...@googlegroups.com
Maybe this example will help.
http://code.google.com/p/mybatis/wiki/ResultHandlerExample

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Rajiv Nair
Envoyé : December-13-10 9:11 AM
À : mybatis-user
Objet : ResultHandler with SQL Map XML files?

Rajiv Nair

unread,
Dec 14, 2010, 3:47:54 AM12/14/10
to mybatis-user
Thanks for the quick reply, Christian.
I guess I didn't ask my question clearly.

Currently, I use an sql mapping file (with
org.mybatis.spring.MapperFactoryBean) to run my queries. So I am using
the generated proxy from mybatis-spring integration which also
provides me with the ability to use Spring Transactions. However, for
one of my queries, I need to do a bit more with the result set than is
currently supported by the <resultMap /> element.
I was wondering if it was possible to use the ResultHandler via the
MapperFactoryBean and XML without doing something like the following:

sqlSession.select("query", null, resultHandler);

I would want to use just the mapper interface and get the proxy
generated through the integration code which will support the result
handler either through an annotation or a parameter. Something like
this:

interface SomeMapper {
List<SomeStuff> selectSomeStuff(String parameter,ResultHandler
handler)
}

or
interface SomeMapper {
@ResultHandler("SomeResultHanlder.class")
List<SomeStuff> selectSomeStuff(String parameter)
}

I think there is similar support provided for @MapKey. Is this
possible? If not, please consider this a feature request :)

Thanks,
Rajiv


On Dec 13, 8:53 pm, Poitras Christian <Christian.Poit...@ircm.qc.ca>
wrote:
> Maybe this example will help.http://code.google.com/p/mybatis/wiki/ResultHandlerExample

Poitras Christian

unread,
Dec 14, 2010, 8:48:39 AM12/14/10
to mybati...@googlegroups.com
I see your point now.
I didn't work on the Spring integration, but if it's possible to get an instance of SqlSession injected in your object, you could do "sqlSession.select("query", null, resultHandler);". That is sure to wok.

For "List<SomeStuff> selectSomeStuff(String parameter,ResultHandler handler)", I don't think it's possible since it would be seen as a parameter to pass to the query.
The @ResultHandler could be a good option, but in such a case I cannot see how a List could be returned. The mapper would absolutely need to return the ResultHandler to make any sense since the ResultHandler doesn't return any value.

This could be seen as a improvement. But since I am not sure enough that it's the way to go, I recommend to stick with "sqlSession.select("query", null, resultHandler);" for now.

Christian

-----Message d'origine-----
De : mybati...@googlegroups.com [mailto:mybati...@googlegroups.com] De la part de Rajiv Nair

Envoyé : December-14-10 3:48 AM
À : mybatis-user
Objet : Re: ResultHandler with SQL Map XML files?

Rajiv Nair

unread,
Dec 16, 2010, 2:39:49 AM12/16/10
to mybatis-user
Yes, you're right. I've now done this by injecting an
SqlSessionTemplate into my object. That works fine.
Thanks very much for your help!
-Rajiv

On Dec 14, 6:48 pm, Poitras Christian <Christian.Poit...@ircm.qc.ca>
Reply all
Reply to author
Forward
0 new messages