ibatis中iterate的用法

PersonDaoImp如下:
  /**
    * 删除性别为man,年龄为 11,12 的Person记录
    */
   public int deletePerson(Map<String, Object> map) {
        
         List<Person> personList= new ArrayList<Person>();

         Person p1= new Person(11);
         person p2= new Person(12);
         personList.add(p1);
         personList.add(p2);

         map.put( "personList", personList);

         map.put( "sex",'man');
         return getSqlMapClientTemplate().delete(
                                 "person.deletePerson", map);
     }
person.xml如下:
<!-- 删除相应的person记录 -->
   < delete id =" deletePerson " parameterClass ="map" >

    delete from 表名 where      sex=#sex#        
         < iterate prepend =" and" property =" personList " open ="("
           close =")" conjunction ="or" >
          age=$ personList[]. age$
         </ iterate >
      
   </ delete >
输出sql如下:
delete from 表名 where sex='man' and (age =11 or age=12 )

当然你也可以这么写:
person.xml如下:
<!-- 删除相应的person记录 -->
   < delete id =" deletePerson " parameterClass ="map" >

    delete from 表名 where      sex=#sex#     and age in  
         < iterate property =" personList " open ="("
           close =")" conjunction ="," >
          $ personList[]. age$
         </ iterate >
      
   </ delete >
输出sql如下:
delete from 表名 where sex='man' and  age in (11 ,12 )

最后Person代码大致如下:
public class Person{
   public Person(int age){
      this.age=age;
  }
   /**
    * 年龄
    */
   private int age;
   /**
    * 性别
    */
   private String sex;
 
   //get/set方法略
   ...
 } //end of Person

你可能感兴趣的:(ibatis,职场,iterate,休闲)