MyBatis案例十一:批量更新
- 接口方法:
public int batchUpdate(List<Dept> list);
- mapper:
<update id="batchUpdate">
<foreach collection="list" item="dept" >
update dept set dname = #{dept.dname},loc=#{dept.loc}
where deptno = #{dept.deptno};
</foreach>
</update>
- 修改连接url:(&为&的实体写法)
<property name="url" value="jdbc:mysql://localhost:3306/hr?allowMultiQueries=true&characterEncoding=utf-8"/>
- 测试类:
@Test
public void testBatchUpdate() throws IOException{
DeptMapper deptMapper = session.getMapper(DeptMapper.class);
List<Dept> list = new ArrayList<>();
list.add(new Dept(103, "hr7", "bj"));
list.add(new Dept(104, "hr8", "sh"));
list.add(new Dept(105, "hr9", "tj"));
int n = deptMapper.batchUpdate(list);
session.commit();
System.out.println(n);
}