How Mybatis uses ExecutorType.BATCH
This article mainly shows you "Mybatis how to use ExecutorType.BATCH", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Mybatis how to use ExecutorType.BATCH" this article.
Use ExecutorType.BATCH
There are three kinds of ExecutorType built into Mybatis, and the default is simple. In this mode, it creates a new preprocessing statement for the execution of each statement, a single sql; is submitted, while the batch mode reuses the pre-processed statements, and all update statements are executed in batches. Obviously, batch performance will be better. However, the batch mode also has its own problems. For example, in the Insert operation, there is no way to get the self-increasing id before the transaction is committed, which does not meet the business requirements in a certain situation.
The specific usage is as follows
/ / batch save method test @ Test public void testBatch () throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory (); / / sqlSession SqlSession openSession = sqlSessionFactory.openSession (ExecutorType.BATCH) that can perform batch operations; / / time before batch save long start = System.currentTimeMillis (); try {EmployeeMapper mapper = openSession.getMapper (EmployeeMapper.class); for (int I = 0; I < 1000) Mapper.addEmp (new Employee (UUID.randomUUID (). ToString (). Substring (0,5), "b", "1"));} openSession.commit (); long end = System.currentTimeMillis (); / / batch save the time after execution System.out.println ("execution time" + (end-start)) / / batch precompile sql once = "set parameters =" 10000 times = "execute 1 time 677 / / non-batch (precompilation = setting parameters = execution) =" 10000 times 1121} finally {openSession.close ();}}
Mapper and mapper.xml are as follows
Public interface EmployeeMapper {/ / bulk save employee Long addEmp (Employee employee);}