首先在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));