Search code examples
javasqlibatis

Iterate list of Objects in Ibatis


I have a list of object where I want to iterate and access a particular field in ibatis sql.

Ex.

public Class Student
{
String id;
String name;
}

I will pass as parameter a List of Student object(List(Student))
and do iteration accessing the id for each object bean. How do I do this?


Solution

  • The foreach-tag is what you are looking for. Example:

    <select id="selectPostIn" resultType="domain.blog.Post">
      SELECT *
       FROM POST P
       WHERE ID in
       <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
        #{item}
       </foreach>
    </select>
    

    See the user guide for more info, chapter "dynamic sql".

    By the way, iBatis is no longer developed and is frozen, it is now called "MyBatis" and the whole developer team moved away from Apache to the new MyBatis home.