foreach标签的作用就是做循环遍历(框架中所接收的集合参数),foreach中有item、index、collection、open、sparator、close这些参数
item 表示集合中每一个元素进行迭代时的别名
index 指定一个名字,用于表示迭代过程中,每次迭代到的位置
open 表示该语句什么时候开始
separator 表示在每次进行迭代之间用什么符号作为分隔符
close 表示以什么结束
使用foreach实现批处理添加功能
一、部门sql的映射文件中写入<insert>标签mapper.xml
<insert id="deptSave">
insert into dept(loc,ziduanming)//括号中为表中字段名
valuse
<foreach collection="list" item="dept" separator=","></foreach>//C:表明向表中传入的参数是一个list集合,I:每做一次循环都会从集合取出一个对象交给所指定的对象(dept),S:使用逗号进行分割
(#{dept.dname}),(#{dept.ziduanming})
</insert>I
在Deptmapper.java中做接口的绑定处理
public int deptSave(List<Dept> deptList);
在测试类中
public void test06(){
DeptMapper dao = session.getMapper(DeptMapper.class);//首先向mabits框架索要DeptMapper的实例对象
Dept dept =new Dept();
dept.setLoc("中国"); //向部门中插入新的数据
dept.ziduanming("字段名");
Dept dept2 =new Dept();
dept.setLoc("中国"); //向部门中插入新的数据
dept.ziduanming("字段名");
List<Dept> deptList = new ArrayList<Dept>();//声明一个list集合,把新建的两个部门信息都放入list集合中
deptList.add(dept);
deptList.add(dept2);
dao.deptSave(deptList); //把list集合送入当前所需的SQL语句中
session.commit();//作用:通知数据库本次传输的SQL语句可以提交
}