foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名.
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置.
open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符.
close表示以什么结束.
在使用foreach的时候最容易出错的就是collection属性,该属性是必须指定的,在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
1、批量添加元素session.insert(String string,Object o)
- public void batchInsertStudent(){
- List<Student> ls = new ArrayList<Student>();
- for(int i = 5;i < 8;i++){
- Student student = new Student();
- student.setId(i);
- student.setName("maoyuanjun" + i);
- student.setSex("man" + i);
- student.setTel("tel" + i);
- student.setAddress("浙江省" + i);
- ls.add(student);
- }
- SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
- session.insert("mybatisdemo.domain.Student.batchInsertStudent", ls);
- session.commit();
- session.close();
- }
- <insert id="batchInsertStudent" parameterType="java.util.List">
- INSERT INTO STUDENT (id,name,sex,tel,address)
- VALUES
- <foreach collection="list" item="item" index="index" separator="," >
- (#{item.id},#{item.name},#{item.sex},#{item.tel},#{item.address})
- </foreach>
- </insert>
2、批量修改session. insert (String string,Object o)
- 实例1:
- public void batchUpdateStudent(){
- List<Integer> ls = new ArrayList<Integer>();
- for(int i = 2;i < 8;i++){
- ls.add(i);
- }
- SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
- session.insert("mybatisdemo.domain.Student.batchUpdateStudent",ls);
- session.commit();
- session.close();
- }
- <update id="batchUpdateStudent" parameterType="java.util.List">
- UPDATE STUDENT SET name = "5566" WHERE id IN
- <foreach collection="list" item="item" index="index" open="(" separator="," close=")" >
- #{item}
- </foreach>
- </update>
- 实例2:
- public void batchUpdateStudentWithMap(){
- List<Integer> ls = new ArrayList<Integer>();
- for(int i = 2;i < 8;i++){
- ls.add(i);
- }
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("idList", ls);
- map.put("name", "mmao789");
- SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
- session.insert("mybatisdemo.domain.Student.batchUpdateStudentWithMap",map);
- session.commit();
- session.close();
- }
- <update id="batchUpdateStudentWithMap" parameterType="java.util.Map" >
- UPDATE STUDENT SET name = #{name} WHERE id IN
- <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </update>
3、批量删除session.delete(String string,Object o)
- public void batchDeleteStudent(){
- List<Integer> ls = new ArrayList<Integer>();
- for(int i = 4;i < 8;i++){
- ls.add(i);
- }
- SqlSession session = SessionFactoryUtil.getSqlSessionFactory().openSession();
- session.delete("mybatisdemo.domain.Student.batchDeleteStudent",ls);
- session.commit();
- session.close();
- }
- <delete id="batchDeleteStudent" parameterType="java.util.List">
- DELETE FROM STUDENT WHERE id IN
- <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
- #{item}
- </foreach>
- </delete>