You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by costab <k4...@nym.hush.com> on 2010/02/01 23:23:36 UTC

Custom ResultSet handler

I am a newbie to ibatis and I want to figure out where iBatis would fit to
solve my requirements.

Is it possible in iBatis 3.0 (or 2.x) to provide my own ResultSet handler? I
read the documentation and it is seems that I would have to use a plugin to
do that. Are there any samples on how to do it? Can I specify what plug-in I
want to use for specific select elements? For some queries I want to leave
to iBatis to do the mapping for others I want to specify my own handler.




-- 
View this message in context: http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27412455.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Custom ResultSet handler

Posted by Martin Ellis <el...@gmail.com>.
On 2 February 2010 15:55, Clinton Begin <cl...@gmail.com> wrote:
> The original question sort of implied an answer, which might have us all
> missing the goals.

Sorry, I should've been clearer that I was answering Stephen
Friedrich's question, which requires low-level JDBC access.
Also that I didn't have an answer for the original question.

Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Custom ResultSet handler

Posted by Clinton Begin <cl...@gmail.com>.
The original question sort of implied an answer, which might have us all
missing the goals.

For example, to map directly to XML, there's no need to replace the
ResultSetHandler.  Perhaps the confusion is that the op might have been
referring to Spring's ResultSetHandler, which is a completely different
construct.

In iBATIS, if you want to support a different result type, it's done at a
much different level.

The ObjectWrapperFactory interface is a sort of plug-in that allows iBATIS
to be extended to support different types (like Scala objects, or XML).  The
only two supported by iBATIS out of the box are Java POJOs/Beans and Maps
(like HashMap).

The interface is pretty simple.  However I recommend looking at the other
implementations, including the Scala one, to see how it should be
implemented.

public interface ObjectWrapperFactory {
  boolean hasWrapperFor(Object object);
  ObjectWrapper getWrapperFor(MetaObject metaObject, Object object);
}

Cheers,
Clinton

On Tue, Feb 2, 2010 at 2:20 AM, Stephen Friedrich <
stephen.friedrich@fortis-it.eu> wrote:

> I am also interested in this feature:
> Much of my code uses ibatis to generate beans, then works on those.
> However I have existing code that takes an Excel-template and a JDBC
> result set and fills the excel file using apache poi.
> I would very much like to re-use the existing ibatis-queries (including
> their dynamic parts).
>
> Another option for me would be to somehow get the final SQL statement and
> parameters, then execute that statement myself. Is that possible somehow?
>
> -----Ursprüngliche Nachricht-----
> Von: costab [mailto:k444@nym.hush.com]
> Gesendet: Montag, 1. Februar 2010 23:24
> An: user-java@ibatis.apache.org
> Betreff: Custom ResultSet handler
>
>
> I am a newbie to ibatis and I want to figure out where iBatis would fit to
> solve my requirements.
>
> Is it possible in iBatis 3.0 (or 2.x) to provide my own ResultSet handler?
> I
> read the documentation and it is seems that I would have to use a plugin to
> do that. Are there any samples on how to do it? Can I specify what plug-in
> I
> want to use for specific select elements? For some queries I want to leave
> to iBatis to do the mapping for others I want to specify my own handler.
>
>
>
>
> --
> View this message in context:
> http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27412455.html
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

Re: Custom ResultSet handler

