MyBatis案例十:批量插入

2019-07-14 12:19发布

class="markdown_views prism-tomorrow-night">

MyBatis案例十:批量插入

  1. 接口方法:
public int batchInsert(List<Dept> list);
  1. mapper:
<insert id="batchInsert"> insert into dept(dname,loc) values <foreach collection="list" item="dept" separator=","> (#{dept.dname},#{dept.loc}) foreach> insert>
  1. 测试类:
@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); }