首先在spring配置文件(applicationContext.xml)中添加
<!-- 配置一个可以执行批量的sqlSession -->
<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>
然后看测试类:
EmployeeMapper mapper=sqlSession.getMapper(EmployeeMapper.class);
long starttime=System.currentTimeMillis(); System.out.println("批量添加开始:"+starttime); for(int i=0;i<1000;i++) { String str=UUID.randomUUID().toString().substring(0, 6)+i; mapper.insert(new Employee(null, str, "M", str+"@qq.com",nums[random.nextInt(nums.length-1)])); } long endtime=System.currentTimeMillis(); System.out.println("批量添加执行成功"+endtime+"总耗时:"+(endtime-starttime));
