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)文章
Struts2學(xué)習(xí)教程之?dāng)r截器機(jī)制與自定義攔截器
這篇文章主要給大家介紹了關(guān)于Struts2學(xué)習(xí)基礎(chǔ)教程之?dāng)r截器機(jī)制與自定義攔截器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05mybatis?返回Map類型key默認(rèn)為大寫問題
這篇文章主要介紹了mybatis?返回Map類型key默認(rèn)為大寫問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis-Plus通過SQL注入器實(shí)現(xiàn)批量插入的實(shí)踐
本文主要介紹了Mybatis-Plus通過SQL注入器實(shí)現(xiàn)批量插入的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08使用Idea maven創(chuàng)建Spring項(xiàng)目過程圖解
這篇文章主要介紹了使用Idea maven創(chuàng)建Spring項(xiàng)目過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02MyBatisPlus分頁時排序的實(shí)現(xiàn)
本文主要介紹了MyBatisPlus分頁時排序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03rabbitmq的消息持久化處理開啟,再關(guān)閉后,消費(fèi)者啟動報(bào)錯問題
這篇文章主要介紹了rabbitmq的消息持久化處理開啟,再關(guān)閉后,消費(fèi)者啟動報(bào)錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11