Example One
Query Condition DTO
public class Querycondition
{
Private string[] stuids;
private String name;
}
Query Sqlmap
<select id= "Selectstu" parameterclass= "cn.xy.queryCondition" resultclass= "Cn.xy.Student" >
Select Id,name from Student
<dynamic prepend= "where" >
<isnotnull property= "Stuids" prepend= "and" >
<iterate property= "Stuids" open= "id in (" close= ")" conjunction= "," >
#stuIds []#
</iterate>
</isNotNull>
<isnotnull property= "name" prepend= "and" >
Name like '% $name $% '
</isNotNull>
</dynamic>
</select>
In the query condition there is an array stuids, which is traversed in the dynamic tag to see if each student ID is in the array.
The Issued statement select Id,name from student where ID in (?,?) ...
Example Two
Query Condition DTO
public class Querycondition
{
Private list<student> lst;
private String name;
}
Query Sqlmap
<select id= "Selectstu" parameterclass= "cn.xy.queryCondition" resultclass= "Cn.xy.Student" >
Select Id,name from Student
<dynamic prepend= "where" >
<isnotnull property= "LST" prepend= "and" >
<iterate property= "LST" open= "(" close= ")" conjunction= "or" >
id = #lst[].id#
</iterate>
</isNotNull>
<isnotnull property= "name" prepend= "and" >
Name like '% $name $% '
</isNotNull>
</dynamic>
</select>
The Issued statement select Id,name from student where (id =? or id =?) ...
Usage of iterate in Ibatis (conjunction= "or", ")