原文: https://blog.csdn.net/yangliuhbhd/article/details/80982254

结论: 在测试中使用simple模式提交10000条数据,时间为18248 毫秒,batch模式为5023 ,性能提高70%;

一、在mybatis中ExecutorType的使用

1.Mybatis内置的ExecutorType有3种,默认的是simple,该模式下它为每个语句的执行创建一个新的预处理语句,单条提交sql;而batch模式重复使用已经预处理的语句,

并且批量执行所有更新语句,显然batch性能将更优;

2.但batch模式也有自己的问题,比如在Insert操作时,在事务没有提交之前,是没有办法获取到自增的id,这在某型情形下是不符合业务要求的;

  1. 在测试中使用simple模式提交10000条数据,时间为18248 毫秒,batch模式为5023 ,性能提高70%;
  1. @Test
  2. public void mybatisBatch() {
  3. SqlSession session = getSqlSessionFactory().openSession();
  4. try {
  5. DeptMapper deptMapper = (DeptMapper) session.getMapper(DeptMapper.class);
  6. long start =System.currentTimeMillis();
  7. for (int i = 0; i <10000 ; i++) {
  8. SysDept dept=new SysDept(UUID.randomUUID().toString().substring(1,6), 1, new Date(), new Date(), 1);
  9. deptMapper.saveSysDept(dept);
  10. }
  11. long end =System.currentTimeMillis();
  12. System.out.println("耗时:"+(end-start));
  13. //ExecutorType.BATCH 批量耗时耗时:2134
  14. //单条操作耗时 耗时:8584
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. } finally {
  18. session.commit();
  19. session.close();
  20. }
  21. }
  22. @Test
  23. public void saveDeptBatchOne() {
  24. SqlSession session = getSqlSessionFactory().openSession();
  25. try {
  26. DeptMapper deptMapper = (DeptMapper) session.getMapper(DeptMapper.class);
  27. long start =System.currentTimeMillis();
  28. List<SysDept> deptList=new ArrayList<SysDept>();
  29. for (int i = 0; i <100000 ; i++) {
  30. SysDept dept=new SysDept(UUID.randomUUID().toString().substring(1,6), 1, new Date(), new Date(), 1);
  31. deptList.add(dept);
  32. if(i%500==0){
  33. deptMapper.saveDeptBatch(deptList);
  34. deptList.clear();
  35. }
  36. }
  37. deptMapper.saveDeptBatch(deptList);
  38. long end =System.currentTimeMillis();
  39. System.out.println("耗时:"+(end-start));
  40. //非BATCH批量耗时 耗时:938
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. } finally {
  44. session.commit();
  45. session.close();
  46. }
  47. }
  48. @Test
  49. public void saveDeptBatchTwo() {
  50. //设置ExecutorType.BATCH原理:把SQL语句发个数据库,数据库预编译好,数据库等待需要运行的参数,接收到参数后一次运行,ExecutorType.BATCH只打印一次SQL语句,多次设置参数步骤,
  51. SqlSession session = getSqlSessionFactory().openSession(ExecutorType.BATCH);
  52. try {
  53. DeptMapper deptMapper = (DeptMapper) session.getMapper(DeptMapper.class);
  54. long start =System.currentTimeMillis();
  55. List<SysDept> deptList=new ArrayList<SysDept>();
  56. for (int i = 0; i <100000; i++) {
  57. SysDept dept=new SysDept(UUID.randomUUID().toString().substring(1,6), 1, new Date(), new Date(), 1);
  58. deptList.add(dept);
  59. if(i%500==0){
  60. deptMapper.saveDeptBatch(deptList);
  61. deptList.clear();
  62. }
  63. }
  64. deptMapper.saveDeptBatch(deptList);
  65. long end =System.currentTimeMillis();
  66. System.out.println("耗时:"+(end-start));
  67. //BATCH批量耗时 耗时:822
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. } finally {
  71. session.commit();
  72. session.close();
  73. }
  74. }

二、在mybatis+spring中ExecutorType的使用

1、在spring配置文件中添加批量执行的SqlSessionTemplate

  1. <!--配置一个可以进行批量执行的sqlSession -->
  2. <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  3. <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
  4. <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  5. </bean>

2、service中获取批量添加的SqlSession

  1. @Service
  2. public class DeptService {
  3. @Autowired
  4. private DeptMapper deptMapper;
  5. @Autowired
  6. private SqlSession sqlSession;
  7. public List<Dept> addDept(){
  8. //executorType=BATCH 添加操作
  9. DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
  10. return mapper.saveDept(Dept);
  11. }
  12. }

三、$和#的区别

{}:可以获取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语句中;会有安全问题;