class="markdown_views prism-tomorrow-night">
MyBatis案例十:批量插入
- 接口方法:
public int batchInsert(List<Dept> list);
- mapper:
<insert id="batchInsert">
insert into dept(dname,loc)
values
<foreach collection="list" item="dept" separator=",">
(#{dept.dname},#{dept.loc})
foreach>
insert>
- 测试类:
@Test
public void testBatchInsert() throws IOException{
DeptMapper deptMapper = session.getMapper(DeptMapper.class);
List<Dept> list = new ArrayList<>();
list.add(new Dept(null, "hr1", "sy"));
list.add(new Dept(null, "hr2", "sy"));
list.add(new Dept(null, "hr3", "sy"));
int n = deptMapper.batchInsert(list);
session.commit();
System.out.println(n);
}