MyBatis的批量操作

在sqlSessionFactory创建sqlSession是传入参数ExecutorType.BANTCH,以实现批量操作的sqlSession对象。

        //可以执行批量操作的sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)
    @Test
    public void testBatch() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

        //可以执行批量操作的sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);

        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            mapper.addEmp(new Employee(null, UUID.randomUUID().toString().substring(0,5),UUID.randomUUID().toString().substring(0,5)+"@qq.com","0"));
        }
        sqlSession.commit();
        long end = System.currentTimeMillis();

        System.out.println("执行时长"+(end-start));//14905===20582
    }
原文地址:https://www.cnblogs.com/xjs1874704478/p/11963064.html