Posted by Martin Ellis <el...@gmail.com>.
On 2 February 2010 11:43, Martin Ellis <el...@gmail.com> wrote:
> On 2 February 2010 09:20, Stephen Friedrich
> <st...@fortis-it.eu> wrote:
>> Another option for me would be to somehow get the final SQL statement and
>> parameters, then execute that statement myself. Is that possible somehow?
>
> I have something like this:
>
>        String sqlId = ... // The 'id' of the SQL statement in the XML config
>        Object param = ... // The parameter object for the query
>        ResultSet rs = null;
>        PreparedStatement ps = null;
>        try {
>            Configuration conf = sf.getConfiguration();
>            MappedStatement ms = conf.getMappedStatement(sqlId);
>            BoundSql boundSql = ms.getBoundSql(param);
>            ParameterHandler pHandler = new
> DefaultParameterHandler(ms, param, boundSql);
>
>            stmt = conn.prepareStatement(boundSql.getSql());
>            pHandler.setParameters(stmt);
>            rs = stmt.executeQuery();

In the above, conn is initialised as follows:

   Environment env = sf.getConfiguration().getEnvironment();
   conn = sf.getConfiguration().getEnvironment().getDataSource().getConnection();
   Transaction txn = env.getTransactionFactory().newTransaction(conn, false);

Probably want to commit/rollback your txn afterwards.

Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Custom ResultSet handler

Posted by Martin Ellis <el...@gmail.com>.
On 2 February 2010 09:20, Stephen Friedrich
<st...@fortis-it.eu> wrote:
> Another option for me would be to somehow get the final SQL statement and
> parameters, then execute that statement myself. Is that possible somehow?

I have something like this:

        String sqlId = ... // The 'id' of the SQL statement in the XML config
        Object param = ... // The parameter object for the query
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            Configuration conf = sf.getConfiguration();
            MappedStatement ms = conf.getMappedStatement(sqlId);
            BoundSql boundSql = ms.getBoundSql(param);
            ParameterHandler pHandler = new
DefaultParameterHandler(ms, param, boundSql);

            stmt = conn.prepareStatement(boundSql.getSql());
            pHandler.setParameters(stmt);
            rs = stmt.executeQuery();

I don't really like it.  Manually handling the result set is probably
a premature optimisation for my use case.  But you live and learn.

Martin

---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


AW: Custom ResultSet handler

Posted by Stephen Friedrich <st...@fortis-it.eu>.
I am also interested in this feature:
Much of my code uses ibatis to generate beans, then works on those.
However I have existing code that takes an Excel-template and a JDBC
result set and fills the excel file using apache poi.
I would very much like to re-use the existing ibatis-queries (including
their dynamic parts).

Another option for me would be to somehow get the final SQL statement and 
parameters, then execute that statement myself. Is that possible somehow?

-----Ursprüngliche Nachricht-----
Von: costab [mailto:k444@nym.hush.com] 
Gesendet: Montag, 1. Februar 2010 23:24
An: user-java@ibatis.apache.org
Betreff: Custom ResultSet handler


I am a newbie to ibatis and I want to figure out where iBatis would fit to
solve my requirements.

Is it possible in iBatis 3.0 (or 2.x) to provide my own ResultSet handler? I
read the documentation and it is seems that I would have to use a plugin to
do that. Are there any samples on how to do it? Can I specify what plug-in I
want to use for specific select elements? For some queries I want to leave
to iBatis to do the mapping for others I want to specify my own handler.




-- 
View this message in context: http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27412455.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Custom ResultSet handler

Posted by Clinton Begin <cl...@gmail.com>.
It sounds like Spring JDBC might be better for your needs.

Clinton

On Mon, Feb 1, 2010 at 8:06 PM, costab <k4...@nym.hush.com> wrote:

>
> Wouldn't be better if the iBatis select tag would allow you to specify the
> ResultSetHandler class as an attribute?
>
> I used Spring JDBC and this is how I did it, I used their ResultSetHandler
> class to process the ResultSet the way I wanted. Then you can have one
> query
> but then you can create different types of output, xml or pojos or excel by
> simply providing which ResultSetHandler you want to process your query.
>
>
> Clinton Begin wrote:
> >
> > But if you really want to do this, extend the Configuration class, and
> > then
> > override the following method:
> >
> >   public ResultSetHandler newResultSetHandler(Executor executor,
> > MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler
> > parameterHandler, ResultHandler resultHandler, BoundSql boundSql);
> >
> > You'll have to use the pure Java configuration approach, not the XML
> > configuration.
> >
>
> Is there any sample I can look at to see how you do this?
>
>
> Did you guys ever think of making use of Spring Jdbc? I think iBatis could
> borrow some of the Spring Jdbc goodies and build on that foundation. There
> are a lot of goodies on both sides and it would be great for users to
> "springify" iBatis. That would solve the dilemma that myself and others
> experience - which one I should use iBatis or Spring Jdbc?
>
>
> Thanks a lot!
>
> --
> View this message in context:
> http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27414917.html
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

Re: Custom ResultSet handler

Posted by costab <k4...@nym.hush.com>.
Wouldn't be better if the iBatis select tag would allow you to specify the
ResultSetHandler class as an attribute? 

I used Spring JDBC and this is how I did it, I used their ResultSetHandler
class to process the ResultSet the way I wanted. Then you can have one query
but then you can create different types of output, xml or pojos or excel by
simply providing which ResultSetHandler you want to process your query.


Clinton Begin wrote:
> 
> But if you really want to do this, extend the Configuration class, and
> then
> override the following method:
> 
>   public ResultSetHandler newResultSetHandler(Executor executor,
> MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler
> parameterHandler, ResultHandler resultHandler, BoundSql boundSql);
> 
> You'll have to use the pure Java configuration approach, not the XML
> configuration.
> 

Is there any sample I can look at to see how you do this?


Did you guys ever think of making use of Spring Jdbc? I think iBatis could
borrow some of the Spring Jdbc goodies and build on that foundation. There
are a lot of goodies on both sides and it would be great for users to
"springify" iBatis. That would solve the dilemma that myself and others
experience - which one I should use iBatis or Spring Jdbc?


Thanks a lot!

-- 
View this message in context: http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27414917.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Re: Custom ResultSet handler

Posted by Clinton Begin <cl...@gmail.com>.
You should do that after iBATIS converts it to a Map or Bean IMHO... perhaps
using XStream or something similar.

But if you really want to do this, extend the Configuration class, and then
override the following method:

  public ResultSetHandler newResultSetHandler(Executor executor,
MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler
parameterHandler, ResultHandler resultHandler, BoundSql boundSql);

You'll have to use the pure Java configuration approach, not the XML
configuration.

You will enter a very dark and scary world trying to implement your own
ResultSetHandler though.  This is by far the most complex function that
iBATIS performs.

Clinton


On Mon, Feb 1, 2010 at 6:02 PM, costab <k4...@nym.hush.com> wrote:

>
> Sorry, just to be more clear I want my map to convert the ResultSet object
> to
> xml.
>
>
> costab wrote:
> >
> > I am a newbie to ibatis and I want to figure out where iBatis would fit
> to
> > solve my requirements.
> >
> > Is it possible in iBatis 3.0 (or 2.x) to provide my own ResultSet
> handler?
> > I read the documentation and it is seems that I would have to use a
> plugin
> > to do that. Are there any samples on how to do it? Can I specify what
> > plug-in I want to use for specific select elements? For some queries I
> > want to leave to iBatis to do the mapping for others I want to specify my
> > own handler.
> >
> >
> >
> >
> >
>
> --
> View this message in context:
> http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27414093.html
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>

Re: Custom ResultSet handler

Posted by costab <k4...@nym.hush.com>.
Sorry, just to be more clear I want my map to convert the ResultSet object to
xml.


costab wrote:
> 
> I am a newbie to ibatis and I want to figure out where iBatis would fit to
> solve my requirements.
> 
> Is it possible in iBatis 3.0 (or 2.x) to provide my own ResultSet handler?
> I read the documentation and it is seems that I would have to use a plugin
> to do that. Are there any samples on how to do it? Can I specify what
> plug-in I want to use for specific select elements? For some queries I
> want to leave to iBatis to do the mapping for others I want to specify my
> own handler.
> 
> 
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Custom-ResultSet-handler-tp27412455p27414093.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org