合理的使用批量插入、更新对性能优化有很大的作用,速度明显快了N倍。

        要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql可以通过分号分割为多个独立sql。

        批量插入的最大限制主要是看你整条sql的长度大小,所以可以根据自身sql长度的大小来配置这个要分批的每批的个数。

批量插入

Dao
1
2
3
public Integer saveStudents(List<Student> students) {
        return super.getSqlSession().insert("StudentMapper.insertStudents", students);
    }
Mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<insert id="insertStudents" parameterType="java.util.List">
    insert into t_student(
        city_id
    )
    values
    <foreach collection="list" item="itm" index="index" separator=",">
        (
        #{itm.city_id}
        )
    </foreach>
</insert>

批量更新

Dao
1
2
3
public Integer updateStudents(List<Student> students) {
    return super.getSqlSession().update("StudentMapper.updateStudents", students);
}
Mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
<update id="updateStudents" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index" open=""
        close="" separator=";">
        UPDATE t_studnet
        <set>
            <if test="item.name != null">
                name= #{item.name}
            </if>
        </set>
        WHERE id = #{item.id}
        AND age = #{item.age}
    </foreach>
</update>

List分批的工具类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 分批list
 
 * @param sourceList
 *            要分批的list
 * @param batchCount
 *            每批list的个数
 * @return List<List<Object>>
 */
public static List<List<?>> batchList(List<?> sourceList, int batchCount) {
    List<List<?>> returnList = new ArrayList<>();
    int startIndex = 0// 从第0个下标开始
    while (startIndex < sourceList.size()) {
        int endIndex = 0;
        if (sourceList.size() - batchCount < startIndex) {
            endIndex = sourceList.size();
        else {
            endIndex = startIndex + batchCount;
        }
        returnList.add(sourceList.subList(startIndex, endIndex));
        startIndex = startIndex + batchCount; // 下一批
    }
    return returnList;
}


Mybatis 批量插入、批量更新的更多相关文章

  1. mybatis 注解的方式批量插入,更新数据

    一,当向数据表中插入一条数据时,一般先检查该数据是否已经存在,如果存在更新,不存在则新增  使用关键字  ON DUPLICATE KEY UPDATE zk_device_id为主键 model  ...

  2. mybatis中批量插入以及更新

    1:批量插入 批量插入就是在预编译的时候,将代码进行拼接,然后在数据库执行 <insert id="batchInsert" parameterType="java ...

  3. java批量插入或更新的问题

    在批量插入或者更新中,setXXX的时候字段类型必须一致.例如:在普通sql中 pstmt8.setBigDecimal(j ,xxx);可以写成pstmt8.setString(j,xxx.toSt ...

  4. C#使用SqlDataAdapter 实现数据的批量插入和更新

    近日由于项目要求在需要实现中型数据的批量插入和更新,晚上无聊,在网上看到看到这样的一个实现方法,特摘抄过来,以便以后可能用到参考. 一.数据的插入 DateTime begin = DateTime. ...

  5. MySQL on duplicate key update 批量插入并更新已存在数据

    业务上经常存在一种现象,需要批量往表中插入多条数据,但在执行过程中,很可能因为唯一键冲突,而导致批量插入失败.因此需要事先判断哪些数据是重复的,哪些是新增的.比较常用的处理方法就是找出已存在的数据,并 ...

  6. Mybatis中实现oracle的批量插入、更新

    oracle 实现在Mybatis中批量插入,下面测试可以使用,在批量插入中不能使用insert 标签,只能使用select标签进行批量插入,否则会提示错误 ### Cause: java.sql.S ...

  7. 【mybatis】mybatis中批量插入 批量更新 batch 进行insert 和 update,或者切割LIst进行批量操作

    ================================================================== 分别展示 mybatis 批量新增  和 批量更新   的操作: ...

  8. Mybatis 批量插入和更新小例

    SpringBoot配置Mybatis前文有博文,数据库mysql: package com.example.demo.biz.dto; public class User { private int ...

  9. MyBatis动态批量插入、更新Mysql数据库的通用实现方案

    一.业务背景 由于需要从A数据库提取大量数据同步到B系统,采用了tomikos+jta进行分布式事务管理,先将系统数据源切换到数据提供方,将需要同步的数据查询出来,然后再将系统数据源切换到数据接收方, ...

  10. Oracle+Mybatis批量插入,更新和删除

    1.插入 (1)第一种方式:利用<foreach>标签,将入参的list集合通过UNION ALL生成虚拟数据,从而实现批量插入(验证过) <insert id="inse ...

随机推荐

  1. POJ-2031 Building a Space Station---MST + 空间距离

    题目链接: https://vjudge.net/problem/POJ-2031 题目大意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则 ...

  2. 南阳OJ-91-阶乘之和---二进制枚举(入门)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91 题目大意: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为 ...

  3. 竞赛基础篇---部分和问题(DFS)

    问题链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1058 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使 ...

  4. [转]解决scrapy下载图片时相对路径转绝对路径的问题

    专注自:http://blog.csdn.net/hjy_six/article/details/6862648 这段时间一直在研究利用scrapy抓取图片的问题,我发觉,用官网的http://doc ...

  5. javascript的变量声明、数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. require.js按需加载使用简介

    一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...

  7. JDK安装、变量、变量的分类

    Lesson One 2018-04-17  19:50:35 JAVA语言特点: 编译型.强类型语言. 纯面向对象的语言,所有的代码都必须包含在class中的方法中 配置JAVA环境变量 1.安装J ...

  8. python day two,while

    一.运算符号 算数运算符:+ .-.*././/(取整除).%(去余).** 比较运算符:>.< .>=.<=.== 赋值运算符:=.+=.-=./=.%=.**= 逻辑预算符 ...

  9. STM32 - GPIO

    买了一个STM32F4的开发板,想把上面的东西重新学一下 快速过: 一.GPIO控制 void GPIO_DeInit(GPIO_TypeDef* GPIOx); //把某一个IO口恢复到默认值 /* ...

  10. [CEOI 2004]Sweet

    Description 题面链接 有 \(n\) 种糖果.第 \(i\) 种糖果有 \(m_i\) 个.取出一些糖果,至少 \(a\) 个,但不超过 \(b\) 个.求方案数. \(1\leq n\l ...