mybatis批量保存的两种方式(高效插入)
知识点:mybatis中,批量保存的两种方式
1.使用mybatis foreach标签
2.mybatis ExecutorType.BATCH
参考博客:https://www.jb51.net/article/91951.htm
一:使用mybatis foreach标签
具体用法如下:
<!-- 批量保存(foreach插入多条数据两种方法)
int addEmpsBatch(@Param("emps") List<Employee> emps); -->
<!-- MySQL下批量保存,可以foreach遍历 mysql支持values(),(),()语法 --> //推荐使用
<insert id="addEmpsBatch">
INSERT INTO emp(ename,gender,email,did)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert>
<!-- 这种方式需要数据库连接属性allowMutiQueries=true的支持 --> //在jdbc.url=jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true
<!-- <insert
id="addEmpsBatch"> 后加上allowMultiQueries=true
<foreach collection="emps" item="emp" separator=";"> 表示可以多次执行insert into语句,中间;不会错
INSERT INTO emp(ename,gender,email,did)
VALUES(#{emp.eName},#{emp.gender},#{emp.email},#{emp.dept.id})
</foreach>
</insert> -->
二:mybatis ExecutorType.BATCH
Mybatis内置的ExecutorType有3种,默认为simple,该模式下它为每个语句的执行创建一个新的预处理语句,单条提交sql;而batch模式重复使用已经预处理的语句,并且批量执行所有更新语句,显然batch性能将更优; 但batch模式也有自己的问题,比如在Insert操作时,在事务没有提交之前,是没有办法获取到自增的id,这在某型情形下是不符合业务要求的
具体用法如下:
@Test //批量保存方法测试
public void testBatch() throws IOException{
SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
//可以执行批量操作的sqlSession
SqlSession openSession=sqlSessionFactory.openSession(ExecutorType.BATCH);
//批量保存执行前时间
long start=System.currentTimeMillis();
try{
EmployeeMapper mapper= openSession.getMapper(EmployeeMapper.class);
for (int i = 0; i < 1000; i++) {
mapper.addEmp(new Employee(UUID.randomUUID().toString().substring(0,5),"b","1"));
}
openSession.commit();
long end= System.currentTimeMillis();
//批量保存执行后的时间
System.out.println("执行时长"+(end-start));
//批量 预编译sql一次==》设置参数==》10000次==》执行1次 677
//非批量 (预编译=设置参数=执行 )==》10000次 1121
}finally{
openSession.close();
}
}
mapper和mapper.xml如下:
public interface EmployeeMapper {
//批量保存员工
public Long addEmp(Employee employee);
}
<mapper namespace="com.agesun.mybatis.dao.EmployeeMapper"
<!--批量保存员工 -->
<insert id="addEmp">
insert into employee(lastName,email,gender)
values(#{lastName},#{email},#{gender})
</insert>
</mapper>
三:补充:mybatis ExecutorType.BATCH 在SSM框架中的用法
(1)在全局配置文件applcationContext.xml中加入
<!-- 配置一个可以批量执行的sqlSession -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
(2)在serviceImpl中加入
@Autowired
private SqlSession sqlSession;
//批量保存员工
@Override
public Integer batchEmp() {
// TODO Auto-generated method stub
//批量保存执行前时间
long start=System.currentTimeMillis();
EmployeeMapper mapper= sqlSession.getMapper(EmployeeMapper.class);
for (int i = 0; i < 10000; i++) {
mapper.addEmp(new Employee(UUID.randomUUID().toString().substring(0,5),"b","1"));
}
long end= System.currentTimeMillis();
long time2= end-start;
//批量保存执行后的时间
System.out.println("执行时长"+time2);
return (int) time2;
}
mybatis批量保存的两种方式(高效插入)的更多相关文章
- mybatis中批量插入的两种方式(高效插入)
MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...
- mysql批量更新的两种方式效率试验<二>
Mysql两种批量更新的对比 简介: mysql搭载mybits框架批量更新有两种方式,一种是在xml中循环整个update语句,中间以‘:’隔开,还有一种是使用case when 变相实现批量更新, ...
- MyBatis配置数据源的两种方式
---------------------siwuxie095 MyBatis 配置数据源的两种方式 1.配置方 ...
- MyBatis获取参数值的两种方式
MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接,#{}的本质就是占位符赋值 ${}使用字符串拼接的方式拼接sql,若为字符串类型或日期类型的字段进行赋值时,需要手动加单 ...
- mybatis批量更新的两种实现方式
mapper.xml文件,后台传入一个对象集合,另外如果是mysql数据库,一点在配置文件上加上&allowMultiQueries=true,这样才可以执行多条sql,以下为mysql: & ...
- mybatis调用存储过程的两种方式
先总结和说明一下注意点: 1.如果传入的某个参数可能为空,必须指定jdbcType 2.当传入map作为参数时,必须指定JavaType 3.如果做动态查询(参数为表名,sql关键词),可以使用${} ...
- 数据库链接 mybatis spring data jpa 两种方式
jdbc mybatis spring data jpa dao service webservice jaxrs jaxws springmvc w ...
- Mybatis Dao开发的两种方式(一)
原始Dao的开发方式: 1.创建数据库配置文件db.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localh ...
- SpringBoot+MybatisPlus实现批量添加的两种方式
第一种: 因为Mysql数据每次发送sql语句的长度不能超过1M,所以,每次发送insert语句以固定长度发送: 将sql语句在provider中,以固定长度装入List集合中,然后返回service ...
随机推荐
- HDU_5514_Frogs
Frogs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- HDU3535——AreYouBusy
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题目意思:给出两个数n,T,分别表示有n个任务集合,T的总时间,对于每个任务集合有两个属性m和t ...
- cmd运行php
w D:\wamp64\bin\php\php7.0.4\php.exe 执行了 D:\wamp64\bin\php\php7.0.4\wtest.php
- The Personal Touch Client Identification 个性化接触 客户识别
w服务器要知道和谁在交谈. HTTP The Definitive Guide Web servers may talk to thousands of different clients simul ...
- Python--格式化输出%s和%d
https://www.cnblogs.com/claidx/p/7253288.html pythn print格式化输出. %r 用来做 debug 比较好,因为它会显示变量的原始数据(raw d ...
- html禁止选中文字
简单的办法,可以直接使用CSS: div { -moz-user-select:none; -webkit-user-select:none; user-select:none; } 嗯,就酱~
- 利用AES算法加密数据
准备工作: 模块安装问题: 首先在python中安装Crypto这个包 但是在安装模块后在使用过程中他会报错 下面是解决方法: pip3 install pycrypto 安装会报错 https:// ...
- 实时流计算Spark Streaming原理介绍
1.Spark Streaming简介 1.1 概述 Spark Streaming 是Spark核心API的一个扩展,可以实现高吞吐量的.具备容错机制的实时流数据的处理.支持从多种数据源获取数据,包 ...
- Oracle扩容表空间
1.程序报错,无法进行修改操作,通过日志,看到如下错误 2.通过google查询,问题是表空间文件不够了 "表空间大小(M)",(a.bytes "已使用空间(M)&qu ...
- shiro的/favicon.ico问题
登录成功之后跳转/favicon.ico问题. 1.spring-shrio.xml里面配置加上 /favicon.ico = anon 2.web.xml中配置中加上: <mime-mappi ...