MyBatis 學習筆記(七)批量插入ExecutorType.BATCH效率對比
一、在mybatis中ExecutorType的使用
1.Mybatis內置的ExecutorType有3種,默認的是simple,該模式下它為每個語句的執行創建一個新的預處理語句,單條提交sql;而batch模式重復使用已經預處理的語句,
並且批量執行所有更新語句,顯然batch性能將更優;
2.但batch模式也有自己的問題,比如在Insert操作時,在事務沒有提交之前,是沒有辦法獲取到自增的id,這在某型情形下是不符合業務要求的;
在測試中使用simple模式提交10000條數據,時間為18248 毫秒,batch模式為5023 ,性能提高70%;
@Test public void mybatisBatch() { SqlSession session = getSqlSessionFactory().openSession(); try { DeptMapper deptMapper = (DeptMapper) session.getMapper(DeptMapper.class); long start =System.currentTimeMillis(); for (int i = 0; i <10000 ; i++) { SysDept dept=new SysDept(UUID.randomUUID().toString().substring(1,6), 1, new Date(), new Date(), 1); deptMapper.saveSysDept(dept); } long end =System.currentTimeMillis(); System.out.println("耗時:"+(end-start)); //ExecutorType.BATCH 批量耗時耗時:2134 //單條操作耗時 耗時:8584 } catch (Exception e) { e.printStackTrace(); } finally { session.commit(); session.close(); } } @Test public void saveDeptBatchOne() { SqlSession session = getSqlSessionFactory().openSession(); try { DeptMapper deptMapper = (DeptMapper) session.getMapper(DeptMapper.class); long start =System.currentTimeMillis(); List<SysDept> deptList=new ArrayList<SysDept>(); for (int i = 0; i <100000 ; i++) { SysDept dept=new SysDept(UUID.randomUUID().toString().substring(1,6), 1, new Date(), new Date(), 1); deptList.add(dept); if(i%500==0){ deptMapper.saveDeptBatch(deptList); deptList.clear(); } } deptMapper.saveDeptBatch(deptList); long end =System.currentTimeMillis(); System.out.println("耗時:"+(end-start)); //非BATCH批量耗時 耗時:938 } catch (Exception e) { e.printStackTrace(); } finally { session.commit(); session.close(); } } @Test public void saveDeptBatchTwo() { //設置ExecutorType.BATCH原理:把SQL語句發個數據庫,數據庫預編譯好,數據庫等待需要運行的參數,接收到參數后一次運行,ExecutorType.BATCH只打印一次SQL語句,多次設置參數步驟, SqlSession session = getSqlSessionFactory().openSession(ExecutorType.BATCH); try { DeptMapper deptMapper = (DeptMapper) session.getMapper(DeptMapper.class); long start =System.currentTimeMillis(); List<SysDept> deptList=new ArrayList<SysDept>(); for (int i = 0; i <100000; i++) { SysDept dept=new SysDept(UUID.randomUUID().toString().substring(1,6), 1, new Date(), new Date(), 1); deptList.add(dept); if(i%500==0){ deptMapper.saveDeptBatch(deptList); deptList.clear(); } } deptMapper.saveDeptBatch(deptList); long end =System.currentTimeMillis(); System.out.println("耗時:"+(end-start)); //BATCH批量耗時 耗時:822 } catch (Exception e) { e.printStackTrace(); } finally { session.commit(); session.close(); } }
二、在mybatis+spring中ExecutorType的使用
1、在spring配置文件中添加批量執行的SqlSessionTemplate
<!--配置一個可以進行批量執行的sqlSession --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg> <constructor-arg name="executorType" value="BATCH"></constructor-arg> </bean>
2、service中獲取批量添加的SqlSession
@Service public class DeptService { @Autowired private DeptMapper deptMapper; @Autowired private SqlSession sqlSession; public List<Dept> addDept(){ //executorType=BATCH 添加操作 DeptMapper mapper = sqlSession.getMapper(DeptMapper.class); return mapper.saveDept(Dept); } }
三、$和#的區別
#{}:可以獲取map中的值或者pojo對象屬性的值;
${}:可以獲取map中的值或者pojo對象屬性的值;
select * from tbl_employee where id=${id} and last_name=#{lastName}
Preparing: select * from tbl_employee where id=2 and last_name=?
區別:
#{}:是以預編譯的形式,將參數設置到sql語句中;PreparedStatement;防止sql注入
${}:取出的值直接拼裝在sql語句中;會有安全問題;