方法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的三种批量操作数据的方法的更多相关文章

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

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

  2. sqlserver 下三种批量插入数据的方法

    本文将介绍三种批量插入数据的方法,需要的朋友可以参考下 本文将介绍三种批量插入数据的方法.第一种方法是使用循环语句逐个将数据项插入到数据库中:第二种方法使用的是SqlBulkCopy,使您可以用其他源 ...

  3. MySql中4种批量更新的方法update table2,table1,批量更新用insert into ...on duplicate key update, 慎用replace into.

    mysql 批量更新记录 MySql中4种批量更新的方法最近在完成MySql项目集成的情况下,需要增加批量更新的功能,根据网上的资料整理了一下,很好用,都测试过,可以直接使用. mysql 批量更新共 ...

  4. iOS 三种收起键盘的方法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  5. 【读书笔记】iOS-开发技巧-三种收起键盘的方法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  6. File类三种得到路径的方法

    转: File类三种得到路径的方法 2010年11月29日 20:37:00 ssyan 阅读数:27123 标签: filemicrosoftstringexceptionwindowsunix   ...

  7. 【转】python 三种遍历list的方法

    [转]python 三种遍历list的方法 #!/usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': list ...

  8. 【Java 线程的深入研究1】Java 提供了三种创建线程的方法

    Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 1.通过实现 Runnable 接口来 ...

  9. Oracle数据库三种标准的备份方法

    Oracle数据库的三种标准的备份方法: 1.导出/导入(EXP/IMP). 2.热备份. 3.冷备份. 注释:导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一.导出/导入(Export/Imp ...

随机推荐

  1. pip install 安装指定版本的包

    pip install 安装指定版本的包   要用 pip 安装指定版本的 Python 包,只需通过 == 操作符 指定 pip install robotframework==2.8.7 将安装r ...

  2. STA之RC网

    STA的主要工作是计算电路网络的延时,如今的电路网络还是由CMOS cell和net组成的,所以STA所要计算的延时仍是电容的充放电时间.等量子计算机普及的时候,如今的这一套理论都将随着科技的进步被丢 ...

  3. <好きになるなら> 歌詞

    あー生意気なこと言ったあと 何故かしらぽつんとしてしまう あー偶然のふり待ちぶせた ゴメンネと素直に言えるかな 帰る道はいつもカナリア 変ねこのごろ自分の気持ちがよめない もうじき風の向きが変わりそう ...

  4. linux日常运维工作

    Linux的使用环境也日趋成熟,各种开源产品络绎不绝,大有百花齐放的盛景,那么当Linux落地企业,回归工作时,我们还要面对这Linux运维方面的诸多问题,今天我们特意组织一场有关Linux 在企业运 ...

  5. libcurl库的简介(一)

    一.Libcurl库简介 LibCurl是免费的客户端URL传输库,支持FTP,FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, FILE ,LDAP ...

  6. C语言笔记 10_文件读写&预处理器

    文件读写 上一章我们讲解了 C 语言处理的标准输入和输出设备.本章我们将介绍 C 程序员如何创建.打开.关闭文本文件或二进制文件. 一个文件,无论它是文本文件还是二进制文件,都是代表了一系列的字节.C ...

  7. git查漏补缺

    1. commit提交注释规范 2. commit 注释没写完或写错了,在不用删除这条commit的情况下,如何更正注释信息 git commit -m '1' git commit  --amend ...

  8. Plastic Bottle Manufacturer: Characteristic Analysis Of Plastic Packaging Bottles

    Plastic packaging bottles are usually made of 7 materials. Due to its inherent characteristics, the ...

  9. 大组合数Lucas

    https://blog.csdn.net/sr_19930829/article/details/39058487 LL Lucas(LL n, LL m, int p){ ; } Saving B ...

  10. jvm01

    hotspot:是jvm的核心组件(或者名称),jvm 需要对class文件进行编译成cpu能直接运行的代码.hotspot会对频繁使用的class代码进行缓存,不会再次编译,类似于缓存 client ...