MyBatis通过BATCH批量提交的方法
很多人在用MyBatis或者通用Mapper时,经常会问有没有批量插入和批量更新的方法。
实际上许多时候没必要用
下面是一个批量用法的例子:
@Autowired privateSqlSessionFactorysqlSessionFactory; @Transactional(rollbackFor=Exception.class) @Override publicvoidbatchTest(){ SqlSessionsqlSession=sqlSessionFactory.openSession(ExecutorType.BATCH); CountryMappermapper=sqlSession.getMapper(CountryMapper.class); Listcountries=mapper.selectAll(); for(inti=0;i 在上面例子中,在Service中直接注入了SqlSessionFactory,通过下面方法获取了一个可以批量提交的SqlSession:
SqlSessionsqlSession=sqlSessionFactory.openSession(ExecutorType.BATCH);后续通过SqlSession直接执行方法,或者获取的Mapper接口,都使用的批量提交方式。
上述代码执行过程中输出的日志如下:
DEBUG-Creatingnewtransactionwithname[com.isea533.mybatis.service.impl.CountryServiceImpl.batchTest]:PROPAGATION_REQUIRED,ISOLATION_DEFAULT
DEBUG-AcquiredConnection[com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2]forJDBCtransaction
DEBUG-SwitchingJDBCConnection[com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2]tomanualcommit
DEBUG-JDBCConnection[com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2]willbemanagedbySpring
DEBUG-==> Preparing:SELECTId,countryname,countrycodeFROMcountry
DEBUG-==>Parameters:
DEBUG-<== Total:183
DEBUG-==> Preparing:UPDATEcountrySETId=Id,countryname=?,countrycode=?WHEREId=?
DEBUG-==>Parameters:AngolaTest(String),AO(String),1(Integer)
DEBUG-==>Parameters:AfghanistanTest(String),AF(String),2(Integer)
DEBUG-==>Parameters:AlbaniaTest(String),AL(String),3(Integer)
==========================================
...省略中间部分参数
==========================================
DEBUG-==>Parameters:EthiopiaTest(String),ET(String),50(Integer)
DEBUG-==> Preparing:UPDATEcountrySETId=Id,countryname=?,countrycode=?WHEREId=?
DEBUG-==>Parameters:FijiTest(String),FJ(String),51(Integer)
DEBUG-==>Parameters:FinlandTest(String),FI(String),52(Integer)
==========================================
...省略中间部分参数
==========================================
DEBUG-==>Parameters:MadagascarTest(String),MG(String),98(Integer)
DEBUG-==>Parameters:MalawiTest(String),MW(String),99(Integer)
DEBUG-==>Parameters:MalaysiaTest(String),MY(String),100(Integer)
DEBUG-==> Preparing:UPDATEcountrySETId=Id,countryname=?,countrycode=?WHEREId=?
DEBUG-==>Parameters:MaldivesTest(String),MV(String),101(Integer)
DEBUG-==>Parameters:MaliTest(String),ML(String),102(Integer)
==========================================
...省略中间部分参数
==========================================
DEBUG-==>Parameters:SouthAfricaTest(String),ZA(String),149(Integer)
DEBUG-==>Parameters:SpainTest(String),ES(String),150(Integer)
DEBUG-==> Preparing:UPDATEcountrySETId=Id,countryname=?,countrycode=?WHEREId=?
DEBUG-==>Parameters:SriLankaTest(String),LK(String),151(Integer)
DEBUG-==>Parameters:St.LuciaTest(String),LC(String),152(Integer)
==========================================
...省略中间部分参数
==========================================
DEBUG-==>Parameters:ZaireTest(String),ZR(String),182(Integer)
DEBUG-==>Parameters:ZambiaTest(String),ZM(String),183(Integer)
==========================================
下面事务自动提交
==========================================
DEBUG-Initiatingtransactioncommit
DEBUG-CommittingJDBCtransactiononConnection[com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2]
DEBUG-ReleasingJDBCConnection[com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@752c11a2]aftertransaction
DEBUG-ReturningJDBCConnectiontoDataSource注意事项
1.事务
由于在Spring集成的情况下,事务连接由Spring管理(SpringManagedTransaction),所以这里不需要手动关闭sqlSession,在这里手动提交(commit)或者回滚(rollback)也是无效的。
2.批量提交
批量提交只能应用于insert,update,delete。
并且在批量提交使用时,如果在操作同一SQL时中间插入了其他数据库操作,就会让批量提交方式变成普通的执行方式,所以在使用批量提交时,要控制好SQL执行顺序。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。