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语句中;会有安全问题;

MyBatis 学习笔记(七)批量插入ExecutorType.BATCH效率对比的更多相关文章

  1. mybatis 学习笔记(4) —— 批量新增数据

    1.业务是从前台传入List<T> ,在controller层接受参数,并进行批量新增操作. 2.需要处理的细节 a) mybatis可以支持批量新增,注意数据表需要将主键设置成自增列. ...

  2. mybatis的三种批量插入以及次效率比较

    1.表结构 CREATE TABLE `t_user` ( `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键', `name` varc ...

  3. MyBatis学习笔记(七)——Mybatis缓存

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4270403.html 一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓 ...

  4. Mybatis学习笔记(七) —— 关联查询

    一.一对多查询 需求:查询所有订单信息,关联查询下单用户信息. 注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询.如果从用户信息出发查询用户下的订单信息则 ...

  5. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  6. mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现

    项目结构  基础入门可参考:mybatis学习笔记(一)-- 简单入门(附测试Demo详细过程) 开始体验 1.新建项目,新建类MybatisUtil.java,路径:src/util/Mybatis ...

  7. mybatis学习之路----批量更新数据两种方法效率对比

    原文:https://blog.csdn.net/xu1916659422/article/details/77971696/ 上节探讨了批量新增数据,这节探讨批量更新数据两种写法的效率问题. 实现方 ...

  8. mybatis 学习笔记(四):mybatis 和 spring 的整合

    mybatis 学习笔记(四):mybatis 和 spring 的整合 尝试一下整合 mybatis 和 spring. 思路 spring通过单例方式管理SqlSessionFactory. sp ...

  9. mybatis学习笔记(14)-查询缓存之中的一个级缓存

    mybatis学习笔记(14)-查询缓存之中的一个级缓存 标签: mybatis mybatis学习笔记14-查询缓存之中的一个级缓存 查询缓存 一级缓存 一级缓存工作原理 一级缓存測试 一级缓存应用 ...

随机推荐

  1. reids 数据库学习

    最近项目中用到了redis数据库,学习整理下 redis操作学习手册--key操作命令 http://www.cnblogs.com/stephen-liu74/archive/2012/03/26/ ...

  2. (尚028)Vue_案例_交互删除

    删除一条;1.鼠标移入移除这一条时颜色有变化 2.删除当前的todo ================================================================= ...

  3. React的基本使用

    一.初始化和安装依赖 ①建立项目文件夹 mkdir react-democd react-demo ②在项目里执行命令:初始项目 npm init -y ③安装相关依赖 npm install --s ...

  4. Getting A Mime Type From A File Name In .NET Core

    Getting a mime type based on a file name (Or file extension), is one of those weird things you never ...

  5. Django 基础篇(一)

    创建虚拟环境 创建:mkvirtualenv [虚拟环境名称] 删除:rmvirtualenv [虚拟环境名称] 进入:workon [虚拟环境名称] 退出:deactivate 所有的虚拟环境,都位 ...

  6. luogu P4248 [AHOI2013]差异 SAM

    luogu P4248 [AHOI2013]差异 链接 luogu 思路 \(\sum\limits_{1<=i<j<=n}{{len}(T_i)+{len}(T_j)-2*{lcp ...

  7. 一起学Makefile(五)

    多目标规则 多目标规则,可以简单的理解为是一种将多条具有1)相同依赖和2)相同生成命令的规则,合并成一条规则的语法,其基本格式为: targets...: prerequisites... comma ...

  8. eclipse 如何将文件编辑器窗口的背景填充为背景图片?

        1.情景展示 文件编辑窗口的背景默认为白色. 我们知道,对于整日面对电脑的程序员来说,白色容易造成眼疲劳,而且对于眼睛的伤害比较大. 所以,eclipse添加了黑色主题. 切换成黑色主题 改变 ...

  9. iis php web.config处理404,500等,跳转友好页面,显示500错误信息

    显示500错误信息<pre name="code" class="html"><?xml version="1.0" en ...

  10. HDFS部署测试记录(2019/05)

    目录 HDFS部署测试记录 0.HDFS基础知识 1.基本组成结构与文件访问过程 2.NameNode启动时如何维护元数据 3.HDFS文件上传流程 1.系统环境 1.安装大致记录: 2.磁盘分区 3 ...