Mybatis的三种批量操作数据的方法
方法1:
使用for循环在java代码中insert (不推荐)
方法2:
使用 在Mapper.xml当中使用 foreach循环的方式进行insert
PersonDao.java文件
public interface PersonDao {
//这个是使用 foreach方式的mybatis 批量操作
public void batchInsert(@Param("list")List<Person>list);
}
PersonDao.xml
<insert id="batchInsert" >
insert into person (
id,
person_name,
birthday,
address,
age,
gender
)
values
<foreach collection="list" item="list" index="index" separator="," >
(
#{list.id},
#{list.person_name},
#{list.birthday},
#{list.address},
#{list.age},
#{list.gender}
)
</foreach>
</insert>
主测试函数:
public static void main5(String[] args) throws Exception {
SqlSession session = getSqlSession();
PersonDao pd = session.getMapper( PersonDao.class );
List<Person>pl = new ArrayList<Person>();
Person p1 = new Person(); p1.setPerson_name("哈哈哈吧"); p1.setAddress("深圳"); p1.setBirthday(new Date());
Person p2 = new Person(); p2.setPerson_name("您好"); p2.setAddress("上海"); p2.setBirthday(new Date());
Person p3 = new Person(); p3.setPerson_name("我是张伟"); p3.setAddress("广州"); p3.setBirthday(new Date());
pl.add(p1);
pl.add(p2);
pl.add(p3);
pd.batchInsert(pl);
System.out.println("完成batchInsert");
session.commit();
session.close();
//pd.batchInsert( pl );
}
方法3:
Mybatis内置的 ExecutorType有三种,默认是Simple,该模式下它为每个语句的执行创建一个新的预处理语句,
单条提交sql,而batch模式 重复使用已经预处理的语句,并且批量执行所有更新语句,显然 batch的性能更优,
中间在提交的过程中还可以设置等待时间,避免数据库压力过大。(获取batch模式下的session)
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream( "F:\\myeclipse_workspace\\mybatisGeneratortest\\src\\test\\resources\\mybatis-config.xml");
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = ssfb.build(in);
SqlSession session = factory.openSession( ExecutorType.BATCH,false );
PersonDao pd = session.getMapper( PersonDao.class );
int size = 100000;
try {
for(int i=0;i<size;i++){
Person p = new Person();
p.setPerson_name("小明"); p.setBirthday(new Date());
pd.insertPeron(p);
if( i % 100 == 0 || i == size - 1 ){ //加上这样一段代码有好处,比如有100000条记录,每超过 100 条提交一次,中间等待10ms,可以避免一次性提交过多数据库压力过大
session.commit();
session.clearCache();
Thread.sleep(10);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
Mybatis的三种批量操作数据的方法的更多相关文章
- mybatis的三种批量插入以及次效率比较
1.表结构 CREATE TABLE `t_user` ( `id` varchar(32) CHARACTER SET utf8 NOT NULL COMMENT '主键', `name` varc ...
- sqlserver 下三种批量插入数据的方法
本文将介绍三种批量插入数据的方法,需要的朋友可以参考下 本文将介绍三种批量插入数据的方法.第一种方法是使用循环语句逐个将数据项插入到数据库中:第二种方法使用的是SqlBulkCopy,使您可以用其他源 ...
- MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.
mysql 批量更新记录 MySql中4种批量更新的方法最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共 ...
- iOS 三种收起键盘的方法
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- 【读书笔记】iOS-开发技巧-三种收起键盘的方法
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- File类三种得到路径的方法
转: File类三种得到路径的方法 2010年11月29日 20:37:00 ssyan 阅读数:27123 标签: filemicrosoftstringexceptionwindowsunix ...
- 【转】python 三种遍历list的方法
[转]python 三种遍历list的方法 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list ...
- 【Java 线程的深入研究1】Java 提供了三种创建线程的方法
Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...
- Oracle数据库三种标准的备份方法
Oracle数据库的三种标准的备份方法: 1.导出/导入(EXP/IMP). 2.热备份. 3.冷备份. 注释:导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一.导出/导入(Export/Imp ...
随机推荐
- CSS学习(5)更多的选择器
1.通配符选择器 * 表示选中所有元素 *{color:red;} 2.属性选择器 根据属性名和属性值选中元素 https://developer.mozilla.org/zh-CN/docs/We ...
- K8S集群搭建之软路由的安装
一.系统要求 ①镜像:win10 ②1C.4G.20G即可 ③仅主机模式(共享网卡上网) ④老毛桃PE ⑤ip为192.168.66网段(因为我设置的其他K8S节点也为该网段)---koolshare ...
- linux 配置php环境变量
vim /etc/profile //加上 export PATH=$PATH:/usr/local/php/bin 保存退出 source /etc/profile php -v 注:该配置对所有用 ...
- java篇 之 静态
Final:不可改变 Static:静态修饰符,在编译阶段就能确定了,可以修饰成员变量,相应的称之为静态变量 是一个共享的变量(被这个类和这个类所产生的对象所共享的,他是唯一的,出生时间 为类第一次产 ...
- Clausen Functions (and related series, functions, integrals)
Since the Clausen functions are intimately related to a number of other important special functions, ...
- TOPSIS算法
title: TOPSIS算法 date: 2020-02-24 11:18:06 categories: 数学建模 tags: [评价模型, MATLAB] mathjax: true 定义 C ...
- 迷のbug
已解决(ps over%100写错了,应该是over/100...) #include <bits/stdc++.h> #define rep(i, a, b) for(int i = a ...
- python linux windows 历史版本下载
Index of /ftp/python/ ../ 2.0/ 14-Feb-2019 14:53 - 2.0.1/ 06-Aug-2001 02:14 - 2.1/ 06-Aug-2001 02:14 ...
- SpringBoot 配置 Redis 多缓存名(不同缓存名缓存失效时间不同)
import com.google.common.collect.ImmutableMap; import org.springframework.cache.CacheManager; import ...
- 【知识学习】PHP实现批量替换字典后缀
<?php //要打开字典的物理路径 $filename = 'E:\Local Test\WWW\password.txt'; $handle = fopen($filename,'r') o ...