ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ted Schrader" <teds.mailing.li...@gmail.com>
Subject Re: SQL Map Problem - iterating through a list, using a like query
Date Tue, 06 Mar 2007 23:46:59 GMT
Hi Ayan,

> Brad - i believe when you have a WHERE clause already specified,
> you do not need to use "<dynamic>".

You're thinking is probably right on this, but I'll go ahead and be a
bit more explicit on the <dynamic> tag (hopefully I'm not hijacking
the thread too much here).

Let's suppose we have a query that *might* need a WHERE clause.
Here's an example:

SELECT ID, DESCRIPTION
  FROM ITEMS
<dynamic prepend="WHERE">
     <isNotEmpty property="id">
         ID = #id#
     </isNotEmpty>
</dynamic>

This query could be used with queryForObject() if I specify an ID.
This query could also be used with queryForList() when I don't.

Let's take the case where I do specify the ID.  In this case, the
<isNotEmpty> would evaluate to true and the ID = #id# bit would be
rendered.  Since there's content within the <dynamic> tag, iBATIS will
render the prepend="WHERE" part.  So, we'd ultimately get:

SELECT ID, DESCRIPTION
  FROM ITEMS
WHERE ID = ?

Now, on to queryForList() and no ID.  The <isNotEmpty> would evaluate
to false and therefore ID = #id# will not be rendered.  This will
leave the <dynamic> tag without any content of its own and, therefore,
the prepend="WHERE" will not be rendered, either.  The result:

SELECT ID, DESCRIPTION
  FROM ITEMS


Now, a further question:  Can we nest <dynamic> tags?  I'm still on
version 2.1.5 (yeah, a year and a half ago) and it didn't seem to
work.

Ted

Mime
View raw message