惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
IT之家
IT之家
M
MIT News - Artificial intelligence
博客园_首页
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
B
Blog
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
L
LangChain Blog
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Fortinet All Blogs
罗磊的独立博客
博客园 - 叶小钗
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Last Watchdog
The Last Watchdog
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
月光博客
月光博客
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 司徒正美
S
Schneier on Security
V
Vulnerabilities – Threatpost
T
Troy Hunt's Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 【当耐特】
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Forbes - Security
Forbes - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
雷峰网
雷峰网
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
V
Visual Studio Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog

博客园 - 倾听-静轩水月

Mysql优化笔记 Arthas使用 vue2.0和vue3.0同时使用 零基础尝试mybatis-plus读写分离 零基础尝试搭建docker和nacos环境 零基础尝试mysql主从复制 找回windows应用商店 linux 开放端口 小白零基础在 Centos 7 中安装 mysql http转向https 内存不够导致编译报错:Information:java: java.lang.OutOfMemoryError: GC overhead limit exceeded docker常用命令 CentOS7使用命令行安装Oracle11GR2 使用Xshell连接VMware上的Linux虚拟机 mysql免安装版初次使用 微信小程序支付证书及SSL证书使用 SqlServer无备份下误删数据恢复 javaweb学习--javabean javaweb学习--jsp
mybatis-plus 批量插入示例
倾听-静轩水月 · 2023-07-30 · via 博客园 - 倾听-静轩水月

正常我们使用mybatis-plus插入的时候,首先想到的是  saveBatch 方法,不过看了下打印出来的sql和底层代码,才发现它并不是真正的批量插入。

IService 中的代码为
    default boolean saveBatch(Collection<T> entityList) {
        return this.saveBatch(entityList, 1000);
    }

    实现层   ServiceImpl 中的代码为

    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        String sqlStatement = this.getSqlStatement(SqlMethod.INSERT_ONE);
        return this.executeBatch(entityList, batchSize, (sqlSession, entity) -> {
            sqlSession.insert(sqlStatement, entity);
        });
    }
SqlMethod.INSERT_ONE 的中文枚举为  
INSERT_ONE("insert", "插入一条数据(选择字段插入)", "<script>\nINSERT INTO %s %s VALUES %s\n</script>"),

通过监控控制台发现,它只是循环每1000条去插入,效率非常低。

参考网友的文章,找到一个支持批量操作的方法,下面直接贴上代码

1、添加批量操作参数类  CustomSqlInjector

/**
 * 支持自定义SQL注入方法
 */
public class CustomSqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        // 获取父类SQL注入方法列表
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        // 将批量插入方法添加进去
        methodList.add(new InsertBatchSomeColumn());
        return methodList;
    }
}

2、在MybatisPlusConfig中配置 

@Bean
public CustomSqlInjector customSqlInjector() {
     return new CustomSqlInjector();
}

3、添加自定义 Mapper接口 

/**
 * 自定义Mapper,添加批量插入接口
 * @param <T>
 */
@Mapper
public interface CustomMapper<T> extends BaseMapper<T> {
    /**
     * 批量插入
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}

4、将原来的Mapper业务接口,换成继承此接口

@Mapper
public interface StudentDao extends CustomMapper<Student> {
    List<Student> query(Student student);
}

5、再进行测试一下

    @Transactional  //事务注解要加上
    @Override
    public void testBatchCreate() {
        List<Student> list = new ArrayList<>();
        int startIndex = this.lambdaQuery().select(Student::getId).count();
        for (int i = 1; i <= 1000000; i++) {
            Student student = new Student();
            Long id = SnowFlake.nextId();
            student.setId(id);
            student.setCode("studentCode-" + (startIndex + i));
            student.setName("studentName-" + (startIndex + i));
            list.add(student);
        }
        Long startTime = System.currentTimeMillis();
        System.out.println("开始批量操作:" + startTime);
        int count = this.baseMapper.insertBatchSomeColumn(list);
        Long endTime = System.currentTimeMillis();
        System.out.println("完成批量操作:" + endTime);
        System.out.println("用时:" + (endTime - startTime));
        System.out.println("保存成功:" + count);
        //this.saveBatch(list);
    }

测试结果为,1000000条数据,用时大概在 35秒左右

 注意事项:

1、mysql会提示超过  max_allowed_packet  设置的最大值,到  my.cnf文件(windows为my.ini)修改就行了,我这直接改成  200M

2、此处为示例,实际业务中循环不要放在事务里面