Ibatis的批量处理-iterateiterate标签

iterate标签
prepend-可被覆盖的SQL语句组成部分,添加在语句的前面(可选)

property-类型为java.util.List的用于遍历的元素(必选)

open-整个遍历内容体开始的字符串,用于定义括号(可选)

close-整个遍历内容体结束的字符串,用于定义括号(可选)

conjunction-每次遍历内容之间的字符串,用于定义AND或OR(可选)

iterate-遍历类型为java.util.List(或数组)的元素。

/** 
     * 批量更新测试方法 
     * @param map 
     */
    public void updateListTest(){ 
        Map<String, Object> map=new HashMap<String, Object>(); 
        List<User> userList=new ArrayList<User>(); 
        User user1=new User(); 
        user1.setUsername("tom"); 
        user1.setUserpwd("000"); 
        User user2=new User(); 
        user2.setUsername("tom"); 
        user2.setUserpwd("222"); 
        User user3=new User(); 
        user3.setUsername("tom"); 
        user3.setUserpwd("333"); 
        userList.add(user1); 
        userList.add(user2); 
        userList.add(user3); 
        map.put("userList", userList); 
        map.put("username", "tom"); 
        int res=getSqlMapClientTemplate().update("updateListTest", map); 
        System.out.println("res----"+res); 
    } 
    public static void main(String\[\] args) { 
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); 
        UserDAO dao=(UserDAO) context.getBean("userDAO"); 
        dao.updateListTest(); 
    }

sql文件

<!-- 批理更新  --> 
   <update id="updateListTest" parameterClass="map"> 
         update user set address='批理处理' where username=#username# and userpwd in   
        <iterate property="userList" open="("
          close=")" conjunction=","> 
          $userList\[\].userpwd$ 
        </iterate>  
   </update> 
   <!-- 批量更新 --> 
   <update id="updateListTest1" parameterClass="map"> 
        update user set address='批理处理' where username=#username# 
        <iterate prepend="and" property="userList" open="("
          close=")" conjunction="or"> 
          age=$userList\[\].userpwd$ 
        </iterate>  
   </update> 
</sqlMap>