Friday, January 22, 2010

IBatis: Iterate over multiple lists at the same time

Problem:
I needed to verify if each record passed several rules, where each rule consisted of several flags and their values. The problem was complexity: I had 7 flags(which had 3 possible values) to check, and 1 to 5 rules per status. This could lead to some ugly and hard to test SQL.

Another problem was that you cant iterate over multiple lists at the same time (using the same index) in ibatis.

Solution:
The solution was to use Ibatis's Iterate directive. I would iterate over lists of maps, where each map represented a rule, and each bucket of the map represented a flag and its expected value.

Those iBastis code ended up looking something like this:


and the parameter class had the following code:



This worked pretty well to abstract the flags and rules away from the iBatis sql and keep them all in the rules class.

3 comments:

  1. Hey Mikhail,
    I used trick you gave in this post to resolve ibatis problem i was facing.You really did it in smart way.I didnt thought this way.Thanks for this post,it really helped.

    ReplyDelete
  2. It helped so much. Thanks!

    ReplyDelete
  3. Thanks a lot for this solution. it worked for me.
    i was trying to have a list of beans and iterate the list and use bean properties. that didn't work.

    Hence, i replaced the bean with a map.
    so, list of beans ===> list of maps. That works.

    ReplyDelete