mybatis/mybatis-3 — pull request #27 by emacarron (2013-04-07 07:45:27+00:00)
Title: Added support for associations and collections filled with multiple result sets

Hi.

This change added support for filling associations and collections out of multiple result sets.

Suppose we have the tipical N+1 problem: and order and order lines. Right now MyBatis provides two ways of solving this.
- A join (nested result map)
- A nested select

Both work but have their drawbacks.

The join gets more data than needed and MyBatis should do an extra work to split the data back into entities.

The nested select is slow and resource consuming. It is much better to hit once the DB instead of N+1 times.

The proposal is to add a new way to solve this.

First, two sentences are sent to the database at the same time.

select * from mbtest.order_header where order_id=#{id};
select * from mbtest.order_detail where order_id=#{id}

In the mappings, we include a new attribute to indicate that the data is already in the statement execution result.

Probably this method will be the fastest of the three.

The change is quite small and it is very unlikely that it has side effects.

Please have a look at it and post your thoughts.