mybatis-plus 批量插入示例代碼
正常我們使用mybatis-plus插入的時候,首先想到的是saveBatch方法,不過看了下打印出來的sql和底層代碼,才發(fā)現(xiàn)它并不是真正的批量插入。
IService 中的代碼為
default boolean saveBatch(Collection<T> entityList) {
return this.saveBatch(entityList, 1000);
}實(shí)現(xiàn)層 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", "插入一條數(shù)據(jù)(選擇字段插入)", "<script>\nINSERT INTO %s %s VALUES %s\n</script>"),通過監(jiān)控控制臺發(fā)現(xiàn),它只是循環(huán)每1000條去插入,效率非常低。
參考網(wǎng)友的文章,找到一個支持批量操作的方法,下面直接貼上代碼
1、添加批量操作參數(shù)類CustomSqlInjector
/**
* 支持自定義SQL注入方法
*/
public class CustomSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
// 獲取父類SQL注入方法列表
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
// 將批量插入方法添加進(jìn)去
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 實(shí)體列表
* @return 影響行數(shù)
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}4、將原來的Mapper業(yè)務(wù)接口,換成繼承此接口
@Mapper
public interface StudentDao extends CustomMapper<Student> {
List<Student> query(Student student);
}5、再進(jìn)行測試一下
@Transactional //事務(wù)注解要加上
@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);
}測試結(jié)果為,1000000條數(shù)據(jù),用時大概在 35秒左右

注意事項(xiàng):
1、mysql會提示超過max_allowed_packet設(shè)置的最大值,到 my.cnf文件(windows為my.ini)修改就行了,我這直接改成 200M
2、此處為示例,實(shí)際業(yè)務(wù)中循環(huán)不要放在事務(wù)里面
到此這篇關(guān)于mybatis-plus 批量插入示例的文章就介紹到這了,更多相關(guān)mybatis-plus 批量插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Spring Batch向Elasticsearch批量導(dǎo)入數(shù)據(jù)示例
本文介紹了基于Spring Batch向Elasticsearch批量導(dǎo)入數(shù)據(jù)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
java Swing JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法示例
這篇文章主要介紹了java Swing JFrame框架類中setDefaultCloseOperation的參數(shù)含義與用法,結(jié)合實(shí)例形式分析了Swing組件的JFrame框架類中setDefaultCloseOperation方法的簡單使用技巧,需要的朋友可以參考下2017-11-11
Map映射LinkedHashSet與LinkedHashMap應(yīng)用解析
這篇文章主要為大家介紹了Map映射LinkedHashSet與LinkedHashMap的應(yīng)用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步2022-03-03
java基本教程之java線程等待與java喚醒線程 java多線程教程
這篇文章主要介紹了對線程等待/喚醒方法,文中使用了多個示例,大家參考使用吧2014-01-01
關(guān)于Spring?Validation數(shù)據(jù)校檢的使用流程分析
在實(shí)際項(xiàng)目中,對客戶端傳遞到服務(wù)端的參數(shù)進(jìn)行校驗(yàn)至關(guān)重要,SpringValidation提供了一種便捷的方式來實(shí)現(xiàn)這一需求,通過在POJO類的屬性上添加檢查注解,本文給大家介紹Spring?Validation數(shù)據(jù)校檢的使用流程,感興趣的朋友一起看看吧2024-11-11
hibernate-validator后端表單數(shù)據(jù)校驗(yàn)的使用示例詳解
這篇文章主要介紹了hibernate-validator后端表單數(shù)據(jù)校驗(yàn)的使用,hibernate-validator提供的校驗(yàn)方式為在類的屬性上加入相應(yīng)的注解來達(dá)到校驗(yàn)的目的,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Java利用HttpClient模擬POST表單操作應(yīng)用及注意事項(xiàng)
本文主要介紹JAVA中利用HttpClient模擬POST表單操作,希望對大家有所幫助。2016-04-04
Java8通過CompletableFuture實(shí)現(xiàn)異步回調(diào)
這篇文章主要介紹了Java8通過CompletableFuture實(shí)現(xiàn)異步回調(diào),CompletableFuture是Java?8?中新增的一個類,它是對Future接口的擴(kuò)展,下文關(guān)于其更多相關(guān)詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04

