<insert id="addEmps">
insert into employee(name,gender,email,d_id) values
(#{emp.name},#{emp.email},#{emp.gender},#{emp.dept.id})
</insert>
public void test4() throws IOException {
SqlSession opensession=getSqlSessionFactory().openSession(ExecutorType.BATCH);
//可以执行批量操作的opensession
EmployeeMapper mapper=opensession.getMapper(EmployeeMapper.class);
Employee employee=new Employee(null,"b","bb","bbb",new Department(1));
for(int i=0;i<100;i++) {
mapper.addEmps(employee);
}
opensession.commit();
opensession.close();
}
SqlSession opensession=getSqlSessionFactory().openSession(ExecutorType.BATCH);
使用的是ExecutorType.BATCH这个mybatis自带的方法
若是在整合spring情况下 可以在spring的配置文件中加入:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
然后在service层,
@Autowried
private SqlSession sqlsession
然后再执行程序,就是一个批量操作了