//使用批量插入的方法向数据库中插入100条记录
@Test
public void testbatchsave2() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
Department department=new Department();
department.setDepartmentId(1);
for(int i=0;i<100;i++){
Customer customer=new Customer("LL1", 1, "ll@", null);
customer.setDepartment(department);
CustomerMapper mapper = session.getMapper(CustomerMapper.class);
mapper.saveCustomer(customer);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
session.commit();
session.close();
}
}`在这里插入代码片`
2.Spring中使用Mybatis的批量处理
<bean id="sqlSessionTemplate"
class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>