参考:

https://blog.csdn.net/qq_45525848/article/details/137725872

前提:使用for循环+insert单条插入1W条用时26348ms

四、MyBatis以集合方式批量新增
1、编写UserService服务类

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;
     
    public void InsertUsers(){
        long start = System.currentTimeMillis();
        List<User> userList = new ArrayList<>();
        User user;
        for(int i = 0 ;i < 10000; i++) {
            user = new User();
            user.setUsername("name" + i);
            user.setPassword("password" + i);
            userList.add(user);
        }
        userMapper.insertUsers(userList);
        long end = System.currentTimeMillis();
        System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
    }

}

3、编写UserMapper接口

@Mapper
public interface UserMapper {

    Integer insertUsers(List<User> userList);
}

4、编写UserMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ithuang.demo.mapper.UserMapper">
    <insert id="insertUsers">
        INSERT INTO user (username, password)
        VALUES
        <foreach collection ="userList" item="user" separator =",">
            (#{user.username}, #{user.password})
        </foreach>
    </insert>
</mapper>

以下完整且规范的写法,注:如果需要MySQL支持批量操作需要在yml的url配置中新增allowMultiQueries=true,支持以;分隔批量执行SQL。

例如:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?allowMultiQueries=true&useSSL=false
    username: 
    password: 

完整代码:

<insert id="insertUserBatch">
        <foreach collection ="list" item="item" separator =";">
            INSERT INTO user
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.id != null and item.id != ''">id,</if>
                <if test="item.username != null and item.username != ''">username,</if>
                <if test="item.password != null and item.password != ''">password,</if>
                <if test="item.sex != null and item.sex != ''">sex,</if>
                <if test="item.age != null">age,</if>
                <if test="item.address != null and item.address != ''">address,</if>
                <if test="item.createTime != null">create_time,</if>
            </trim>
            VALUES
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="item.id != null and item.id != ''">#{item.id},</if>
                <if test="item.username != null and item.username != ''">#{item.username},</if>
                <if test="item.password != null and item.password != ''">#{item.password},</if>
                <if test="item.sex != null and item.sex != ''">#{item.sex},</if>
                <if test="item.age != null">#{item.age},</if>
                <if test="item.address != null and item.address != ''">#{item.address},</if>
                <if test="item.createTime != null">#{item.createTime},</if>
            </trim>
        </foreach>
    </insert>

5、输出结果

一万条数据总耗时:521ms