Empty foreach regression in myBatis 3.0.x, compared to iBatis 2.3.x

510 views
Skip to first unread message

Aaron

unread,
May 13, 2011, 10:00:14 AM5/13/11
to mybatis-user
I'm currently upgrading from iBatis 2 to myBatis 3. We used to have
some SQL maps which looked like this:

...
where
country = #{country}
<iterate property="productNames" prepend="and (" conjunction="or"
close=")">
product_name = #productNames[]#
</iterate>
...

myBatis 3 has replaced the "iterate" element with the "forEach"
element. However, the forEach element handles the empty case
differently than iBatis2. For this specific SQL map, it generates some
SQL including the clause "...and ()...", which is not valid. The
iBatis 2 iterate statement handled this case by omitting the
product_name entirely. We can achieve this same behavior in myBatis3
by wrapping every iterate statement in its own "if" statement, testing
for empty collections. However, this seems like a regression to me.
Why was this change made? If the empty case is handled in this way,
what is the point of the "open" and "close" attributes in myBatis? Is
there any functional difference between the following two SQL maps?

<foreach item="item" collection="productNames" open="and ("
separator="or" close=")">
product_name = #{item}
</foreach>

and (
<foreach item="item" collection="productNames" separator="or">
product_name = #{item}
</foreach>
)

In iBatis 2, those two SQL maps differed because of the handling of
the empty case; the first one would handle the empty case correctly,
while the second one would have an empty "foreach" statement.

- Aaron
Message has been deleted

Mukarram

unread,
Apr 22, 2014, 1:56:13 AM4/22/14
to mybati...@googlegroups.com
Hi All,

I stumbled on this dated post and wanted to check if the concern raised by Aaron seems fair (looks fair to me - unless I am missing something obvious). 

Iwao AVE!

unread,
Apr 22, 2014, 5:26:28 AM4/22/14
to mybatis-user
Hi Mukarram,

It seems like a regression indeed.
Would you mind opening a new issue on the tracker?

Thanks in advance,
Iwao

Mukarram

unread,
May 10, 2014, 5:19:43 AM5/10/14
to mybati...@googlegroups.com
Thanks Iwao, opened https://github.com/mybatis/mybatis-3/issues/195 for this one.

BTW, loved your mybatipse work!

Iwao AVE!

unread,
May 10, 2014, 6:02:11 AM5/10/14
to mybatis-user
Hi Mukarram,

Thanks a lot for your time!
I will take a look later.

> BTW, loved your mybatipse work!

Glad to hear that, thank you! :-)

// Iwao
> --
> You received this message because you are subscribed to the Google Groups
> "mybatis-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mybatis-user...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages